개발학습일지

[AI] 머신러닝 Feature Scaling(StandardScaler(), MinMaxScaler()) / Dataset을 Training과 Test로 나누는 방법 본문

AI/Machine Learning

[AI] 머신러닝 Feature Scaling(StandardScaler(), MinMaxScaler()) / Dataset을 Training과 Test로 나누는 방법

처카푸 2024. 4. 12. 18:06

원본 데이터 X, y로 분리 ->  X, y의 문자열 데이터 레이블, 원핫 인코딩으로 변경한 상태이다

y
# array([0, 1, 0, 0, 1, 1, 0, 1])
X
>>>
    array([[1.0e+00, 0.0e+00, 0.0e+00, 4.4e+01, 7.2e+04],
           [0.0e+00, 0.0e+00, 1.0e+00, 2.7e+01, 4.8e+04],
           [0.0e+00, 1.0e+00, 0.0e+00, 3.0e+01, 5.4e+04],
           [0.0e+00, 0.0e+00, 1.0e+00, 3.8e+01, 6.1e+04],
           [1.0e+00, 0.0e+00, 0.0e+00, 3.5e+01, 5.8e+04],
           [1.0e+00, 0.0e+00, 0.0e+00, 4.8e+01, 7.9e+04],
           [0.0e+00, 1.0e+00, 0.0e+00, 5.0e+01, 8.3e+04],
           [1.0e+00, 0.0e+00, 0.0e+00, 3.7e+01, 6.7e+04]])

 

Feature Scaling을 하기위한 임폴트

from sklearn.preprocessing import StandardScaler, MinMaxScaler

 

Feature Scaling 을 StandardScaler() 이나 MinMaxScaler() 사용하여 X의 데이터를 바꿔보자

- 피처 스케일링을 위한 스케일러는, X용과 y용 따로 만든다

# 1. 표준화
X_scaler = StandardScaler()
X_scaler.fit_transform( X )
>>>
    array([[ 1.        , -0.57735027, -0.57735027,  0.69985807,  0.58989097],
           [-1.        , -0.57735027,  1.73205081, -1.51364653, -1.50749915],
           [-1.        ,  1.73205081, -0.57735027, -1.12302807, -0.98315162],
           [-1.        , -0.57735027,  1.73205081, -0.08137885, -0.37141284],
           [ 1.        , -0.57735027, -0.57735027, -0.47199731, -0.6335866 ],
           [ 1.        , -0.57735027, -0.57735027,  1.22068269,  1.20162976],
           [-1.        ,  1.73205081, -0.57735027,  1.48109499,  1.55119478],
           [ 1.        , -0.57735027, -0.57735027, -0.211585  ,  0.1529347 ]])

# 2. 정규화
X_scaler = MinMaxScaler()
X = X_scaler.fit_transform(X)
X
>>>
    array([[1.        , 0.        , 0.        , 0.73913043, 0.68571429],
           [0.        , 0.        , 1.        , 0.        , 0.        ],
           [0.        , 1.        , 0.        , 0.13043478, 0.17142857],
           [0.        , 0.        , 1.        , 0.47826087, 0.37142857],
           [1.        , 0.        , 0.        , 0.34782609, 0.28571429],
           [1.        , 0.        , 0.        , 0.91304348, 0.88571429],
           [0.        , 1.        , 0.        , 1.        , 1.        ],
           [1.        , 0.        , 0.        , 0.43478261, 0.54285714]])

y   # -> 할 필요 없다
# array([0, 1, 0, 0, 1, 1, 0, 1])

 

Dataset을 Training과 Test로 나누는 방법

필요한 임폴트

from sklearn.model_selection import train_test_split

- train_test_split( X, y, test_size= *, random_state= * ) 사용하여 만든다

train_test_split( X, y, test_size= 0.2, random_state= 32 )
>>> # 리스트 안에 4개의 넘파이어레이로 나온다
	[array([[1.        , 0.        , 0.        , 0.73913043, 0.68571429],
        [0.        , 1.        , 0.        , 0.13043478, 0.17142857],
        [1.        , 0.        , 0.        , 0.34782609, 0.28571429],
        [1.        , 0.        , 0.        , 0.91304348, 0.88571429],
        [0.        , 0.        , 1.        , 0.47826087, 0.37142857],
        [1.        , 0.        , 0.        , 0.43478261, 0.54285714]]),
 array([[0., 1., 0., 1., 1.],
        [0., 0., 1., 0., 0.]]),
 array([0, 0, 1, 1, 0, 1]),
 array([0, 1])]
 
 # 한개씩 변수명 지정
 X_train, X_test, y_train, y_test = train_test_split( X, y, test_size= 0.2, random_state= 32 )