sklearn学习笔记3——pipeline

本文介绍sklearn中Pipeline和FeatureUnion的使用方法,包括串行化数据处理流程及并行化特征处理,展示了如何简化机器学习工作流,提高模型训练效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sklearn学习笔记3——pipeline

pipeline为方便数据处理,提供了两种模式:串行化和并行化


1.串行化,通过Pipeline类实现

通过steps参数,设定数据处理流程。格式为('key','value'),key是自己为这一step设定的名称,value是对应的处理类。最后通过list将这些step传入。前n-1个step中的类都必须有transform函数,最后一步可有可无,一般最后一步为模型。pipe继承了最后一个类的所有方法。


 
 
  1. In [ 42]: from sklearn.pipeline import Pipeline
  2. ...: from sklearn.svm import SVC
  3. ...: from sklearn.decomposition import PCA
  4. ...: pipe=Pipeline(steps=[( 'pca',PCA()),( 'svc',SVC())])
  5. ...:
  6. ...: from sklearn.datasets import load_iris
  7. ...: iris=load_iris()
  8. ...: pipe.fit(iris.data,iris.target)
  9. ...:
  10. Out[ 42]:
  11. Pipeline(steps=[( 'pca', PCA(copy= True, iterated_power= 'auto', n_components= None,
  12. random_state= None,
  13. svd_solver= 'auto', tol= 0.0, whiten= False)), ( 'svc', SVC(C= 1.0, cache_size= 200,
  14. class_weight= None, coef0= 0.0,
  15. decision_function_shape= None, degree= 3, gamma= 'auto', kernel= 'rbf',
  16. max_iter= -1, probability= False, random_state= None, shrinking= True,
  17. tol= 0.001, verbose= False))])

训练得到的是一个模型,可直接用来预测,预测时,数据会从step1开始进行转换,避免了模型用来预测的数据还要额外写代码实现。还可通过pipe.score(X,Y)得到这个模型在X训练集上的正确率。



 
 
  1. In [ 46]: pipe.predict(iris.data)
  2. Out[ 46]:
  3. array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6. 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1,
  7. 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  8. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  9. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
  10. In [ 47]: iris.target
  11. Out[ 47]:
  12. array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  14. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  15. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  16. 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  17. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  18. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
make_pipeline函数是Pipeline类的简单实现,只需传入每个step的类实例即可,不需自己命名,自动将类的小写设为该step的名。


 
 
  1. In [ 50]: make_pipeline(StandardScaler(),GaussianNB())
  2. Out[ 50]: Pipeline(steps=[( 'standardscaler', StandardScaler(copy= True, with_mean=
  3. True, with_std= True)), ( 'gaussiannb', GaussianNB(priors= None))])
  4. In [ 51]: p=make_pipeline(StandardScaler(),GaussianNB())
  5. In [ 52]: p.steps
  6. Out[ 52]:
  7. [( 'standardscaler', StandardScaler(copy= True, with_mean= True, with_std= True)),
  8. ( 'gaussiannb', GaussianNB(priors= None))]

同时可以通过set_params重新设置每个类里边需传入的参数,设置方法为step的name__parma名=参数值


 
 
  1. In [ 59]: p.set_params(standardscaler__with_mean= False)
  2. Out[ 59]: Pipeline(steps=[( 'standardscaler', StandardScaler(copy= True, with_mean=
  3. False, with_std= True)), ( 'gaussiannb', GaussianNB(priors= None))])

2.并行化,通过FeatureUnion实现
FeatureUnion,同样通过(key,value)对来设置,通过set_params设置参数。不同的是,每一个step分开计算,FeatureUnion最后将它们计算得到的结果合并到一块,返回的是一个数组,不具备最后一个estimator的方法。有些数据需要标准化,或者取对数,或onehot编码最后形成多个特征项,再选择重要特征,这时候FeatureUnion非常管用。



 
 
In [60]: from sklearn.pipeline import FeatureUnion
  
  

 
 
  1. In [ 61]: from sklearn.preprocessing import StandardScaler
  2. In [ 63]: from sklearn.preprocessing import FunctionTransformer
  3. In [ 64]: from numpy import log1p
  4. In [ 65]: step1=( 'Standar',StandardScaler())
  5. In [ 66]: step2=( 'ToLog',FunctionTransformer(log1p))
  6. In [ 67]: steps=FeatureUnion(transformer_list=[step1,step2])
  7. In [ 68]: steps.fit_transform(iris.data)
  8. Out[ 68]:
  9. array([[ -0.90068117, 1.03205722, -1.3412724 , ..., 1.5040774 ,
  10. 0.87546874, 0.18232156],
  11. [ -1.14301691, -0.1249576 , -1.3412724 , ..., 1.38629436,
  12. 0.87546874, 0.18232156],
  13. [ -1.38535265, 0.33784833, -1.39813811, ..., 1.43508453,
  14. 0.83290912, 0.18232156],
  15. ...,
  16. [ 0.79566902, -0.1249576 , 0.81962435, ..., 1.38629436,
  17. 1.82454929, 1.09861229],
  18. [ 0.4321654 , 0.80065426, 0.93335575, ..., 1.48160454,
  19. 1.85629799, 1.19392247],
  20. [ 0.06866179, -0.1249576 , 0.76275864, ..., 1.38629436,
  21. 1.80828877, 1.02961942]])
  22. In [ 69]: data=steps.fit_transform(iris.data)
  23. In [ 70]: data.shape #最后有8个特征,标准化后4个,log后4个,共个
  24. Out[ 70]: ( 150, 8)
  25. In [ 71]: iris.data.shape
  26. Out[ 71]: ( 150, 4)

 

 


参考:

http://scikit-learn.org/stable/modules/pipeline.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值