인공지능/데이터 전처리 5

[데이터 전처리] 데이터 균형 맞추기 SMOTE()

>>> X.shape (392, 8) >>> y.value_counts() # 데이터 불균형 class 0 262 1 130 Name: count, dtype: int64 SMOTE() 사용, up sampling 기법으로 불균형한 데이터의 균형을 맞춰준다. # ! pip install imblearn from imblearn.over_sampling import SMOTE >>> sm = SMOTE(random_state=5) >>> X, y = sm.fit_resample(X, y) >>> X.shape (524, 8) >>> y.shape (524,) >>> y.value_counts() # 데이터 불균형 문제 해결! class 0 262 1 262 Name: count, dtype: int64

[데이터 전처리] Training/Test용 데이터 분리 train_test_split()

from sklearn.model_selection import train_test_split train_test_split() : 데이터셋을 학습용과 테스트용으로 나눈다. >>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=32) >>> X_train 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.478260..

[데이터 전처리] 데이터 정규화, 표준화 Feature Scaling, StandardScaler(), MinMaxScaler()

학습 시킬 때 값의 레인지를 맞춰줘야 더 정확한 트레이닝이 가능하다. from sklearn.preprocessing import StandardScaler, MinMaxScaler # 피처 스케일링을 위한 스케일러는, x 용과 y 용 따로 만든다. StandardScaler() : 표준화 # 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.12302..

[데이터 전처리] 레이블 인코딩, 원 핫 인코딩 LabelEncoder(), OneHotEncoder()

데이터를 학습하기 위해서는 방정식에 대입되어야 하는데 방정식은 수학식이므로 데이터는 모두 숫자로 되어 있어야 한다. 따라서 문자열 데이터를 숫자로 바꿔줘야 한다. import numpy as np import matplotlib.pyplot as plt import pandas as pd from sklearn.preprocessing import LabelEncoder, OneHotEncoder from sklearn.compose import ColumnTransformer LabelEncoder() : 문자열 데이터를 정렬해서 순서대로 0부터 시작하는 숫자로 변경 - 카테고리컬 데이터이 3개 이상일 때는 Label Encoding으로 학습하면 학습이 잘 안된다. 3개 이상의 카테고리컬 데이터는 On..

[데이터 전처리] 인공지능 만들기 전 데이터 전처리

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 CountryAgeSalaryPurchased 0France44.072000.0No 1Spain27.048000.0Yes 2Germany30.054000.0No 3Spain38.061000.0No 4Germany40.0NaNYes 5France35.058000.0Yes 6SpainNaN52000.0No 7France48.079000.0Yes 8Germany50.083000.0No 9France37.067000.0Yes 2. 0이나 다른..