Bird
Raised Fist0
ML Pythonml~20 mins

Feature scaling (StandardScaler, MinMaxScaler) in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Feature Scaling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use StandardScaler instead of MinMaxScaler?

Imagine you have a dataset with features measured in very different units, like height in centimeters and income in dollars. You want to prepare the data for a machine learning model.

Why might you choose StandardScaler over MinMaxScaler?

AStandardScaler centers data to mean 0 and scales to unit variance, which handles outliers better than MinMaxScaler.
BStandardScaler always scales data between 0 and 1, which is better for all models.
CMinMaxScaler removes outliers automatically, so StandardScaler is better for noisy data.
DMinMaxScaler centers data to mean 0 and scales to unit variance, so StandardScaler is not needed.
Attempts:
2 left
💡 Hint

Think about how each scaler treats extreme values or outliers.

❓ Predict Output
intermediate
1:30remaining
Output of scaling with MinMaxScaler

What is the output of the following code?

ML Python
from sklearn.preprocessing import MinMaxScaler
import numpy as np

X = np.array([[1], [2], [3], [4], [5]])
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
print(X_scaled.flatten())
A[0. 0.25 0.5 0.75 1. ]
B[1. 2. 3. 4. 5.]
C[-1. -0.5 0. 0.5 1.]
D[0.2 0.4 0.6 0.8 1. ]
Attempts:
2 left
💡 Hint

MinMaxScaler rescales values to the range 0 to 1 based on min and max values.

❓ Metrics
advanced
1:30remaining
Effect of StandardScaler on mean and variance

After applying StandardScaler to a feature column, what will be the approximate mean and variance of the transformed data?

AMean close to 1 and variance close to 0
BMean close to 0 and variance close to 1
CMean close to 0 and variance close to 0
DMean close to 1 and variance close to 1
Attempts:
2 left
💡 Hint

StandardScaler standardizes data by removing the mean and scaling to unit variance.

🔧 Debug
advanced
2:00remaining
Why does this scaling code raise an error?

Consider this code snippet:

from sklearn.preprocessing import StandardScaler
import numpy as np

X = np.array([1, 2, 3, 4, 5])
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(X_scaled)

Why does this code raise an error?

Afit_transform method does not exist for StandardScaler, causing AttributeError.
BStandardScaler cannot handle integer data types, causing a TypeError.
CMissing import for StandardScaler causes NameError.
DX is 1D array but fit_transform expects 2D array, causing a ValueError.
Attempts:
2 left
💡 Hint

Check the shape of the input array expected by sklearn scalers.

❓ Model Choice
expert
2:00remaining
Choosing scaler for neural network input

You are training a neural network on image pixel data with values from 0 to 255. Which scaler is the best choice to preprocess the input pixels before training?

ANo scaling needed because neural networks handle raw pixel values well
BStandardScaler to center pixels around 0 with unit variance
CMinMaxScaler to scale pixel values between 0 and 1
DUse a scaler that scales pixels between -1 and 1 by subtracting 128 and dividing by 128
Attempts:
2 left
💡 Hint

Think about common practices for image data preprocessing in neural networks.

Practice

(1/5)
1. What is the main purpose of using StandardScaler in feature scaling?
easy
A. To center data to mean 0 and scale to unit variance
B. To rescale data to a fixed range like 0 to 1
C. To remove missing values from the dataset
D. To convert categorical data into numbers

Solution

  1. Step 1: Understand what StandardScaler does

    StandardScaler subtracts the mean and divides by the standard deviation for each feature.
  2. Step 2: Compare with other options

    MinMaxScaler rescales to a fixed range, not StandardScaler. Removing missing values or encoding categories is unrelated.
  3. Final Answer:

    To center data to mean 0 and scale to unit variance -> Option A
  4. Quick Check:

    StandardScaler = mean 0, variance 1 [OK]
Hint: StandardScaler centers and scales data, MinMaxScaler rescales range [OK]
Common Mistakes:
  • Confusing StandardScaler with MinMaxScaler
  • Thinking StandardScaler changes data range to 0-1
  • Mixing feature scaling with data cleaning
