sklearn pipeline简介

来源:互联网 发布:mac文稿无端占内存 编辑:程序博客网 时间:2024/05/29 16:37

Pipeline可以将许多算法模型串联起来,比如将特征提取、归一化、分类组织在一起形成一个典型的机器学习问题工作流。主要带来两点好处:
1. 直接调用fit和predict方法来对pipeline中的所有算法模型进行训练和预测。
2. 可以结合grid search对参数进行选择

下面是一个官方文档的示例:

>>> from sklearn.pipeline import Pipeline>>> from sklearn.svm import SVC>>> from sklearn.decomposition import PCA>>> estimators = [('reduce_dim', PCA()), ('svm', SVC())]>>> clf = Pipeline(estimators)>>> clfPipeline(steps=[('reduce_dim', PCA(copy=True, n_components=None,    whiten=False)), ('svm', SVC(C=1.0, cache_size=200, class_weight=None,    coef0=0.0, decision_function_shape=None, degree=3, gamma='auto',    kernel='rbf', max_iter=-1, probability=False, random_state=None,    shrinking=True, tol=0.001, verbose=False))])

estimators中定义了两个模型,一个是PCA、另一个是SVC。

>>> clf.set_params(svm__C=10)

可以通过set_params函数对pipeline中的某个模型设定参数,上面是将svm参数C设置为10

另外一个例子:

>>> from sklearn import svm>>> from sklearn.datasets import samples_generator>>> from sklearn.feature_selection import SelectKBest>>> from sklearn.feature_selection import f_regression>>> from sklearn.pipeline import Pipeline>>> # generate some data to play with>>> X, y = samples_generator.make_classification(...     n_informative=5, n_redundant=0, random_state=42)>>> # ANOVA SVM-C>>> anova_filter = SelectKBest(f_regression, k=5)>>> clf = svm.SVC(kernel='linear')>>> anova_svm = Pipeline([('anova', anova_filter), ('svc', clf)])>>> # You can set the parameters using the names issued>>> # For instance, fit using a k of 10 in the SelectKBest>>> # and a parameter 'C' of the svm>>> anova_svm.set_params(anova__k=10, svc__C=.1).fit(X, y)...                                              Pipeline(steps=[...])>>> prediction = anova_svm.predict(X)>>> anova_svm.score(X, y)                        0.77...>>> # getting the selected features chosen by anova_filter>>> anova_svm.named_steps['anova'].get_support()...array([ True,  True,  True, False, False,  True, False,  True,  True, True,       False, False,  True, False,  True, False, False, False, False,       True], dtype=bool)
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 奶瓶吸奶费力怎么办 小孩上火感冒了怎么办 宝宝上火感冒了怎么办 3岁宝宝上火怎么办 上火又受凉感冒怎么办 上火引起的感冒怎么办 奶瓶排气孔漏水怎么办 奶嘴排气孔漏水怎么办 bbox吸管杯漏水怎么办 四个月宝宝拉肚子怎么办 租的房子坐月子怎么办 榨果汁不甜怎么办 宝宝不会喝奶粉怎么办 两个月宝宝不长肉怎么办 打疫苗后发烧怎么办 孕期不爱吃水果怎么办 孕期很少吃水果怎么办 三个月小孩不吃奶粉怎么办 三个月宝宝偏瘦怎么办 破壁机打果汁有沫怎么办 宝宝7个月坐不稳怎么办 婴儿头睡偏了怎么办天 宝宝不爱趴着怎么办 宝宝喜欢竖着抱怎么办 婴儿抱习惯了怎么办 新生儿总让抱着放下就哭可怎么办 三个月宝宝认人怎么办 三个月的宝宝认生怎么办 一岁半宝宝尿黄怎么办 一岁多宝宝尿少怎么办 1岁宝宝一晚没尿怎么办 抗利尿激素少怎么办 小孩夜里尿多怎么办 一岁宝宝认生怎么办 婴儿一个月认生怎么办 婴儿大便带血丝怎么办 两个月宝宝认生怎么办 晚上宝宝认人怎么办 小孩长白头发怎么办 三个月婴儿脚力不足怎么办 未满月宝宝便秘怎么办