我在以下两个示例中使用以下包:
from sklearn.ensemble import ExtraTreesRegressorimport numpy as npfrom sklearn.pipeline import Pipelinefrom sklearn.preprocessing import polynomialFeatures
只要我单独转换功能并在之后生成和训练模型,一切都很顺利:
#Feature generationX = np.random.rand(200,4)Y = np.random.rand(200)#Feature transformationpoly = polynomialFeatures(degree=2)poly.fit_transform(X)#Model generation and fitclf = ExtraTreesRegressor(n_estimators=5,max_depth = 3)weights = [1]*100 + [2]*100clf.fit(X,Y,weights)
但是在管道中执行它不起作用:
#Pipeline generationpipe = Pipeline([('poly2',polynomialFeatures(degree=2)),('ExtraTrees',ExtraTreesRegressor(n_estimators=5,max_depth = 3))])#Feature generationX = np.random.rand(200,4)Y = np.random.rand(200)#Fitting modelclf = pipeweights = [1]*100 + [2]*100clf.fit(X,weights)
我收到以下错误:TypeError:fit()最多需要3个参数(给定4个)
在这个简单的例子中,修改代码没有问题,但是当我想在我的实际代码中对我的真实数据运行几个不同的测试时,能够使用管道和样本权重
Pipeline
文档的fit方法中提到了** fit_params.您必须指定要将参数应用于管道的哪个步骤.您可以通过遵循文档中的命名规则来实现此目的: For this,it enables setting parameters of the varIoUs steps using their names and the parameter name separated by a ‘__’,as in the example below.
所以说的是,尝试将最后一行更改为:
clf.fit(X,**{'ExtraTrees__sample_weight': weights})
This is a good example如何使用管道中的参数.
总结以上是内存溢出为你收集整理的python – sklearn pipeline – 在管道中应用多项式特征转换后应用样本权重全部内容,希望文章能够帮你解决python – sklearn pipeline – 在管道中应用多项式特征转换后应用样本权重所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)