[AI] Pipeline : Scikit-Learn

Ik·2022년 7월 17일
0

Data

목록 보기
7/34

Pipeline

Pipeline이란

  • 여러 단계의 머신러닝 프로세스 (전처리의 각 단계, 모델생성, 학습) 처리 과정을 설정하여 한번에 처리되도록 한다
    • 데이터의 흐름이 중요
  • 종류
    • 전처리작업 파이프라인
      • Transformer
    • 전체 프로세스 파이프라인
      • Transformer + Estimator
from sklearn.pipeline import Pipeline
# 데이터 전처리, 모델 선택
order = [
    ('scaler', StandardScaler()),  # 단순히 객체만 넣는 것이 아닌 이름도 함께 준다
    ('svm',SVC())
]
pipeline = Pipeline(order, verbose=True)  # True이면 학습에서 현재 일하고 있는 것이 무엇인지 보여줌
# 학습
pipeline.fit(Feature, Label)
# 추정, scaler를 지나 svm 모델로 추정하는 과정 한번에 
pred_train = pipeline.predict(X_train)
pred_test = pipeline.predict(X_test)

Pipeline - GridSearch

order = [
    ('scaler', StandardScaler()),  
    ('svc', SVC(random_state=0))
]
pipeline = Pipeline(order)
# -----------------------------------여기까지는 위에 설명과 동일
# svc model의 hyper parameter C,gamma
param = {
    "svc__C":[0.001, 0.01, 0.1, 1, 10],   # 프로세스이름__하이퍼 파라미터 
    "svc__gamma":[0.001, 0.01, 0.1, 1, 10]  # Pipeline 안에 이름이 scv인 모델의 gamma 하이퍼 파라미터 후보
}
gs = GridSearchCV(pipeline,   # model에 pipeline 입력
                  param, 
                  scoring='accuracy', 
                  cv=4, 
                  n_jobs=-1)
# 학습
gs.fit(Feature, Label)

pipeline - make_pipeline()

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import MinMaxScaler
pipeline2 = make_pipeline(MinMaxScaler(), SVC(C=100))
# 모델 학습 및 새로운 데이터로 검증
pipeline2.fit(Feature, Label).score(New_Feature, New_Label)

0개의 댓글