【Scikit-Learn 中文文档】Pipeline(管道)和 FeatureUnion(特
中文文档:http://sklearn.apachecn.org/cn/stable/modules/pipeline.html 英文文档:http://sklearn.apachecn.org/en/stable/modules/pipeline.html
官方文档:http://scikit-learn.org/stable/ GitHub:https://github.com/apachecn/scikit-learn-doc-zh(觉得不错麻烦给个 Star,我们一直在努力) 贡献者:https://github.com/apachecn/scikit-learn-doc-zh#贡献者 关于我们:http://www.apachecn.org/organization/209.html
4.1. Pipeline(管道)和 FeatureUnion(特征联合): 合并的评估器4.1.1. Pipeline: 链式评估器
管道中的所有评估器,除了最后一个评估器,管道的所有评估器必须是转换器。 (例如,必须有 4.1.1.1. 用法
功能函数 import make_pipeline
sklearn.naive_bayes import MultinomialNB
sklearn.preprocessing import Binarizer
>>> make_pipeline(Binarizer(), MultinomialNB())
steps=[('binarizer',Binarizer(copy=True,threshold=0.0)),51)"> ('multinomialnb',MultinomialNB(alpha=1.0,51)"> class_prior=None,51)"> fit_prior=True))])
管道中的评估器作为一个列表保存在
pipe.steps[0]
('reduce_dim',iterated_power='auto',n_components=None,random_state=None,51)"> svd_solver='auto',tol=0.0,whiten=False))
并作为
named_steps['reduce_dim']
PCA(copy=True,whiten=False)
管道中的评估器参数可以通过
set_params(clf__C=10)
named_steps 的属性映射到多个值,在交互环境支持 tab 补全:
named_steps.reduce_dim is pipeTrue
这对网格搜索尤其重要:
import GridSearchCV
>>> param_grid = dict(reduce_dim__n_components=[2, 5,80)">10],
... clf__C=[0.1,80)">10,80)">100])
>>> grid_search = GridSearchCV(pipe, param_grid=param_grid)
单独的步骤可以用多个参数替换,除了最后步骤,其他步骤都可以设置为
import LogisticRegression
dict(reduce_dim=[None, PCA(5),80)">10)],9); font-weight:bold">... clf=[SVC(), LogisticRegression()],102)">=param_grid)
例子:
也可以参阅:
4.1.1.2. 注意点对管道调用 4.1.1.3. 缓存转换器:避免重复计算适配转换器是很耗费计算资源的。设置了``memory`` 参数,
import mkdtemp
shutil import rmtree
>>> cachedir = mkdtemp()
= Pipeline(estimators, memory=cachedir)
Pipeline(...,...))])
>>> # Clear the cache directory when you don't need it anymore
>>> rmtree(cachedir)
Warning Side effect of caching transfomers 使用
import load_digits
>>> digits = load_digits()
>>> pca1 = PCA()
>>> svm1 = SVC()
= Pipeline([(pca1), svm1)])
.fit(digits.data, digits.target)
...
# The pca instance can be inspected directly
>>> print(pca1.components_)
[[ -1.77484909e-19 ... 4.07058917e-18]]
开启缓存会在适配前触发转换器的克隆。因此,管道的转换器实例不能被直接查看。 在下面例子中, 访问
pca2 >>> svm2 >>> cached_pipe pca2), svm2)],9); font-weight:bold">... memory>>> cached_pipe Pipeline(memory=...,51)"> steps=[('reduce_dim',32)">print(cached_pipe'reduce_dim'].components_)
[[ -1.77484909e-19 ... 4.07058917e-18]]
# Remove the cache directory
>>> rmtree(cachedir)
例子:
4.1.2. FeatureUnion(特征联合): 个特征层面
可以结合:class:FeatureUnion和 (一个 4.1.2.1. 用法一个
FeatureUnion
import KernelPCA
'linear_pca',160)">'kernel_pca', KernelPCA())]
>>> combined = FeatureUnion(estimators)
>>> combined
FeatureUnion(n_jobs=1,51)"> transformer_list=[('linear_pca',51)"> ('kernel_pca',KernelPCA(alpha=1.0,...))],51)"> transformer_weights=None)
跟管道一样,特征联合有一个精简版的构造器叫做:func:make_union,该构造器不需要显式给每个组价起名字。 正如
combined.set_params(kernel_pca=None)
transformer_weights=None)
例子:
有兴趣的们也可以和我们一起来维护,持续更新中 。。。 机器学习交流群:629470233 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |