0
0
ML Pythonprogramming~10 mins

Stratified K-fold 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 StratifiedKFold class from scikit-learn.

ML Python
from sklearn.model_selection import [1]
Drag options to blanks, or click blank then click option'
AKFold
Bcross_val_score
Ctrain_test_split
DStratifiedKFold
Attempts:
3 left
2fill in blank
medium

Complete 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'
A5
B3
C10
D1
Attempts:
3 left
3fill in blank
hard

Fix 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'
Atransform
Bfit
Csplit
Dfit_transform
Attempts:
3 left
4fill in blank
hard

Fill 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'
Asplit
B0
C1
Dfit
Attempts:
3 left
5fill in blank
hard

Fill 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'
Asplit
Bfit
Cpredict
Dtransform
Attempts:
3 left