Practice
1.
import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
y = np.array([1, 0, 1])
selector = SelectKBest(score_func=f_classif, k=2)
X_new = selector.fit_transform(X, y)
print(X_new)What does the above code output?easy
Solution
Step 1: Understand SelectKBest.
SelectKBest selects the top k features based on the highest scores from the scoring function.Step 2: Determine the output.
f_classif scores features based on ANOVA F-value. Here, features 2 and 3 have the highest scores.Final Answer:
Option A -> Option AQuick Check:
Top k features by score. [OK]
Hint: SelectKBest picks top k features by score. [OK]
Common Mistakes:
2. In the context of handling class imbalance, which technique is specifically designed to adjust the loss function to focus more on hard-to-classify examples?
easy
Solution
Step 1: Identify the technique that modifies the loss function.
Focal Loss is designed to focus more on hard-to-classify examples by adjusting the loss function.Step 2: Eliminate wrong options.
Oversampling and undersampling adjust the dataset, not the loss function. Cost-sensitive learning modifies the cost, not the focus on hard examples.Final Answer:
Option D -> Option DQuick Check:
Focal Loss focuses on hard examples. [OK]
Hint: Focal Loss focuses on hard examples. [OK]
Common Mistakes:
3. Given the following Python code snippet for evaluating a model offline, what will be the output if the test set is not representative of the real-world data?
from sklearn.metrics import accuracy_score
y_true = [0, 1, 0, 1]
y_pred = [0, 1, 1, 0]
accuracy = accuracy_score(y_true, y_pred)
print(accuracy)easy
Solution
Step 1: Calculate the accuracy score.
The accuracy score is calculated as the number of correct predictions divided by the total number of predictions. Here, 2 out of 4 predictions are correct, so the accuracy is 0.5.Step 2: Consider the representativeness of the test set.
If the test set is not representative, the accuracy score may not reflect real-world performance.Final Answer:
Option A -> Option AQuick Check:
Accuracy 0.5, but test set matters. [OK]
Hint: Accuracy 0.5, but test set matters. [OK]
Common Mistakes:
4. In a regression problem where outliers are present, which metric is generally more robust to these outliers?
easy
Solution
Step 1: Understand the impact of outliers on metrics.
MAE is less sensitive to outliers compared to MSE and RMSE because it doesn't square the errors.Step 2: Eliminate wrong options.
MSE and RMSE are sensitive to outliers due to squaring errors. MAPE is not suitable for zero or near-zero values.Final Answer:
Option D -> Option DQuick Check:
MAE handles outliers better than MSE. [OK]
Hint: MAE is less sensitive to outliers than MSE. [OK]
Common Mistakes:
5. A model performs well overall but poorly on a specific slice of data. What is the most likely cause?
medium
Solution
Step 1: Analyze the problem
Poor performance on a specific slice often indicates distribution mismatch.Step 2: Eliminate wrong options
The model is overfitting to the training data. is incorrect as overfitting would affect overall performance. The model has too many parameters. is unrelated to slice-specific issues. The learning rate is too high. affects convergence, not slice performance.Final Answer:
Option A -> Option AQuick Check:
Slice mismatch = distribution issue. [OK]
Hint: Slice mismatch = distribution issue. [OK]
Common Mistakes:
