Challenge - 5 Problems
K-fold Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of K-fold split indices
Given the following code snippet using sklearn's KFold, what will be the output of the printed train and test indices for fold 1?
TensorFlow
from sklearn.model_selection import KFold import numpy as np X = np.array([10, 20, 30, 40, 50]) kf = KFold(n_splits=5, shuffle=False) for fold, (train_index, test_index) in enumerate(kf.split(X), 1): if fold == 1: print('Train indices:', train_index) print('Test indices:', test_index)
Attempts:
2 left
💡 Hint
Remember that KFold splits data into equal parts without shuffling by default.
✗ Incorrect
With 5 splits and no shuffle, fold 1 test set is the first element (index 0), so train indices are the rest.
❓ Model Choice
intermediate2:00remaining
Choosing model evaluation strategy
You have a small dataset and want to estimate your TensorFlow model's performance reliably. Which cross-validation strategy is best to reduce variance in performance estimates?
Attempts:
2 left
💡 Hint
More folds with shuffling usually give better performance estimates on small datasets.
✗ Incorrect
10-fold CV with shuffling reduces bias and variance better than fewer folds or no shuffling.
❓ Hyperparameter
advanced2:00remaining
Effect of number of folds on training time
If you increase the number of folds in K-fold cross-validation from 5 to 10 for training a TensorFlow model, what is the expected effect on total training time?
Attempts:
2 left
💡 Hint
Each fold requires training a separate model on a subset of data.
✗ Incorrect
Doubling folds means training twice as many models, roughly doubling total training time.
❓ Metrics
advanced2:00remaining
Calculating average accuracy from K-fold results
You performed 4-fold cross-validation on a classification model and got accuracies: [0.82, 0.85, 0.80, 0.83]. What is the correct average accuracy to report?
Attempts:
2 left
💡 Hint
Average accuracy is the mean of fold accuracies; formatting can vary.
✗ Incorrect
All options represent the same average accuracy in different formats.
🔧 Debug
expert3:00remaining
Identifying error in K-fold TensorFlow training loop
Consider this TensorFlow K-fold training loop code snippet. What error will it raise?
```python
from sklearn.model_selection import KFold
import tensorflow as tf
import numpy as np
X = np.random.rand(100, 10)
y = np.random.randint(0, 2, 100)
kf = KFold(n_splits=5, shuffle=True)
for train_idx, test_idx in kf.split(X):
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X[train_idx], y[train_idx], epochs=3)
loss, acc = model.evaluate(X[test_idx], y[test_idx])
print(f"Test accuracy: {acc}")
print(model.predict(X))
```
Attempts:
2 left
💡 Hint
Check where the variable 'model' is defined and used.
✗ Incorrect
The variable 'model' is defined inside the loop and not accessible after the loop ends, causing NameError.