Bird
Raised Fist0

Feature Scaling - Normalization vs Standardization, When Each Works

THEORYbeginnerInterview freq: high
Choose your preparation mode6 modes available
🐛
The Vanishing Recall
Started after the last feature update two days ago

Fraud detection model for a financial service processing 500K transactions/day

Symptom
Model accuracy is stable at 0.92, but recall has dropped to 0
📡
Signals
metric
Recall on validation set
Recall dropped from 0.85 to 0.00 after the last feature update
feature_importance
Feature importance ranking
New feature 'transaction_amount_scaled' ranked #1 with importance 0.45
distribution
Distribution of 'transaction_amount_scaled'
Highly skewed with most values near 0
log
Model training logs
Gradient explosion warnings during training
metric
Validation AUC
AUC stable at 0.92
🔬
Hypotheses
Improper scaling of 'transaction_amount_scaled' causing gradient issues
Gradient explosion warningsHighly skewed distribution of 'transaction_amount_scaled'
Verify: Check if normalization was applied instead of standardization
import numpy as np; mean = np.mean(df['transaction_amount_scaled']); std_dev = np.std(df['transaction_amount_scaled']); print(f'Mean: {mean}, Std Dev: {std_dev}')
New feature 'transaction_amount_scaled' is dominating the model
Feature importance rankingRecall dropped to 0
Verify: Assess the impact of removing 'transaction_amount_scaled'
model.fit(X.drop(columns=['transaction_amount_scaled']), y)
🎯
Root Cause
Improper normalization of 'transaction_amount_scaled' leading to skewed feature distribution

Normalization was applied instead of standardization, causing the feature to dominate the model

🔧
Fix Strategy
ImmediateRollback to previous model version
Short-termApply standardization to 'transaction_amount_scaled'
Long-termImplement automated checks for feature scaling consistency
🐍
Python Fix
Python
from sklearn.preprocessing import StandardScaler; scaler = StandardScaler(); df['transaction_amount_scaled'] = scaler.fit_transform(df[['transaction_amount']])
✗
Common Wrong Diagnoses
▼ Show
Model overfitting due to new feature
Why candidates think this: New feature importance is high, suggesting overfitting
Why wrong: Overfitting would typically affect both accuracy and recall, not just recall
Data leakage in validation set
Why candidates think this: Stable AUC suggests validation issues
Why wrong: Data leakage would affect all metrics, not just recall
📊
Monitoring to Add
  • Monitor recall on validation set continuously
  • Check feature distribution skewness post-scaling
  • Alert on gradient explosion warnings during training
📋
Process Change

Include feature scaling verification in the model deployment checklist

🏗️
Architectural Change

Integrate a feature scaling validation step in the data preprocessing pipeline

💡
Key Insight
Feature scaling method can drastically affect model performance, especially recall, when features are improperly normalized.

Practice

(1/5)
1. What is the output of the following Python code snippet using NumPy for logistic regression log-odds calculation?
import numpy as np
coefficients = np.array([0.5, -0.25])
features = np.array([2, 3])
log_odds = np.dot(coefficients, features)
print(log_odds)
easy
A. -0.25
B. 0.25
C. 1.25
D. 2.75

Solution

  1. Step 1: Calculate the dot product.

    The dot product is calculated as (0.5 * 2) + (-0.25 * 3) = 1.0 - 0.75 = 0.25.
  2. Step 2: Identify the correct output.

    -0.25 is correct because the calculation results in 0.25. 1.25 is incorrect as it suggests a miscalculation. 0.25 is incorrect due to a sign error. 2.75 is incorrect due to a misunderstanding of dot product.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Dot product = sum of products [OK]
Hint: Dot product = sum of products [OK]
Common Mistakes:
2. In the context of model debugging, which of the following is a common symptom of data leakage?
easy
A. High training error but low validation error
B. Low training error but high validation error
C. High training and validation error
D. Low training and validation error

Solution

  1. Step 1: Identify data leakage symptoms

    Data leakage often results in a model that performs well on both training and validation sets because it has inadvertently learned from the validation data.
  2. Step 2: Eliminate wrong options

    Options A, B, and C indicate poor generalization or overfitting, not data leakage.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Data leakage = too good to be true [OK]
Hint: Data leakage = too good to be true [OK]
Common Mistakes:
3. In the context of Transformer architecture, what is the primary purpose of self-attention?
easy
A. To capture dependencies between words regardless of their distance in the sequence
B. To reduce the dimensionality of input data
C. To encode positional information of words in a sequence
D. To improve the model's ability to generalize to unseen data

Solution

  1. Step 1: Understand self-attention's role

    Self-attention allows the model to weigh the importance of different words in a sequence, capturing dependencies regardless of their position.
  2. Step 2: Eliminate wrong options

    To reduce the dimensionality of input data is incorrect because self-attention does not reduce dimensionality. To encode positional information of words in a sequence is incorrect as positional encoding handles positional information. To improve the model's ability to generalize to unseen data is incorrect because self-attention focuses on sequence dependencies, not generalization.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Self-attention captures word dependencies. [OK]
Hint: Self-attention captures word dependencies. [OK]
Common Mistakes:
4.
import numpy as np
from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(1, input_dim=2, activation='relu'))
model.add(Dense(1, activation='relu'))

weights = model.get_weights()
print([w.shape for w in weights])
What does this code output?
easy
A. [(2, 1), (1,), (1, 1), (1,)]
B. [(2, 1), (1,), (1, 2), (1,)]
C. [(1, 2), (1,), (1, 1), (1,)]
D. [(2, 1), (2,), (1, 1), (1,)]

Solution

  1. Step 1: Analyze the network structure

    The first layer has 2 inputs and 1 output, so weights are (2, 1) and biases are (1,). The second layer has 1 input and 1 output, so weights are (1, 1) and biases are (1,).
  2. Step 2: Eliminate wrong options

    [(2, 1), (1,), (1, 2), (1,)] is incorrect because the second layer's weights should be (1, 1). [(1, 2), (1,), (1, 1), (1,)] is incorrect because the first layer's weights should be (2, 1). [(2, 1), (2,), (1, 1), (1,)] is incorrect because the first layer's biases should be (1,).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Check layer dimensions match input/output. [OK]
Hint: Check layer dimensions match input/output. [OK]
Common Mistakes:
5. When monitoring an ML model in production, which approach is better for detecting data drift and why?
medium
A. Monitor statistical properties of input features.
B. Use a static threshold on model accuracy.
C. Rely on user feedback to identify issues.
D. Increase the frequency of model retraining.

Solution

  1. Step 1: Identify effective drift detection

    Monitoring statistical properties helps detect subtle changes in input data.
  2. Step 2: Eliminate wrong options

    Use a static threshold on model accuracy. is reactive, not proactive. Rely on user feedback to identify issues. is unreliable and subjective. Increase the frequency of model retraining. addresses performance decay, not drift detection.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Monitor input stats for drift detection [OK]
Hint: Monitor input stats for drift detection [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