2. Which of the following is the correct way to import and create a MinMaxScaler object in Python using scikit-learn?
easy
A. from sklearn.preprocessing import StandardScaler scaler = MinMaxScaler()
B. import sklearn.MinMaxScaler scaler = MinMaxScaler()
C. from sklearn.scale import MinMaxScaler scaler = MinMaxScaler()
D. from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler()

Solution

  1. Step 1: Recall correct import path

    MinMaxScaler is in sklearn.preprocessing module, so import from there.
  2. Step 2: Check object creation syntax

    Creating scaler with MinMaxScaler() is correct. Other options have wrong module or mismatch.
  3. Final Answer:

    from sklearn.preprocessing import MinMaxScaler\nscaler = MinMaxScaler() -> Option D
  4. Quick Check:

    Correct import and instantiation [OK]
Hint: MinMaxScaler is in sklearn.preprocessing, not sklearn.scale [OK]
Common Mistakes:
  • Using wrong module name like sklearn.scale
  • Importing StandardScaler but creating MinMaxScaler
  • Trying to import MinMaxScaler directly from sklearn
3. Given the data array [[2], [4], [6], [8]], what will be the output after applying MinMaxScaler()?
medium
A. [[0.0], [0.33], [0.67], [1.0]]
B. [[0.0], [0.5], [1.0], [1.5]]
C. [[-1.34], [-0.45], [0.45], [1.34]]
D. [[2], [4], [6], [8]]

Solution

  1. Step 1: Understand MinMaxScaler formula

    MinMaxScaler scales each value x as (x - min) / (max - min). Here min=2, max=8.
  2. Step 2: Calculate scaled values

    For 2: (2-2)/(8-2)=0.0; 4: (4-2)/6=0.33; 6: (6-2)/6=0.67; 8: (8-2)/6=1.0.
  3. Final Answer:

    [[0.0], [0.33], [0.67], [1.0]] -> Option A
  4. Quick Check:

    MinMaxScaler scales between 0 and 1 [OK]
Hint: MinMaxScaler maps min to 0 and max to 1 linearly [OK]
Common Mistakes:
  • Confusing MinMaxScaler with StandardScaler output
  • Not calculating min and max correctly
  • Expecting scaled values outside 0-1 range
4. You applied StandardScaler on a dataset but got an error: ValueError: Input contains NaN. What is the best way to fix this?
medium
A. Use StandardScaler without fitting
B. Remove or fill missing values before scaling
C. Ignore the error and continue
D. Change StandardScaler to MinMaxScaler

Solution

  1. Step 1: Understand cause of error

    StandardScaler cannot handle NaN (missing) values directly, causing ValueError.
  2. Step 2: Fix by preprocessing data

    Remove rows with NaN or fill them with mean/median before scaling to avoid error.
  3. Final Answer:

    Remove or fill missing values before scaling -> Option B
  4. Quick Check:

    Handle NaN before scaling [OK]
Hint: Clean missing data before scaling to avoid errors [OK]
Common Mistakes:
  • Switching scaler without fixing NaN
  • Ignoring error and expecting correct results
  • Trying to scale without fitting the scaler
5. You have two features: height (in cm) ranging from 150 to 190, and income (in thousands) ranging from 20 to 200. You want to use a model sensitive to feature scale. Which scaler should you use and why?
hard
A. Use StandardScaler to center and scale both features to mean 0 and variance 1
B. Use no scaling because features have different units
C. Use MinMaxScaler to rescale both features to 0-1 range
D. Use MinMaxScaler only on income, leave height as is

Solution

  1. Step 1: Understand feature ranges and model sensitivity

    Height and income have different units and ranges; model sensitive to scale needs comparable ranges.
  2. Step 2: Choose scaler based on range preservation

    MinMaxScaler rescales both features to 0-1, preserving relative differences and making scales comparable.
  3. Step 3: Why not StandardScaler?

    StandardScaler centers data but may produce negative values and does not bound range, which can confuse some models.
  4. Final Answer:

    Use MinMaxScaler to rescale both features to 0-1 range -> Option C
  5. Quick Check:

    MinMaxScaler = rescale to fixed range [OK]
Hint: MinMaxScaler makes features comparable by fixed range [OK]
Common Mistakes:
  • Using StandardScaler without considering model needs
  • Not scaling features with different units
  • Scaling only one feature and not the other
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