Bird
Raised Fist0

Implement Cross-Validation - K-Fold, Stratified

CODINGintermediateInterview freq: high
Choose your preparation mode6 modes available
▶
Execution Trace
Step 1/5
Data Splitting

Data indices [1, 2, 3, 4] are split into 4 folds.

🎯 Understanding how data is partitioned into folds is crucial for cross-validation.
from sklearn.model_selection import KFold
data = [1, 2, 3, 4]
kf = KFold(n_splits=4)
for train_index, test_index in kf.split(data):
    print('Train:', train_index, 'Test:', test_index)
First Fold

Train indices: [2, 3, 4], Test indices: [1]

🎯 Each fold uses a different subset of data for testing.
train_index, test_index = next(kf.split(data))
train_data = [data[i] for i in train_index]
test_data = [data[i] for i in test_index]
print('Train Data:', train_data, 'Test Data:', test_data)
Model Training

Model is trained on indices [2, 3, 4].

🎯 Training on different subsets helps in generalizing the model.
train_labels = [0, 1, 0]
model.fit(train_data, train_labels)
Model Evaluation

Model is evaluated on index [1].

🎯 Evaluation on unseen data is key to assessing model performance.
test_labels = [1]
predictions = model.predict(test_data)
accuracy = accuracy_score(test_labels, predictions)
print('Accuracy:', accuracy)
Stratified Sampling

Ensures each fold has the same proportion of class labels.

🎯 Stratification is important for imbalanced datasets.
from sklearn.model_selection import StratifiedKFold
labels = [0, 1, 0, 1]
skf = StratifiedKFold(n_splits=4)
for train_index, test_index in skf.split(data, labels):
    print('Train:', train_index, 'Test:', test_index)
📊
Diagram
Diagram
┌───────────────────────────────┐
│       K-Fold Cross-Val        │
├─────────────┬─────────────────┤
│ Fold 1      │ Train: [2, 3, 4]│
│             │ Test: [1]       │
├─────────────┼─────────────────┤
│ Fold 2      │ Train: [1, 3, 4]│
│             │ Test: [2]       │
├─────────────┼─────────────────┤
│ Fold 3      │ Train: [1, 2, 4]│
│             │ Test: [3]       │
├─────────────┼─────────────────┤
│ Fold 4      │ Train: [1, 2, 3]│
│             │ Test: [4]       │
└─────────────┴─────────────────┘
  • Fold 1: First partition of data
  • Train: Indices used for training
  • Test: Indices used for testing
🔑
Key Moments
▲
Q: Why is it important to use different data for testing in each fold?
A: Using different data for testing in each fold ensures that the model is evaluated on various subsets, providing a more robust estimate of its performance.
Q: How does stratified sampling improve cross-validation?
A: Stratified sampling maintains the class distribution across folds, which is crucial for imbalanced datasets to ensure each fold is representative of the whole dataset.
Q: What is the main advantage of K-Fold Cross-Validation?
A: The main advantage is that it reduces the variance of the performance estimate by averaging over multiple folds, leading to a more reliable assessment of the model's generalization ability.

This diagram and execution trace illustrate the process of K-Fold Cross-Validation, where data is divided into multiple folds, each serving as a test set once while the others form the training set. The trace highlights key steps such as data splitting, model training, and evaluation. Stratified sampling is also demonstrated, ensuring class balance across folds. This method provides a comprehensive evaluation of model performance, crucial for robust machine learning model development.

Primary Focus

✓ Implementing K-Fold and Stratified Cross-Validation

Practice

(1/5)
1. What is the time complexity of a single iteration of gradient descent for linear regression with n samples and d features?
easy
A. O(n)
B. O(d)
C. O(nd)
D. O(n^2)

Solution

  1. Step 1: Identify operations in a single iteration

    Each iteration involves matrix multiplication and gradient computation.
  2. Step 2: Calculate complexity

    Matrix multiplication and gradient computation both require O(nd) operations.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Matrix operations scale with samples and features. [OK]
Hint: Matrix operations scale with samples and features. [OK]
Common Mistakes:
2. What does the following Python code output when implementing logistic regression with a sigmoid activation function?
import numpy as np

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

z = np.array([0, 2, -2])
output = sigmoid(z)
print(output)
easy
A. [0.5, 0.88, 0.12]
B. [0.5, 0.88, 0.11]
C. [0.5, 0.87, 0.12]
D. [0.5, 0.86, 0.12]

Solution

  1. Step 1: Calculate sigmoid for each element

    Sigmoid function is applied element-wise: sigmoid(0) = 0.5, sigmoid(2) ≈ 0.88, sigmoid(-2) ≈ 0.12.
  2. Step 2: Check precision of calculations

    Common mistake is rounding errors. Correct values are [0.5, 0.880797, 0.119203], rounded to [0.5, 0.88, 0.12].
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Sigmoid(0)=0.5, Sigmoid(2)≈0.88, Sigmoid(-2)≈0.12 [OK]
Hint: Sigmoid(0)=0.5, Sigmoid(2)≈0.88, Sigmoid(-2)≈0.12 [OK]
Common Mistakes:
3. In the context of implementing linear regression with gradient descent, how would you handle a dataset with missing values?
medium
A. Impute missing values using the mean of each feature.
B. Ignore missing values during training.
C. Replace missing values with zeros.
D. Remove any samples with missing values.

Solution

  1. Step 1: Consider impact of missing values

    Ignoring or zero-filling can bias results; removing samples reduces data.
  2. Step 2: Choose appropriate imputation

    Mean imputation is a common strategy that maintains dataset size and reduces bias.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Mean imputation balances bias and data retention. [OK]
Hint: Mean imputation balances bias and data retention. [OK]
Common Mistakes:
4. In the context of logistic regression, which approach is correct for handling a dataset with highly imbalanced classes?
medium
A. Use accuracy as the primary evaluation metric.
B. Apply SMOTE to balance the dataset.
C. Increase the learning rate to improve minority class prediction.
D. Use a linear activation function instead of sigmoid.

Solution

  1. Step 1: Identify the problem with imbalanced classes

    Imbalanced classes can lead to biased predictions towards the majority class.
  2. Step 2: Choose a method to address imbalance

    SMOTE (Synthetic Minority Over-sampling Technique) is effective for balancing datasets.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    SMOTE balances classes effectively [OK]
Hint: SMOTE balances classes effectively [OK]
Common Mistakes:
5. How would you apply sinusoidal positional encoding in a transformer model for a sequence of variable length?
medium
A. Apply encoding only to the first few positions of each sequence.
B. Apply encoding to each sequence independently, based on its length.
C. Pad all sequences to the maximum length and apply encoding.
D. Use a fixed-length encoding for all sequences, regardless of length.

Solution

  1. Step 1: Consider variable sequence lengths

    Each sequence should have positional encoding applied based on its actual length to maintain positional information.
  2. Step 2: Eliminate wrong options

    Pad all sequences to the maximum length and apply encoding. is inefficient and may introduce unnecessary padding. Apply encoding only to the first few positions of each sequence. ignores the rest of the sequence. Use a fixed-length encoding for all sequences, regardless of length. loses positional information for longer sequences.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Encode based on actual sequence length. [OK]
Hint: Encode based on actual sequence length. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard