以下是与问题相关的部分代码。如果需要完整代码,这里有一个完整的可重现代码也可以下载数据:https ://github.com/ageron/handson-ml2/blob/master/02_end_to_end_machine_learning_project.ipynb我有一个管道:prepare_select_and_predict_pipeline = Pipeline([ ('preparation', full_pipeline), ('feature_selection', TopFeatureSelector(feature_importances, k)), ('svm_reg', SVR(**rnd_search.best_params_))])现在,我只想从上面的管道执行这一部分:('preparation', full_pipeline),('feature_selection', TopFeatureSelector(feature_importances, k)),我试过prepare_select_and_predict_pipeline.fit(housing, housing_labels)了,但它也执行 SVM 部分。最后,我需要从上面的管道中获得与执行以下代码相同的结果:preparation_and_feature_selection_pipeline = Pipeline([ ('preparation', full_pipeline), ('feature_selection', TopFeatureSelector(feature_importances, k))])housing_prepared_top_k_features = preparation_and_feature_selection_pipeline.fit_transform(housing)我该怎么做?
2 回答
冉冉说
TA贡献1877条经验 获得超1个赞
您可以将管道切片,就好像它们是列表(版本 >=0.21)一样,所以
prepare_select_and_predict_pipeline[:-1].fit_transform(housing)
应该管用。
(你在这里需要小心;你正在改装管道的变压器部分,所以在一个新的数据集上进行,然后prepare_select_and_predict_pipeline.predict(X_new)
将使用改装的变压器!clone
如果需要,你可以使用一个新变量。)
BIG阳
TA贡献1859条经验 获得超6个赞
FeatureUnion可以做到这一点:
from sklearn.pipeline import FeatureUnion, Pipeline
prepare_select_pipeline = Pipeline([
('preparation', full_pipeline),
('feature_selection', TopFeatureSelector(feature_importances, k))
])
feats = FeatureUnion([('prepare_and_select', prepare_select_pipeline)])
prepare_select_and_predict_pipeline = Pipeline([('feats', feats),
('svm_reg', SVR(**rnd_search.best_params_))])
您可以在深入了解 Sklearn 管道中找到有关此的更多信息
添加回答
举报
0/150
提交
取消