0Pricing
Machine Learning Academy · 강의

특성 스케일링: StandardScaler와 MinMaxScaler

훈련 데이터에 StandardScaler와 MinMaxScaler를 적합하고, 테스트 데이터는 별도로 변환하며, 테스트 데이터에 적합하면 누수가 발생하는 이유를 이해합니다.

특성 스케일링: StandardScaler와 MinMaxScaler은(는) CoddyKit의 무료 Machine Learning Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Machine Learning Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Machine Learning Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Why Feature Scaling Matters

Many machine learning algorithms are sensitive to the scale of input features. If one feature spans 0–1000 and another spans 0–1, distance-based models like KNN and SVM will be dominated by the larger-scale feature. Gradient descent algorithms also converge much faster when features share a similar scale. Tree-based models like Random Forests and XGBoost are scale-invariant and do not require scaling, but almost every other algorithm benefits from it.

import numpy as np

# Without scaling: 'income' dominates 'age'
X = np.array([
    [25, 50000],
    [35, 80000],
    [45, 120000]
])

# Distance between row 0 and row 1 (Euclidean)
dist = np.sqrt((25-35)**2 + (50000-80000)**2)
print('Distance dominated by income:', dist)
# age contributes 100, income contributes 900,000,000 to squared distance

StandardScaler: Z-Score Normalisation

StandardScaler transforms each feature to have zero mean and unit variance by applying the formula: z = (x - mean) / std. This is called standardisation or z-score normalisation. The resulting values are centred around 0 with no fixed range. StandardScaler is the default choice for most algorithms including logistic regression, SVMs, and neural networks, especially when the feature distribution is approximately Gaussian.

from sklearn.preprocessing import StandardScaler
import numpy as np

X = np.array([[25, 50000], [35, 80000], [45, 120000]], dtype=float)

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

print('Scaled data:\n', X_scaled)
print('Mean per feature:', X_scaled.mean(axis=0))  # ~[0, 0]
print('Std per feature:', X_scaled.std(axis=0))    # ~[1, 1]

How StandardScaler Stores Statistics

After calling fit(), StandardScaler stores the training-set statistics in scaler.mean_ and scaler.scale_ (standard deviation). These same statistics are used when you call transform() on new data — meaning the test set is scaled using training set parameters, not its own. This is the correct behaviour: in production, you receive individual samples one at a time and must scale them using the statistics computed during training.

from sklearn.preprocessing import StandardScaler
import numpy as np

X_train = np.array([[1, 200], [2, 400], [3, 600]], dtype=float)
X_test  = np.array([[4, 800]], dtype=float)

scaler = StandardScaler()
scaler.fit(X_train)  # Learn mean and std from training data ONLY

print('Learned mean:', scaler.mean_)
print('Learned scale:', scaler.scale_)

X_train_s = scaler.transform(X_train)
X_test_s  = scaler.transform(X_test)  # Uses SAME training statistics
print('Test scaled:', X_test_s)

MinMaxScaler: Rescaling to a Fixed Range

MinMaxScaler compresses each feature to a specified range, typically [0, 1], using the formula: x_scaled = (x - x_min) / (x_max - x_min). Unlike StandardScaler, it preserves the relative shape of the distribution and handles non-Gaussian data well. It is sensitive to outliers: a single extreme value can squish all other values near zero or one. MinMaxScaler is popular for neural networks and image pixel values (scaled to [0, 1]).

from sklearn.preprocessing import MinMaxScaler
import numpy as np

X = np.array([[10], [20], [30], [40], [50]], dtype=float)

scaler = MinMaxScaler(feature_range=(0, 1))
X_scaled = scaler.fit_transform(X)

print('MinMax scaled:', X_scaled.flatten())
# [0.0, 0.25, 0.5, 0.75, 1.0]

# Custom range: scale to [-1, 1]
scaler2 = MinMaxScaler(feature_range=(-1, 1))
print('[-1,1] scaled:', scaler2.fit_transform(X).flatten())

