title: Weights for unbalanced classification
body: <p>I'm working with an unbalanced classification problem, in which the target variable contains:</p>
  
  <pre><code>np.bincount(y_train)
  array([151953,  13273])
  </code></pre>
  
  <p>i.e. <code>151953</code>  zeroes and <code>13273</code> ones.</p>
  
  <p>To deal with this I'm using <code>XGBoost</code>'s <code>weight</code> parameter when defining the DMatrix:</p>
  
  <pre><code>dtrain = xgb.DMatrix(data=X_train, 
                       label=y_train,
                       weight=weights)
  </code></pre>
  
  <p>For the weights I've been using:</p>
  
  <pre><code>bc = np.bincount(y_train)
  n_samples = bc.sum()
  n_classes = len(bc)
  weights = n_samples / (n_classes * bc)
  w = weights[y_train.values]
  </code></pre>
  
  <p>Where <code>weights</code>is <code>array([0.54367469, 6.22413923])</code>, and with the last line of code I'm just indexing it using the binary values in <code>y_train</code>. This seems like the correct approach to define the weights, since it represents the actual ratio between the amount of values of one class vs the other. However this seems to be favoring the minoritary class, which can be seen by inspecting the confusion matrix:</p>
  
  <pre><code>array([[18881, 19195],
         [  657,  2574]])
  </code></pre>
  
  <p>So just by trying out different weight values, I've realized that with a fairly close weight ratio, specifically <code>array([1, 7])</code>, the results seem much more reasonable:</p>
  
  <pre><code>array([[23020, 15056],
         [  837,  2394]])
  </code></pre>
  
  <p>So my question is:</p>
  
  <ul>
  <li>Why using the actual weights of each class is yielding poor metrics?</li>
  <li>Which is the right way to set the weights for an unbalanced problem?</li>
  </ul>