Pipelining Transformer
pipeline类本身具有fit、predict和score方法,其行为与Scikit-Learn中的其他模型相同
Pipeline是使用 (key,value) 对的list构建的,key是自己为这一step设定的名称,value是对应的处理类, 最后通过list将这些step传入
创建pipeline
from sklearn.pipeline import Pipeline from sklearn.svm import SVC from sklearn.decomposition import PCA pipe = Pipeline(steps=[('pca', PCA()), ('svc', SVC())]) #pipe是<class 'sklearn.pipeline.Pipeline'> print(pipe)
或写作
from sklearn.pipeline import Pipeline from sklearn.svm import SVC from sklearn.decomposition import PCA estimators = [('reduce_dim', PCA()), ('clf', SVC())] pipe = Pipeline(estimators)
或者用make_pipeline
from sklearn.pipeline import make_pipeline pipe = make_pipeline(MinMaxScaler(),SVC())
就不用我们指定名称了,函数会自动命名
一般来说,自动命名的步骤名称是类名称的小写版本,如果多个步骤属于同一个类,则会附加一个数字。
pipeline.steps
from sklearn.pipeline import Pipeline from sklearn.svm import SVC from sklearn.decomposition import PCA pipe = Pipeline(steps=[('pca', PCA()), ('svc', SVC())]) #pipe是<class 'sklearn.pipeline.Pipeline'> print(pipe.steps)
通过set_params重新设置每个类里边需传入的参数
设置方法为step的name__parma名=参数值
from sklearn.pipeline import Pipeline from sklearn.svm import SVC from sklearn.decomposition import PCA pipe = Pipeline(steps=[('pca', PCA()), ('svc', SVC())]) #pipe是<class 'sklearn.pipeline.Pipeline'> pipe.set_params(svc__C=10.0) print(pipe.steps)
创建管道并训练
import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn.linear_model import LogisticRegression from sklearn.pipeline import make_pipeline df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data', header=None) X = df.loc[:, 2:].values y = df.loc[:, 1].values le = LabelEncoder() y = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=1) pipe_lr = make_pipeline(StandardScaler(), PCA(n_components=2), LogisticRegression(random_state=1, solver='lbfgs')) pipe_lr.fit(X_train, y_train) print('Test Accuracy: %.3f' % pipe_lr.score(X_test, y_test))
在网格搜索中使用管道
import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from sklearn.pipeline import Pipeline from sklearn.model_selection import GridSearchCV df = pd.read_csv('dataset/wdbc.data',header=None) y = df.loc[:,1].values X = df.loc[:,2:].values le = LabelEncoder() y = le.fit_transform(y) #因为得到的y是'B'和'E',字符,需要编码成数字 X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2, random_state=1,stratify=y) pipe = Pipeline([('scaler',StandardScaler()), ('svm',SVC())]) param_grid = {'svm__C':[0.001,0.01,0.1,1,10,100], 'svm__gamma':[0.001,0.01,0.1,1,10,100]} gs = GridSearchCV(pipe, param_grid=param_grid, cv=5) gs.fit(X_train, y_train) print('Accuracy: %.3f' % gs.score(X_test, y_test))
Stratified K-Fold Cross-Validation
StratifiedKFold()
只是给你划分k份,并不做validation的计算
import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn.linear_model import LogisticRegression from sklearn.pipeline import make_pipeline import numpy as np from sklearn.model_selection import StratifiedKFold df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data', header=None) X = df.loc[:, 2:].values y = df.loc[:, 1].values le = LabelEncoder() y = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=1) pipe_lr = make_pipeline(StandardScaler(), PCA(n_components=2), LogisticRegression(random_state=1, solver='lbfgs')) kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True).split(X_train, y_train) scores = [] for k, (train, test) in enumerate(kfold): pipe_lr.fit(X_train[train], y_train[train]) score = pipe_lr.score(X_train[test], y_train[test]) scores.append(score) print('Fold: %2d, Class dist.: %s, Acc: %.3f' % (k+1, np.bincount(y_train[train]), score)) print('\nCV accuracy: %.3f +/- %.3f' % (np.mean(scores), np.std(scores)))
cross_val_score
内置k-fold cross-validation scorer , 写法更简单
import pandas as pd import numpy as np from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn.linear_model import LogisticRegression from sklearn.pipeline import make_pipeline from sklearn.model_selection import cross_val_score df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data', header=None) X = df.loc[:, 2:].values y = df.loc[:, 1].values le = LabelEncoder() y = le.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=1) pipe_lr = make_pipeline(StandardScaler(), PCA(n_components=2), LogisticRegression(random_state=1, solver='lbfgs')) scores = cross_val_score(estimator=pipe_lr, X=X_train, y=y_train, cv=10, n_jobs=1) print('CV accuracy scores: %s' % scores) print('CV accuracy: %.3f +/- %.3f' % (np.mean(scores), np.std(scores)))