Model Pipeline - Imbalanced class handling (SMOTE, class weights)
This pipeline shows how to handle imbalanced classes in a dataset using SMOTE to create synthetic samples and class weights to help the model learn better from minority classes.
Jump into concepts and practice - no test required
This pipeline shows how to handle imbalanced classes in a dataset using SMOTE to create synthetic samples and class weights to help the model learn better from minority classes.
Loss
0.7 |*
0.6 | *
0.5 | *
0.4 | *
0.3 | *
0.2 | *
0.1 |
+--------
1 2 3 4 5 Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.65 | 0.70 | Model starts learning, loss high, accuracy moderate |
| 2 | 0.48 | 0.80 | Loss decreases, accuracy improves |
| 3 | 0.35 | 0.87 | Model learns minority class better |
| 4 | 0.28 | 0.90 | Loss continues to decrease, accuracy rises |
| 5 | 0.22 | 0.92 | Training converges with good balance |
class_weight and it accepts 'balanced' to auto-adjust weights.class_weight='balanced'.from imblearn.over_sampling import SMOTE X = [[1], [2], [3], [4], [5], [6]] y = [0, 0, 0, 1, 1, 1] smote = SMOTE(random_state=42) X_resampled, y_resampled = smote.fit_resample(X, y) print(len(X_resampled), len(y_resampled))
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(class_weight={'0':1, '1':10})
model.fit(X_train, y_train)