Junior — Middle
What is class imbalance and how to deal with it?
sobes.tech AI
Answer from AI
Class imbalance is a situation in a classification task where the number of objects of one class significantly exceeds the number of objects of another (or other) classes. This leads to the model potentially performing poorly on underrepresented classes, as it tends to optimize for the majority.
Methods to address class imbalance:
- Resampling: increasing the number of objects of the minority class (oversampling) or decreasing the number of objects of the majority class (undersampling).
- Using special metrics: for example, F1-score, ROC-AUC instead of simple accuracy.
- Applying algorithms resistant to imbalance: for example, ensemble methods, class-weighted methods.
- Generating synthetic data: methods like SMOTE create new objects for the minority class.
Example of using SMOTE:
from imblearn.over_sampling import SMOTE
X_resampled, y_resampled = SMOTE().fit_resample(X, y)
This helps improve the model's performance on rare classes.