Practice
1.
import numpy as np
from sklearn.model_selection import train_test_split
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
y = np.array([0, 1, 0, 1])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)
print(X_train.shape, X_test.shape)What is the output of this code snippet?easy
Solution
Step 1: Understand train_test_split behavior.
With test_size=0.5, the data is split equally into train and test sets.Step 2: Calculate expected shapes.
Original data has 4 samples, so both train and test sets will have 2 samples each.Final Answer:
Option C -> Option CQuick Check:
Equal split with test_size=0.5. [OK]
Hint: Equal split with test_size=0.5. [OK]
Common Mistakes:
2. Given the following Python code, what will be the output if the model is overfitting?
from sklearn.metrics import accuracy_score
train_preds = model.predict(X_train)
val_preds = model.predict(X_val)
train_acc = accuracy_score(y_train, train_preds)
val_acc = accuracy_score(y_val, val_preds)
print(f'Train Accuracy: {train_acc}, Validation Accuracy: {val_acc}')easy
Solution
Step 1: Understand overfitting output
Overfitting results in high training accuracy but low validation accuracy.Step 2: Eliminate wrong options
Train Accuracy: 0.95, Validation Accuracy: 0.95 suggests good generalization, Train Accuracy: 0.60, Validation Accuracy: 0.60 suggests underfitting, and Train Accuracy: 0.60, Validation Accuracy: 0.95 is unrealistic for overfitting.Final Answer:
Option A -> Option AQuick Check:
High train, low validation = overfitting [OK]
Hint: High train, low validation = overfitting [OK]
Common Mistakes:
3. For a time series dataset, which cross-validation method is more appropriate and why?
medium
Solution
Step 1: Identify the need to respect temporal order.
Time series data requires maintaining the order of data points.Step 2: Eliminate wrong options.
K-Fold (A) and Stratified K-Fold (B) do not respect temporal order, Leave-One-Out (D) is computationally expensive and doesn't address temporal order.Final Answer:
Option A -> Option AQuick Check:
Time series needs temporal order. [OK]
Hint: Time series needs temporal order. [OK]
Common Mistakes:
4. How would you adapt K-Fold cross-validation for a dataset with a temporal component and class imbalance?
hard
Solution
Step 1: Address both temporal order and class imbalance.
Time Series Split respects temporal order, and manual balancing addresses class imbalance.Step 2: Eliminate wrong options.
Standard K-Fold (A) ignores temporal order, Stratified K-Fold (B) doesn't respect temporal order, Leave-One-Out (D) is computationally expensive and doesn't address temporal order.Final Answer:
Option D -> Option DQuick Check:
Time series + imbalance = manual balance. [OK]
Hint: Time series + imbalance = manual balance. [OK]
Common Mistakes:
5. You are tasked with designing a system to handle label drift in a real-time recommendation engine. Which approach would be most effective?
hard
Solution
Step 1: Identify the best approach for handling label drift.
A feedback loop allows the model to adapt to changes in label distribution by learning from recent interactions.Step 2: Eliminate wrong options.
Static mappings (Use a static label mapping based on historical data.) and increasing dataset size (Increase the size of the training dataset.) don't adapt to real-time changes. Feature selection (Apply feature selection to reduce dimensionality.) doesn't address label drift.Final Answer:
Option A -> Option AQuick Check:
Feedback loop adapts to label drift [OK]
Hint: Feedback loop adapts to label drift [OK]
Common Mistakes:
