mysterious world 158

[데이터 전처리] 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이나 다른..

[Python] Pandas DataFrame 문자열 포함 확인 str.contains(), 시작 문자열 확인 str.startswith(), 문자열 변경 str.replace()

str.contains() : 데이터프레임의 해당 컬럼에 어떤 문자열을 포함하고 있는지 True, False로 알려준다. 문자열 뒤에 아무것도 입력하지 않으면 디폴트값 case=True이 적용 case=False값을 입력하면 대소문자 상관없이 가져온다. str.startswith() : 데이터프레임의 해당 컬럼에 특정 문자열로 시작하는지 True, False로 알려준다. str.replace() : 문자열 a를 b로 변경해준다. 특정 문자열 제거할때도 사용가능

Python/Pandas 2024.04.11