반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

sklearn.preprocessing.MinMaxScaler, StandardScaler, Normalizer 본문

[AI]/python.sklearn

sklearn.preprocessing.MinMaxScaler, StandardScaler, Normalizer

givemebro 2020. 4. 20. 17:40
반응형
  1. StandardScaler : 각 속성들을 평균이 0, 표준편차가 1이 되도록 조정
  2. MinMaxScaler : 최소값이 0, 최대값이 1이 되도록 비율을 조정
  3. Normalizer : 속성(열)이 아니라 각각의 샘플(행)의 유클리디안 길이가 1이 되도록 조정 (지름이 1인 구 표면에 각각의 샘플을 투영)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

 

X = np.arange(12).reshape(4,3)
X

'''
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
       
'''

 

# 각 속성(열)의 범위를 0~1 로 제한하고 싶을 경우


X_max = X.max(axis=0)
X_min = X.min(axis=0)

X_ms = (X-X_min) / (X_max-X_min)
X_ms

'''
array([[0.        , 0.        , 0.        ],
       [0.33333333, 0.33333333, 0.33333333],
       [0.66666667, 0.66666667, 0.66666667],
       [1.        , 1.        , 1.        ]])
       
'''

 

# 각 속성(열)을 정규분표(평균이 0, 표준편차가 1)로 바꾸고 싶을 경우
X_mean = X.mean(axis=0)
X_std = X.std(axis=0)

X_ss = (X-X_mean) / X_std
X_ss
'''
array([[-1.34164079, -1.34164079, -1.34164079],
       [-0.4472136 , -0.4472136 , -0.4472136 ],
       [ 0.4472136 ,  0.4472136 ,  0.4472136 ],
       [ 1.34164079,  1.34164079,  1.34164079]])
       
       
'''

 

 

# 각 샘플(행)의 길이를 1로 바꾸고 싶을 경우

X_dist = np.sqrt((X**2).sum(axis=1)) # 각 행의 길이를 구한다

X_ns = X / X_dist.reshape(-1,1) # reshape() 로 열 벡터로 변환
X_ns

'''

array([[0.        , 0.4472136 , 0.89442719],
       [0.42426407, 0.56568542, 0.70710678],
       [0.49153915, 0.57346234, 0.65538554],
       [0.5178918 , 0.57543534, 0.63297887]])
       
'''

 

 

c1 = np.random.uniform(0,np.pi*2, size=100)
c2 = np.random.uniform(5,15,size=100)
X = np.c_[c2*np.cos(c1),c2*np.sin(c1)]
plt.scatter(X[:,0], X[:,1])
plt.axis('equal')

 

from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer

 

ms = MinMaxScaler()
ms.fit(X)
ms.transform(X)
#ms.fit_transform(X)

'''
array([[0.        , 0.        , 0.        ],
       [0.33333333, 0.33333333, 0.33333333],
       [0.66666667, 0.66666667, 0.66666667],
       [1.        , 1.        , 1.        ]])
'''

 

ss = StandardScaler()
ss.fit_transform(X)

'''
array([[-1.34164079, -1.34164079, -1.34164079],
       [-0.4472136 , -0.4472136 , -0.4472136 ],
       [ 0.4472136 ,  0.4472136 ,  0.4472136 ],
       [ 1.34164079,  1.34164079,  1.34164079]])
       
'''

 

 

ns = Normalizer()
ns.fit_transform(X)

'''
array([[0.        , 0.4472136 , 0.89442719],
       [0.42426407, 0.56568542, 0.70710678],
       [0.49153915, 0.57346234, 0.65538554],
       [0.5178918 , 0.57543534, 0.63297887]])
'''

 

 

ss.fit(X_train)
ss.transform(X_train)
ss.transform(X_test)

# 학습세트와 테스트세트가 분리하였을 때,
# 학습세트를 가지고 스케일 조정에 대한 기준을 만든 다음 그 기준에 따라 테스트세트를 조정

 

# 회전변환

xs = np.random.uniform(0,2,size=100)
ys = xs + np.random.normal(0, 0.1, size=100)

plt.scatter(xs, ys)

xs2 = xs * np.cos(np.pi/4) + ys * np.sin(np.pi/4)
ys2 = -xs * np.sin(np.pi/4) + ys * np.cos(np.pi/4)

plt.scatter(xs2, ys2)
plt.axis('equal')

X = np.c_[xs, ys]

theta = -np.pi/4
tf = [[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]] # 회전변환 행렬
X_rot = np.matmul(X, tf) # 행렬곱

plt.scatter(X_rot[:,0], X_rot[:,1])
plt.axis('equal')

반응형
Comments