Practice
1.
from sklearn.model_selection import KFold
import numpy as np
X = np.array([1, 2, 3, 4, 5])
kf = KFold(n_splits=2)
for train_index, test_index in kf.split(X):
print("Train:", train_index, "Test:", test_index)
What will be the output of the above code?easy
Solution
Step 1: Understand KFold behavior
KFold splits the data into n_splits parts, here 2, and iterates over them.Step 2: Eliminate wrong options
Train: [0 1 2 3] Test: [4] and Train: [4] Test: [0 1 2 3] and D suggest incorrect split sizes. Train: [0 1] Test: [2 3 4] and Train: [2 3 4] Test: [0 1] suggests incorrect indexing.Final Answer:
Option B -> Option BQuick Check:
KFold splits into equal parts. [OK]
Hint: KFold splits data into equal parts. [OK]
Common Mistakes:
2. What does the following Python code output when computing the ROC curve?
from sklearn.metrics import roc_curve
import numpy as np
y_true = np.array([0, 0, 1, 1])
y_scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = roc_curve(y_true, y_scores)
print(fpr, tpr, thresholds)easy
Solution
Step 1: Understand ROC curve output
The ROC curve function returns false positive rates (fpr), true positive rates (tpr), and thresholds.Step 2: Analyze the code
Given the scores and true labels, the ROC curve is calculated correctly, resulting in fpr, tpr, and thresholds as in array([0. , 0.5, 1. ]), array([0. , 0.5, 1. ]), array([0.8, 0.35, 0.1]).Final Answer:
Option C -> Option CQuick Check:
ROC curve outputs fpr, tpr, thresholds [OK]
Hint: ROC curve outputs fpr, tpr, thresholds [OK]
Common Mistakes:
3. How would you modify the K-Means algorithm to handle a dataset with categorical features?
medium
Solution
Step 1: Handle categorical features
One-hot encoding converts categorical features into a numerical format suitable for K-Means.Step 2: Eliminate wrong options
Use Euclidean distance for all features. is incorrect because Euclidean distance isn't suitable for categorical data. Ignore categorical features during clustering. is incorrect as it loses information. Use a different clustering algorithm like DBSCAN. suggests a different algorithm, not a modification.Final Answer:
Option D -> Option DQuick Check:
Use one-hot encoding for categorical data. [OK]
Hint: Use one-hot encoding for categorical data. [OK]
Common Mistakes:
4. In a KNN classifier using KD-Tree, which scenario would make a brute force approach more suitable?
medium
Solution
Step 1: Understand KD-Tree limitations
KD-Trees are inefficient in high-dimensional spaces due to the curse of dimensionality.Step 2: Evaluate scenarios
A: Large datasets benefit from KD-Trees. C: Small datasets are manageable with brute force. D: Categorical features require different handling.Final Answer:
Option A -> Option AQuick Check:
High dimensions favor brute force over KD-Tree [OK]
Hint: High dimensions favor brute force over KD-Tree [OK]
Common Mistakes:
5. How can you optimize the train-test split with stratification for a very large dataset to reduce memory usage?
hard
Solution
Step 1: Identify memory optimization techniques
Using a generator allows processing data in batches, reducing memory footprint.Step 2: Evaluate options
In-place operations and smaller data types help, but generators are more effective for large datasets.Final Answer:
Option C -> Option CQuick Check:
Generators reduce memory by processing in batches. [OK]
Hint: Generators reduce memory by processing in batches. [OK]
Common Mistakes:
