[ML / sklearn] StandardScaler

clean·2023년 9월 30일

StandardScaler

sklearn의 StandardScaler 클래스는 모든 값을 평균이 0, 분산이 1인 정규 분포를 따르도록 scaling한다.
즉, 원래 값이 x이면 이렇게 스케일링된다.

StandardScaler는 outlier에 민감하다.

from sklearn.preprocessing import StandardScaler

sklearn.preprocessing에 구현되어있다.

std_scaler = StandardScaler()
std_scaler.fit(X_train)

X_train_tensor = torch.from_numpy(std_scaler.transform(X_train)).float()
X_test_tensor = torch.from_numpy(std_scaler.transform(X_test)).float()
y_train_tensor = torch.from_numpy(y_train).float()

클래스 안에 여러 메소드가 구현되어 있지만, 여기서는 fit, transform 두 개만 알아보겠다.

fit 메소드

fit(X, y=None, sample_weight=None)

X는 array like

fit 메소드는 나중에 스케일링을 하기 위해서 X의 std와 mean을 계산한다.

transform 메소드

transform(X, copy=None)

standardization을 하는 함수.

출처: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html#sklearn.preprocessing.StandardScaler

profile
블로그 이전하려고 합니다! 👉 https://onfonf.tistory.com 🍀

0개의 댓글