sklearn Pipeline 和Ploynomial

Pipeline:一个管道将模型搭建的过程放在一起,如数据预处理和模型建立放在一起,方便参数的同时调整

转载自:http://blog.csdn.net/yisuoyanyv/article/details/74066962; http://blog.csdn.net/SA14023053/article/details/52079542

1.将要处理的步骤利用pipline进行组合

from sklearn.pipline import pipline

from sklearn.decomposition import PCA

from sklearn.svm import SVC

estimators=[('reduce_dim',PCA()),('clf',SVC())]

pipe=Pipeline(estimators)#摆放在同一个管道中,注意前后顺序

pipe.steps[0]#将显示步骤中的PCA相关信息,同理1将显示SVC中的相关信息;以list的形式;;;steps属性里以列表形式存着管道中的估计器

pipe.named_steps['reduce_dim']#在named_steps属性中以dict形式存着步骤,返回的为值

2.设置相关的参数

pipe.set_params(clf__C=10)#以这种形式给指定名字的估计器(clf)的参数(C)赋值 <estimator>__<parameter>

3.进行网格搜索#http://blog.csdn.net/cherdw/article/details/54970366

from sklearn.model_selection import GridSearchCV

params=dict(reduce_dim__n_components=[2,5,10],#设置reduce_dim的n_components参数为多个值以供选取模型最优值 clf__C=[0.1,10,100])

#在模型选择过程中可以设置参数列表

grid_search=GridSearchCV(pipe,param_grid=params)

grid_search.fit(x,y)

grid_search.score#不清楚这是如何计算的score,为模型中的score参数

总结:

安装到一个管道中,方便进行整体的gridsearchcv

Polynomial:#转载自:http://blog.csdn.net/lulei1217/article/details/49582821;http://blog.csdn.net/xw_classmate/article/details/51171090

用于产生多项式,[a,b],,degree=2,产生的顺序为[1, a, b, a^2, ab, b^2].

from sklearn.preprocessing import  PolynominalFeatures

poly=PolynominalFeatures(degree=2,intersection_only=False,include_bais=False)

主要的参数:

degree:多项式的最高次幂;

intersection_only:是否只有交互影响

include_bais:是否包含常数项

poly.fit_transform(x)#将输出多项式的具体数值

 

posted on 2017-11-05 12:42  湘雨jay  阅读(619)  评论(0编辑  收藏  举报