RobustScaler: Handling Outliers

RobustScaler scales using the median and interquartile range (IQR) instead of mean and standard deviation. Because median and IQR are not influenced by extreme values, RobustScaler is a better choice when your data contains significant outliers. The formula is: x_scaled = (x - median) / IQR. Use RobustScaler when you cannot remove outliers and want them to have minimal influence on the scaled values passed to your model.

from sklearn.preprocessing import RobustScaler
import numpy as np

# Data with outlier at 1000
X = np.array([[1], [2], [3], [4], [1000]], dtype=float)

from sklearn.preprocessing import StandardScaler, RobustScaler

std_s = StandardScaler().fit_transform(X)
rob_s = RobustScaler().fit_transform(X)

print('StandardScaler (outlier squishes others):')
print(std_s.flatten())
print('RobustScaler (outlier isolated):')
print(rob_s.flatten())

Choosing the Right Scaler

Here is a quick decision guide: use StandardScaler when your data is roughly Gaussian and you have no extreme outliers — it is the safest default. Use MinMaxScaler when you need values in a fixed range, such as for image data or algorithms that require [0, 1] inputs. Use RobustScaler when outliers are present and meaningful (e.g., financial data with fraud spikes). For tree-based models (Random Forest, XGBoost, LightGBM), skip scaling entirely — it offers no benefit and adds unnecessary complexity.

from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler

# Decision helper
def pick_scaler(has_outliers, needs_fixed_range, is_tree_model):
    if is_tree_model:
        return 'No scaling needed'
    elif has_outliers:
        return 'RobustScaler'
    elif needs_fixed_range:
        return 'MinMaxScaler'
    else:
        return 'StandardScaler'

print(pick_scaler(False, False, False))  # StandardScaler
print(pick_scaler(True, False, False))   # RobustScaler
print(pick_scaler(False, True, False))   # MinMaxScaler
print(pick_scaler(False, False, True))   # No scaling needed

Fitting on Test Data: The Leakage Trap

A common mistake is calling fit_transform() on the test set, which computes a new mean and standard deviation from test data. This is a form of data leakage because the scaler is influenced by test-set values. The correct pattern is: fit(X_train) → transform(X_train) → transform(X_test). Even a small accuracy improvement from this leak can lead to overconfident production deployments that fail on truly unseen data.

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import numpy as np

X = np.random.randn(100, 3)
y = (X[:, 0] > 0).astype(int)

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

scaler = StandardScaler()

# CORRECT: fit on train, transform both
scaler.fit(X_train)
X_train_s = scaler.transform(X_train)
X_test_s  = scaler.transform(X_test)

# WRONG (leakage):
# scaler.fit_transform(X_test)  <- never do this

Scaling Inside a Pipeline

The cleanest way to avoid scaling leakage is to embed the scaler inside a scikit-learn Pipeline. When the pipeline is fitted with cross_val_score or GridSearchCV, each fold fits the scaler only on the training portion of that fold. The test fold is never touched during fitting. This is the production-ready pattern: preprocessing and modelling steps are bundled together, making deployment as simple as calling pipeline.predict(X_new).

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)

pipe = Pipeline([
    ('scaler', StandardScaler()),
    ('clf', LogisticRegression(max_iter=200))
])

# Scaler fitted only on train fold in each CV iteration
scores = cross_val_score(pipe, X, y, cv=5)
print('CV accuracy:', scores.mean().round(3))

Inverse Transform: Back to Original Scale

After making predictions, you may need to convert predictions back to the original scale — for example, a house-price model trained on log-scaled prices must output prices in dollars. Both StandardScaler and MinMaxScaler provide inverse_transform() to reverse the scaling. Only inverse-transform the target variable when it was scaled before training. Features do not typically need to be inverse-transformed because they are consumed internally by the model.

from sklearn.preprocessing import StandardScaler
import numpy as np

y_train = np.array([100000, 200000, 300000, 400000], dtype=float).reshape(-1, 1)

