title: F1 score reduced after using class weight
body: <p>I am working on a multi class classification use case and the data is highly imbalanced. By highly imbalanced data I mean that there is a huge difference between class with maximum frequency and the class with minimum frequency. So if I go ahead using <code>SMOTE oversampling</code> then the data size increases tremendously (data size goes from 280k rows to more than 25 billion rows because the imbalance is too high) and it becomes practically impossible to fit a ML model to such a huge dataset. Similarly I can't use undersampling as that would lead to loss of information. </p>
  
  <p>So I thought of using <a href="https://scikit-learn.org/stable/modules/generated/sklearn.utils.class_weight.compute_class_weight.html" rel="nofollow noreferrer"><code>compute_class_weight</code></a>  from sklearn while creating a ML model.</p>
  
  <p>Code:</p>
  
  <pre><code>from sklearn.utils.class_weight import compute_class_weight
  
  class_weight = compute_class_weight(class_weight='balanced',
                                      classes=np.unique(train_df['Label_id']),
                                      y=train_df['Label_id'])
  
  dict_weights = dict(zip(np.unique(train_df['Label_id']), class_weight))
  
  svc_model = LinearSVC(class_weight=dict_weights)
  </code></pre>
  
  <p>I did predictions on test data and noted the result of metrics like <code>accuracy</code>, <code>f1_score</code>, <code>recall</code> etc.
  I tried to replicate the same but by not passing <code>class_weight</code>, like this:</p>
  
  <p><code>svc_model = LinearSVC()</code></p>
  
  <p>But the results I obtained were strange. The metrics after passing <code>class_weight</code> were a bit poor than the metrics without <code>class_weight</code>.</p>
  
  <p>I was hoping for the exact opposite as I am using <code>class_weight</code> to make the model better and hence the metrics.</p>
  
  <p>The difference between metrics for both the models was minimal but <code>f1_score</code> was less for model with <code>class_weight</code> as compared to model without <code>class_weight</code>.</p>
  
  <p>I also tried the below snippet:</p>
  
  <p><code>svc_model = LinearSVC(class_weight='balanced')</code></p>
  
  <p>but still the <code>f1_score</code> was less as compared to model without <code>class_weight</code>.</p>
  
  <p>Below are the metrics I obtained:</p>
  
  <pre><code>LinearSVC w/o class_weight
  
  Accuracy: 89.02, F1 score: 88.92, Precision: 89.17, Recall: 89.02, Misclassification error: 10.98
  
  LinearSVC with class_weight=’balanced’
  
  Accuracy: 87.98, F1 score: 87.89, Precision: 88.3, Recall: 87.98, Misclassification error: 12.02
  
  LinearSVC with class_weight=dict_weights
  
  Accuracy: 87.97, F1 score: 87.87, Precision: 88.34, Recall: 87.97, Misclassification error: 12.03
  </code></pre>
  
  <p>I assumed that using <code>class_weight</code> would improve the metrics but instead its deteriorating the metrics. Why is this happening and what should I do? Will it be okay if I don't handle imbalance data?</p>