- 1. pd 데이터 가져오기
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import the dataset
>>> df = pd.read_csv('../data/Data.csv')
>>> df
Country Age Salary Purchased
0 France 44.0 72000.0 No
1 Spain 27.0 48000.0 Yes
2 Germany 30.0 54000.0 No
3 Spain 38.0 61000.0 No
4 Germany 40.0 NaN Yes
5 France 35.0 58000.0 Yes
6 Spain NaN 52000.0 No
7 France 48.0 79000.0 Yes
8 Germany 50.0 83000.0 No
9 France 37.0 67000.0 Yes
- 2. 0이나 다른 데이터로 채워져 있는 값 nan으로 변환
- 3. 비어있는 데이터 확인(있으면 삭제 또는 변경)
>>> df.isna().sum()
Country 0
Age 1
Salary 1
Purchased 0
dtype: int64
# 1. 삭제 전략
>>> df.dropna()
Country Age Salary Purchased
0 France 44.0 72000.0 No
1 Spain 27.0 48000.0 Yes
2 Germany 30.0 54000.0 No
3 Spain 38.0 61000.0 No
5 France 35.0 58000.0 Yes
7 France 48.0 79000.0 Yes
8 Germany 50.0 83000.0 No
9 France 37.0 67000.0 Yes
# 2. 채우는 전략
>>> df.fillna(df.mean(numeric_only=True))
Country Age Salary Purchased
0 France 44.000000 72000.000000 No
1 Spain 27.000000 48000.000000 Yes
2 Germany 30.000000 54000.000000 No
3 Spain 38.000000 61000.000000 No
4 Germany 40.000000 63777.777778 Yes
5 France 35.000000 58000.000000 Yes
6 Spain 38.777778 52000.000000 No
7 France 48.000000 79000.000000 Yes
8 Germany 50.000000 83000.000000 No
9 France 37.000000 67000.000000 Yes
- 4. X, Y 데이터 분리 : 즉 학습할 변수와 레이블링 변수로 분리
>>> y = df['Purchased']
>>> y
0 No
1 Yes
2 No
3 No
5 Yes
7 Yes
8 No
9 Yes
Name: Purchased, dtype: object
>>> X = df.loc[:,'Country':'Salary']
>>> X
Country Age Salary
0 France 44.0 72000.0
1 Spain 27.0 48000.0
2 Germany 30.0 54000.0
3 Spain 38.0 61000.0
5 France 35.0 58000.0
7 France 48.0 79000.0
8 Germany 50.0 83000.0
9 France 37.0 67000.0
- 5. 문자열 데이터가 있을경우 레이블 인코딩(또는 원핫)으로 문자열 데이터를 숫자로 변경
# Lable encoding 레이블 인코딩
# 'France' => 0
# 'Germany' => 1
# 'Spain' => 2
# One Hot encoding 원 핫 인코딩
# France, Germany, Spain Age Salary
# 1 0 0 44 72000
# 0 0 1 27 48000
# 0 1 0
# 0 0 1
# 문자열을 숫자로 바꾸는 작업
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
# 1. 레이블 인코딩 하는 방법
>>> encoder = LabelEncoder()
>>> X['Country'] = encoder.fit_transform(X['Country'])
>>> X
Country Age Salary
0 0 44.0 72000.0
1 2 27.0 48000.0
2 1 30.0 54000.0
3 2 38.0 61000.0
5 0 35.0 58000.0
7 0 48.0 79000.0
8 1 50.0 83000.0
9 0 37.0 67000.0
# 2. 원 핫 인코딩 하는 방법
ct = ColumnTransformer([('encoder', OneHotEncoder(), [0])], remainder='passthrough')
# [0] 이라고 쓴 이유?
# X 에 들어있는 원 핫 인코딩 하고 싶은 컬럼의 인덱스 => [0]
# 예를 들어 원 핫 인코딩 하고 싶은 컬럼이 첫 번째와 세 번째 컬럼일 경우 => [0, 2]
>>> X = df.loc[:,'Country':'Salary']
>>> X
Country Age Salary
0 France 44.0 72000.0
1 Spain 27.0 48000.0
2 Germany 30.0 54000.0
3 Spain 38.0 61000.0
5 France 35.0 58000.0
7 France 48.0 79000.0
8 Germany 50.0 83000.0
9 France 37.0 67000.0
>>> X = ct.fit_transform(X)
>>> 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]])
>>> y
0 No
1 Yes
2 No
3 No
5 Yes
7 Yes
8 No
9 Yes
Name: Purchased, dtype: object
>>> y = encoder.fit_transform(y)
>>> 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]])
>>> y
array([0, 1, 0, 0, 1, 1, 0, 1])
- 6. 예측할 값 데이터 갯수 파악
- 7. 데이터 범위 맞춰주기(피처 스케일링)
- 8. 학습용과 테스트용 데이터 나누기
from sklearn.preprocessing import StandardScaler, MinMaxScaler
# 피처 스케일링을 위한 스케일러는, 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])
'인공지능 > 데이터 전처리' 카테고리의 다른 글
[데이터 전처리] 데이터 균형 맞추기 SMOTE() (0) | 2024.04.15 |
---|---|
[데이터 전처리] Training/Test용 데이터 분리 train_test_split() (0) | 2024.04.12 |
[데이터 전처리] 데이터 정규화, 표준화 Feature Scaling, StandardScaler(), MinMaxScaler() (0) | 2024.04.12 |
[데이터 전처리] 레이블 인코딩, 원 핫 인코딩 LabelEncoder(), OneHotEncoder() (0) | 2024.04.12 |