Bird
Raised Fist0

Implement Train-Test Split with Stratification

CODINGbeginnerInterview freq: high
Choose your preparation mode6 modes available
▶
Execution Trace
Step 1/4
Initial Dataset

Dataset contains 10 samples with labels: [A, A, B, B, B, C, C, C, C, C]

🎯 Understanding the initial distribution of labels is crucial for stratification.
from sklearn.model_selection import train_test_split
X = ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C']
y = X  # Labels are the same as data for simplicity
Stratification

Data is stratified by label to maintain distribution in train and test sets.

🎯 Stratification ensures that the train and test sets are representative of the overall dataset.
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.4, stratify=y)
Train Set

Train set contains 6 samples: [A, B, B, C, C, C]

🎯 The train set should have a balanced representation of each class.
print('Train:', X_train)
Test Set

Test set contains 4 samples: [A, B, C, C]

🎯 The test set should also maintain the class distribution.
print('Test:', X_test)
📊
Diagram
Diagram
┌───────────────────────────────────────────────────────────────┐
│                      Stratified Split                         │
├───────────────────────────────────────────────────────────────┤
│ Dataset: [A, A, B, B, B, C, C, C, C, C]                       │
│                                                               │
│ Stratify by label:                                            │
│   A: [A, A] -> Train: [A], Test: [A]                         │
│   B: [B, B, B] -> Train: [B, B], Test: [B]                   │
│   C: [C, C, C, C, C] -> Train: [C, C, C], Test: [C, C]       │
│                                                               │
│ Result:                                                       │
│ Train: [A, B, B, C, C, C]                                     │
│ Test: [A, B, C, C]                                            │
└───────────────────────────────────────────────────────────────┘
  • Dataset: Initial data with labels A, B, C
  • Stratify by label: Ensures proportional representation
  • Train: Subset of data for training
  • Test: Subset of data for testing
🔑
Key Moments
▲
Q: Why is stratification important in train-test splitting?
A: Stratification ensures that the train and test sets have the same distribution of classes as the original dataset, which is crucial for models to generalize well.
Q: What would happen if we didn't use stratification?
A: Without stratification, the train and test sets might have imbalanced class distributions, leading to biased model performance.
Q: How does the stratified split affect model evaluation?
A: A stratified split provides a more reliable evaluation of model performance by ensuring that both the train and test sets are representative of the entire dataset.

This visual trace document explains how to implement a train-test split with stratification using Python. The diagram shows a dataset with labels A, B, and C, and how it is divided into train and test sets while maintaining the class distribution. The trace steps detail the execution of the stratified split, highlighting the importance of maintaining proportional representation of classes in both sets. Key moments emphasize the significance of stratification in ensuring balanced datasets, which is crucial for accurate model evaluation and generalization.

Primary Focus

✓ Stratified train-test split implementation

Practice

(1/5)
1. What does the following Python code output when applied to an image array img?
import numpy as np

def flip_image(img):
    return np.flip(img, axis=1)

img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
flipped_img = flip_image(img)
print(flipped_img)
easy
A. [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
B. [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
C. [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
D. [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

Solution

  1. Step 1: Understand the flip operation

    The np.flip function flips the array along the specified axis. Here, axis=1 means flipping horizontally.
  2. Step 2: Apply the flip to the array

    Flipping horizontally changes the order of elements in each row.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Horizontal flip reverses row elements. [OK]
Hint: Horizontal flip reverses row elements. [OK]
Common Mistakes:
2. What does the following Python code snippet output when implementing the Skip-Gram model with negative sampling?
import numpy as np

vocab_size = 5
embedding_dim = 3

# Initialize random embeddings
embeddings = np.random.rand(vocab_size, embedding_dim)

# Select a word index
word_index = 2

# Get the embedding for the word
word_embedding = embeddings[word_index]

print(word_embedding.shape)
easy
A. (3, 5)
B. (5, 3)
C. (3,)
D. (1, 3)

Solution

  1. Step 1: Understand the code

    The code initializes a random embedding matrix of shape (vocab_size, embedding_dim) and retrieves the embedding for a specific word index.
  2. Step 2: Determine the shape of the retrieved embedding

    The embedding for a single word is a 1D array with length equal to embedding_dim, hence the shape is (3,).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Single word embedding shape is (embedding_dim,) [OK]
Hint: Single word embedding shape is (embedding_dim,) [OK]
Common Mistakes:
3.
import numpy as np

# Backpropagation for a single neuron
inputs = np.array([1.0, 2.0, 3.0])
weights = np.array([0.2, 0.8, -0.5])
bias = 2.0
learning_rate = 0.01

d_output = np.array([1.0])  # Gradient of loss w.r.t output

# Incorrect gradient calculation
weights_gradient = d_output * inputs
weights -= learning_rate * weights_gradient
print(weights)
medium
A. Weights are updated correctly.
B. Weights are updated incorrectly due to incorrect learning rate.
C. Weights are not updated; learning rate is too small.
D. Weights are updated incorrectly due to missing chain rule.

Solution

  1. Step 1: Identify error in gradient calculation

    The code incorrectly applies the gradient directly without considering the chain rule.
  2. Step 2: Correct approach

    Should multiply by the derivative of the activation function.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Always apply chain rule in backpropagation [OK]
Hint: Always apply chain rule in backpropagation [OK]
Common Mistakes:
4. You implemented gradient checking but the numerical and analytical gradients differ significantly. What is the most likely cause?
medium
A. The model is overfitting.
B. The step size for numerical gradient is too large.
C. The analytical gradient is incorrect.
D. The learning rate is too high.

Solution

  1. Step 1: Identify potential causes for gradient mismatch

    Significant differences often indicate errors in the analytical gradient.
  2. 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.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Mismatch often means analytical error. [OK]
Hint: Mismatch often means analytical error. [OK]
Common Mistakes:
5. In a Random Forest implementation, how would you apply feature sampling to handle high-dimensional data effectively?
medium
A. Use all features for each tree to ensure accuracy
B. Use a fixed subset of features for all trees
C. Randomly select a small subset of features for each tree
D. Select features based on their correlation with the target

Solution

  1. Step 1: Understand Feature Sampling

    Feature sampling involves selecting a random subset of features for each tree to reduce dimensionality.
  2. Step 2: Evaluate Options

    Using all features (A) or a fixed subset (C) doesn't reduce dimensionality; correlation-based selection (D) isn't random.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Random subsets reduce dimensionality [OK]
Hint: Random subsets reduce dimensionality [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