2,378 바이트 추가됨
, 2021년 9월 2일 (목) 22:06
= 개요 =
pipeline. 데이터 전처리 방법은 어떤 상황에서나 대게 유사한 형태로 이루어진다. 데이터 전처리를 위해 많은 타이핑이 필요한데, 사이킷런에선 이러한 반복 작업들을 간단히 마칠 수 있는 도구들이 준비되어 있다.
사이킷런에서 제공하는 알고리즘을 손쉽게 적용할 수 있지만, 개별화된 모델을 만들려 할 경우엔 적용이 힘들어보인다.
= 사용법 =
=== 준비 ===
{| class="wikitable"
!의도
!설명
!방법
|-
|숫자형데이터 전처리
|(파이프라인을 사용하지 않는 경우.)
결측치 채우기
|<syntaxhighlight lang="python">
from sklearn.impute import SimpleImputer
numerical_transformer = SimpleImputer() # 채우기
</syntaxhighlight>
|-
|범주형 데이터 전처리
|(파이프라인 사용.)
결측치 채우고 원-핫 인코더 적용.
|<syntaxhighlight lang="python">
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='constant')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
</syntaxhighlight>
|-
|
|
|
|}
=== 조합 ===
{| class="wikitable"
!의도
!설명
!방법
|-
|준비한 파이프라인 종합
|ColumnTransformer를 이용해 적용
|<syntaxhighlight lang="python">
from sklearn.compose import ColumnTransformer
preprocessor = ColumnTransformer(
transformers=[
('num', numerical_transformer, numerical_cols),
('cat', categorical_transformer, categorical_cols)
])
</syntaxhighlight>
|-
|모델 준비
|사용할 머신러닝 모델을 준비한다.
|<syntaxhighlight lang="python">
model = 사용할모델()
</syntaxhighlight>
|-
|파이프라인과 모델 조합
|둘을 다시 하나로 종합한다.
|<syntaxhighlight lang="python">
my_pipeline = Pipeline(steps=[('preprocessor', preprocessor),
('model', model)
])
</syntaxhighlight>
|}
=== 적용 ===
{| class="wikitable"
!의도
!설명
!방법
|-
|만든 파이프라인에 데이터를 넣는다.
|처음과 나중의 과정을 다 이어붙일 수 있다.
|<syntaxhighlight lang="python">
my_pipeline.fit(X_train, y_train)
</syntaxhighlight>
|}
[[분류:데이터 전처리]]
[[분류:Scikit-learn]]