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?
Think about how each scaler treats extreme values or outliers.
StandardScaler subtracts the mean and divides by standard deviation, which reduces the effect of outliers. MinMaxScaler rescales data to a fixed range (0 to 1), which can be heavily influenced by outliers.
What is the output of the following code?
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())
MinMaxScaler rescales values to the range 0 to 1 based on min and max values.
The minimum value 1 maps to 0, the maximum 5 maps to 1, and values in between are scaled linearly.
After applying StandardScaler to a feature column, what will be the approximate mean and variance of the transformed data?
StandardScaler standardizes data by removing the mean and scaling to unit variance.
StandardScaler subtracts the mean (making it zero) and divides by the standard deviation (making variance one).
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?
Check the shape of the input array expected by sklearn scalers.
Scikit-learn scalers expect 2D arrays (samples, features). Passing a 1D array causes a ValueError.
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?
Think about common practices for image data preprocessing in neural networks.
MinMaxScaler scaling pixels to [0,1] is standard for image data, helping neural networks train faster and more stable. StandardScaler is less common for raw pixel values.