target_scaler = StandardScaler()
y_scaled = target_scaler.fit_transform(y_train)
print('Scaled target:', y_scaled.flatten())

# After predicting in scaled space:
y_pred_scaled = np.array([[0.5]])
y_pred_original = target_scaler.inverse_transform(y_pred_scaled)
print('Prediction in original scale:', y_pred_original)

Scaling Sparse Data with MaxAbsScaler

Sparse matrices — common in text classification with TF-IDF vectors — should not be scaled with StandardScaler or MinMaxScaler because centering destroys sparsity by making most zeros non-zero, which defeats the purpose of sparse storage. MaxAbsScaler scales each feature by its maximum absolute value, mapping values to the range [-1, 1] without centering, and preserves the sparsity structure. Always use MaxAbsScaler when working with sparse feature matrices from vectorisers like TfidfVectorizer.

from sklearn.preprocessing import MaxAbsScaler
from scipy.sparse import csr_matrix
import numpy as np

# Simulated sparse TF-IDF matrix
X_sparse = csr_matrix(np.array([
    [0, 0.5, 0.3, 0],
    [0.8, 0, 0, 0.4],
    [0, 0.2, 0, 0.6]
]))

scaler = MaxAbsScaler()
X_scaled = scaler.fit_transform(X_sparse)
print('Scaled sparse matrix:\n', X_scaled.toarray())
# All values in [-1, 1], sparsity preserved

Comparing Scalers on Real Data

To see the practical effect of each scaler, compare their output distributions side by side. After scaling, StandardScaler output should be approximately N(0,1), MinMaxScaler output should span [0,1], and RobustScaler output should have median at 0 with IQR of 1. Plotting histograms before and after scaling confirms the transformation worked correctly. Good feature scaling is a prerequisite for reliable model training, not an optional add-on.

import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
import numpy as np

X = np.random.exponential(scale=100, size=(200, 1))  # Skewed data

scalers = {
    'StandardScaler': StandardScaler(),
    'MinMaxScaler': MinMaxScaler(),
    'RobustScaler': RobustScaler()
}

fig, axes = plt.subplots(1, 4, figsize=(16, 4))
axes[0].hist(X, bins=30); axes[0].set_title('Original')
for ax, (name, sc) in zip(axes[1:], scalers.items()):
    ax.hist(sc.fit_transform(X), bins=30)
    ax.set_title(name)
plt.tight_layout()
plt.show()

Quick Check

Test your understanding of Machine Learning with Python concepts from this lesson.

Lesson Recap

In this lesson you learned: why feature scaling is essential for distance-based and gradient-descent algorithms, how StandardScaler standardises to N(0,1) while MinMaxScaler compresses to [0,1], and the critical rule of fitting scalers only on training data to prevent leakage. Next up we explore encoding categorical variables with OrdinalEncoder and OneHotEncoder.

자주 묻는 질문

“특성 스케일링: StandardScaler와 MinMaxScaler” 강의는 무료인가요?

네 — “특성 스케일링: StandardScaler와 MinMaxScaler” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Machine Learning Academy 강의 전체를 잠금 해제할 수 있습니다. Machine Learning Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“특성 스케일링: StandardScaler와 MinMaxScaler”에서 뭘 배우나요?

훈련 데이터에 StandardScaler와 MinMaxScaler를 적합하고, 테스트 데이터는 별도로 변환하며, 테스트 데이터에 적합하면 누수가 발생하는 이유를 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Machine Learning Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Machine Learning Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Machine Learning Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“특성 스케일링: StandardScaler와 MinMaxScaler” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Machine Learning Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Machine Learning Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 결측값 처리: 삭제, 대치, 표시
  2. 특성 스케일링: StandardScaler와 MinMaxScaler
  3. 범주형 변수 인코딩: OrdinalEncoder와 OneHotEncoder
  4. ColumnTransformer로 단계 결합
← Machine Learning Academy(으)로 돌아가기