【机器学习】基于流水线的工作流

使用SKlearnPipline类,拟合出包含任意多个处理步骤的模型,并将模型用于新的数据预测。


流水线包含数据预处理还有评估器

代码有两个预处理环节,用于数据缩放转换StandardScalerPCA

评估器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)

输出:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值