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. In a decision tree, how would you handle a dataset with continuous features?
medium
Solution
Step 1: Consider handling continuous features
Discretizing into bins allows decision trees to handle continuous data effectively.Step 2: Eliminate wrong options
One-hot encoding (A) is for categorical data; ignoring (C) loses information; using directly (D) can lead to overfitting.Final Answer:
Option C -> Option CQuick Check:
Continuous? Discretize into bins [OK]
Hint: Continuous? Discretize into bins [OK]
Common Mistakes:
3. You implemented gradient checking but the numerical and analytical gradients differ significantly. What is the most likely cause?
medium
Solution
Step 1: Identify potential causes for gradient mismatch
Significant differences often indicate errors in the analytical gradient.Step 2: Eliminate other options
The step size for numerical gradient is too large. would cause small numerical errors, not significant differences. The learning rate is too high. and C are unrelated to gradient checking.Final Answer:
Option C -> Option CQuick Check:
Mismatch often means analytical error. [OK]
Hint: Mismatch often means analytical error. [OK]
Common Mistakes:
4. How can you optimize the computation of cosine similarity for a large dataset of vectors?
hard
Solution
Step 1: Identify computational bottlenecks
Looping over pairs is inefficient for large datasets.Step 2: Optimize using matrix operations
Precomputing norms and using matrix multiplication reduces redundant calculations.Final Answer:
Option C -> Option CQuick Check:
Matrix ops reduce redundancy. [OK]
Hint: Matrix ops reduce redundancy. [OK]
Common Mistakes:
5. How can the calculation of precision, recall, and F1-score be optimized for a large confusion matrix?
hard
Solution
Step 1: Identify bottlenecks
Calculating metrics for each class can be parallelized.Step 2: Choose the best optimization strategy
Parallel processing allows simultaneous computation, reducing time complexity.Final Answer:
Option C -> Option CQuick Check:
Parallelize class-wise computations. [OK]
Hint: Parallelize class-wise computations. [OK]
Common Mistakes:
