使用SKlearn的Pipline类,拟合出包含任意多个处理步骤的模型,并将模型用于新的数据预测。
流水线包含数据预处理还有评估器。
代码有两个预处理环节,用于数据缩放和转换的StandardScaler和PCA。
评估器为LogisticRegression分类器。
工作流:StandardScaler—>PCA—>LogisticRegression
流程图:
实现工作流:
# 使用pandas从UCI读取乳腺癌数据集
import pandas as pd
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data', header=None)
df.head()
df.shape
# 将数据集的30个特征赋给一个NumPy的数组对象X
# 使用SKlearn的LabelEncoder类将类标从原始字符串表示转换为整数
from sklearn.preprocessing import LabelEncoder
X = df.loc[:, 2:].values
y = df.loc[:, 1].values
le = LabelEncoder()
y = le.fit_transform(y)
le.transform(['M', 'B'])
# 将数据将划分80/20
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = \
train_test_split(X, y, test_size=0.20, random_state=1)
# 通过流水线将StandardScaler,PCA,LogisticRegression串联起来
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
pipe_lr = Pipeline([('scl', StandardScaler()),
('pca', PCA(n_components=2)),
('clf', LogisticRegression(random_state=1))])
pipe_lr.fit(X_train, y_train)
print('Test Accuracy: %.3f' % pipe_lr.score(X_test, y_test))
y_pred = pipe_lr.predict(X_test)
输出: