Practice
1. In the context of cross-validation, which method is most appropriate for imbalanced datasets?
easy
Solution
Step 1: Identify the need for balanced class distribution.
Imbalanced datasets require maintaining class distribution in each fold.Step 2: Eliminate wrong options.
K-Fold does not ensure class balance (A), Time Series Split is for temporal data (C), Leave-One-Out is computationally expensive and doesn't address imbalance (D).Final Answer:
Option C -> Option CQuick Check:
Stratified keeps class balance. [OK]
Hint: Stratified keeps class balance. [OK]
Common Mistakes:
2. What will be the output shape of the following code snippet using K-Fold cross-validation with 5 splits on a dataset of 100 samples?
from sklearn.model_selection import KFold
kf = KFold(n_splits=5)
for train_index, test_index in kf.split(range(100)):
print(len(test_index))easy
Solution
Step 1: Calculate the number of samples per fold.
100 samples divided by 5 splits results in 20 samples per fold.Step 2: Eliminate wrong options.
25 samples per fold (B) is incorrect, 6 outputs (C) is incorrect, 4 outputs (D) is incorrect.Final Answer:
Option A -> Option AQuick Check:
100 samples, 5 splits = 20 per fold. [OK]
Hint: 100 samples, 5 splits = 20 per fold. [OK]
Common Mistakes:
3. What will be the output of the following Python code snippet using XGBoost?
import xgboost as xgb
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
data = load_iris()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
params = {'objective': 'multi:softprob', 'num_class': 3}
model = xgb.train(params, dtrain, num_boost_round=10)
preds = model.predict(dtest)
print(preds.shape)easy
Solution
Step 1: Understand the code
The code splits the Iris dataset into training and testing sets, with 20% for testing, resulting in 30 test samples.Step 2: Analyze the prediction output
The model predicts probabilities for 3 classes, so the output shape is (30, 3).Final Answer:
Option B -> Option BQuick Check:
Test set size = 30, 3 classes [OK]
Hint: Test set size = 30, 3 classes [OK]
Common Mistakes:
4. In the context of K-Means clustering, which initialization method is most likely to avoid poor local minima?
easy
Solution
Step 1: Understand initialization impact
K-Means++ initialization helps in spreading initial centroids to avoid poor local minima.Step 2: Eliminate wrong options
Random initialization can lead to poor local minima. Using centroids from a previous run might not improve if the previous run was suboptimal. Initializing all centroids to the same point is clearly incorrect.Final Answer:
Option A -> Option AQuick Check:
K-Means++ spreads centroids well [OK]
Hint: K-Means++ spreads centroids well [OK]
Common Mistakes:
5. For a dataset with significant outliers, which scaling method is more appropriate and why?
medium
Solution
Step 1: Consider the effect of outliers on scaling methods.
Normalization is sensitive to outliers as it scales data to a fixed range.Step 2: Choose the method less affected by outliers.
Standardization is less affected because it centers data around the mean and scales by standard deviation.Final Answer:
Option D -> Option DQuick Check:
Standardize for outliers, less sensitive. [OK]
Hint: Standardize for outliers, less sensitive. [OK]
Common Mistakes:
