반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

sklearn.pipeline.Pipeline 본문

[AI]/python.sklearn

sklearn.pipeline.Pipeline

givemebro 2020. 4. 27. 09:39
반응형

 

편의성을 위해 사용

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# data 준비
iris=load_iris()
X_train,X_test,y_train,y_test=train_test_split(iris.data,iris.target)

from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
# *****
from sklearn.pipeline import Pipeline
# *****
pipeline 정의
pipe = Pipeline([('scaler',MinMaxScaler()),('pca',PCA(2)),('model',KNeighborsClassifier())])

# train
pipe.fit(X_train,y_train)

# score 확인
s_train=pipe.score(X_train,y_train)
s_test=pipe.score(X_test,y_test)
display(s_test,s_train)

# 0.8947368421052632
# 0.9732142857142857

 

 

# 중간 과정 확인

pipe.steps

 

'''
[('scaler', MinMaxScaler(copy=True, feature_range=(0, 1))),
 ('pca',
  PCA(copy=True, iterated_power='auto', n_components=2, random_state=None,
    svd_solver='auto', tol=0.0, whiten=False)),
 ('model',
  KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
             metric_params=None, n_jobs=1, n_neighbors=5, p=2,
             weights='uniform'))]
'''
반응형
Comments