일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- Flutter
- pytorch
- 피플
- RNN
- 머신러닝
- 앱개발
- map
- CV
- 크롤러
- 파이썬
- mnist
- AI
- 42서울
- 유데미
- filtering
- 모델
- Computer Vision
- 선형회귀
- 자연어처리
- 선형대수학
- 42경산
- 플러터
- 딥러닝
- Regression
- 크롤링
- 지정헌혈
- 데이터분석
- 회귀
- 인공지능
- 코딩애플
Archives
- Today
- Total
David의 개발 이야기!
[Part1, Section 4] Data Preprocessing 데이터 전처리 본문
Udemy Python Machine Learning A-Z
[Part1, Section 4] Data Preprocessing 데이터 전처리
david.kim2028 2022. 9. 7. 19:18반응형
데이터 전처리 과정을 살펴보면,
1. 라이브러리를 불러온다.
Importing the libraries
2. 데이터셋을 불러온다.
Importing the dataset
3. 결측치를 어떻게 처리할 것인가?
Taking care of missing data
4. 범주형 데이터를 처리해준다!
Encoding the categorical data
5. 훈련데이터와 테스트데이터를 나눠준다
Spliting the dataset into the Training set and Test set
6. 표준화, 정규화를 해준다
Feature scaling
이렇게 해주어야한다.
각각에 해당하는 코드를 살펴보자.
1. Importing the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
numpy : 행렬이나, 대규모 다차원의 배열을 쉽게 처리할 수 있도록 지원하는 라이브러리
pandas : 숫자 테이블과 시계열 을 조작하기 위한 데이터 구조 와 연산을 제공하는 라이브러리
matplotlib : 그래프나 시각화를 지원하는 라이브러리
2. Importing the dataset
dataset = pd.read_csv("_____")
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
x 는 독립변수 (영향을 주는 변수)
y 는 종속변수 ( 결과 )
iloc -> locate indexes
values -> 값을 가져오는 함수
3. Taking care of missing data
from sklearn.impute import SimpleImpute
imputer = SimpleImpute(missing_values=np.nan, strategy='mean')
imputer.fit(x[:,1:3])
x[:,1:3] = imputer.transform(x[:,1:3])
fit -> 조정
transform -> 변환
4. Encoding categorical data
Encoding the independant variable
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder',OneHotEncoder,[0])], remainder='passthrough')
x = np.array(ct.fit_transform(x))
# transformer -> 어떤열의 인덱스에 어떤 변환을 적용하고 싶은지 정할 수 있음, remainder -> 어떤 것을 유지시키고 싶을때 지정.
# transformer -> 1. 어떤 변환을 적용할 것인지 2. 어떤 종류의 인코딩을 할것인지 3. 어떤열을 인코딩을 할 것인지 정해줘야함.
# remainder ='passthrough' : transformers에 지정되지 않은 열을 유지시키고 싶을떄.
#fit_transform() 메소드가 한번에 조정과 변환을 해줌, SimpleImputer의 경우 fit(), transform() 각각해줘야했었음.
# fit_transform 은 array 로 반환하지 않음. 따라서 np.array해주어야함
Encoding the dependent variable
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)
5. Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1)
6. Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train[:, 3:] = sc.fit_transform(x_train[:, 3:])
x_test[:, 3:] = sc.transform(x_test[:, 3:])
#피처 스케일링은 반드시 testset 과 trainingset이 나눠진 담에 해야한다. (정규와, 표준화 뜻함)
# 훈련데이터와 테스트데이터가 같으면 안되니까!
반응형
'Udemy Python Machine Learning A-Z' 카테고리의 다른 글
SVR 서포트벡터머신 체험하기! (0) | 2022.09.14 |
---|---|
Polynomial Regression 다항회귀에 대해 알아보자! (0) | 2022.09.12 |
[ 파트2 섹션7 ] Multiple Linear Regression 다중선형회귀에 대해 알아보자! (0) | 2022.09.11 |
[파트2 섹션6] 단순회귀 Simple Regression (0) | 2022.09.11 |
데이터 전처리 fit, fit_transform, transform의 개념 익히기! (0) | 2022.09.09 |
Comments