Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the StratifiedKFold class from scikit-learn.
ML Python
from sklearn.model_selection import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
2fill in blank
mediumComplete the code to create a StratifiedKFold object with 5 splits.
ML Python
skf = StratifiedKFold(n_splits=[1], shuffle=True, random_state=42)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
3fill in blank
hardFix the error in the loop to correctly split data using StratifiedKFold.
ML Python
for train_index, test_index in skf.[1](X, y): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each fold number to the test indices.
ML Python
folds = {i: test_index for i, (_, test_index) in enumerate(skf.[1](X, y), start=[2])} Drag options to blanks, or click blank then click option'
Attempts:
3 left
5fill in blank
hardFill all three blanks to create a loop that trains a model on each stratified fold and prints accuracy.
ML Python
for train_idx, test_idx in skf.[1](X, y): model = LogisticRegression() model.[2](X[train_idx], y[train_idx]) preds = model.[3](X[test_idx]) acc = accuracy_score(y[test_idx], preds) print(f"Fold accuracy: {acc:.2f}")
Drag options to blanks, or click blank then click option'
Attempts:
3 left