ste*_*anE 9 python pipeline python-2.7 scikit-learn
我想应用样本权重,同时使用来自sklearn的管道(应该进行特征转换,例如多项式),然后应用回归量,例如ExtraTrees.
我在以下两个示例中使用以下包:
from sklearn.ensemble import ExtraTreesRegressor
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
Run Code Online (Sandbox Code Playgroud)
只要我单独转换功能并在之后生成和训练模型,一切都很顺利:
#Feature generation
X = np.random.rand(200,4)
Y = np.random.rand(200)
#Feature transformation
poly = PolynomialFeatures(degree=2)
poly.fit_transform(X)
#Model generation and fit
clf = ExtraTreesRegressor(n_estimators=5, max_depth = 3)
weights = [1]*100 + [2]*100
clf.fit(X,Y, weights)
Run Code Online (Sandbox Code Playgroud)
但是在管道中执行它不起作用:
#Pipeline generation
pipe = Pipeline([('poly2', PolynomialFeatures(degree=2)), ('ExtraTrees', ExtraTreesRegressor(n_estimators=5, max_depth = 3))])
#Feature generation
X = np.random.rand(200,4)
Y = np.random.rand(200)
#Fitting model
clf = pipe
weights = [1]*100 + [2]*100
clf.fit(X,Y, weights)
Run Code Online (Sandbox Code Playgroud)
我得到以下错误:TypeError:fit()最多需要3个参数(给定4个)在这个简单的例子中,修改代码没有问题,但是当我想在我的真实数据中运行几个不同的测试时代码,能够使用管道和样品重量
Kev*_*vin 13
**fit_params在文档的fit方法中提到了Pipeline.您必须指定要将参数应用于管道的哪个步骤.您可以通过遵循文档中的命名规则来实现此目的:
为此,它可以使用它们的名称和以"__"分隔的参数名称来设置各个步骤的参数,如下例所示.
所以说的是,尝试将最后一行更改为:
clf.fit(X,Y, **{'ExtraTrees__sample_weight': weights})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2857 次 |
| 最近记录: |