0
0
ML Pythonml~10 mins

Imbalanced class handling (SMOTE, class weights) in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the SMOTE class from the imblearn library.

ML Python
from imblearn.[1] import SMOTE
Drag options to blanks, or click blank then click option'
Aover_sampling
Bunder_sampling
Censemble
Dmetrics
Attempts:
3 left
💡 Hint
Common Mistakes
Importing SMOTE from the wrong module like under_sampling or metrics.
Misspelling the module name.
2fill in blank
medium

Complete the code to create a SMOTE object with a random state for reproducibility.

ML Python
smote = SMOTE(random_state=[1])
Drag options to blanks, or click blank then click option'
ANone
B42
C'seed'
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for random_state.
Passing None which disables reproducibility.
3fill in blank
hard

Fix the error in the code to fit and resample the training data using SMOTE.

ML Python
X_resampled, y_resampled = smote.[1](X_train, y_train)
Drag options to blanks, or click blank then click option'
Afit
Btransform
Cfit_transform
Dsample
Attempts:
3 left
💡 Hint
Common Mistakes
Using fit() which returns the object but not data.
Using transform() which requires prior fitting.
4fill in blank
hard

Fill both blanks to create a logistic regression model with balanced class weights and fit it on training data.

ML Python
model = LogisticRegression(class_weight=[1])
model.[2](X_train, y_train)
Drag options to blanks, or click blank then click option'
A'balanced'
Bfit
Cpredict
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing None or wrong string to class_weight.
Using predict instead of fit to train the model.
5fill in blank
hard

Fill all three blanks to compute class weights manually and train a logistic regression model with them.

ML Python
from sklearn.utils.class_weight import compute_class_weight

weights = compute_class_weight(class_weight=[1], classes=[2], y=[3])
class_weights = dict(zip([2], weights))
model = LogisticRegression(class_weight=class_weights)
model.fit(X_train, y_train)
Drag options to blanks, or click blank then click option'
A'balanced'
Bnp.unique(y_train)
Cy_train
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing None instead of 'balanced' to compute_class_weight.
Using wrong variables for classes or y parameters.