我有一个滑雪管道与PolynomialFeatures()和LinearRegression()系列。我的目标是利用多项式特征的不同degree对数据进行拟合,并测量分数。以下是我使用的代码-
steps = [('polynomials',preprocessing.PolynomialFeatures()),('linreg',linear_model.LinearRegression())]
pipeline = pipeline.Pipeline(steps=steps)
scores = dict()
for i in range(2,6):
params = {'polynomials__degree': i,'polynomials__include_bias': False}
#pipeline.set_params(**params)
pipeline.fit(X_train,y=yCO_logTrain,**params)
scores[i] = pipeline.score(X_train,yCO_logTrain)
scores我收到错误- TypeError: fit() got an unexpected keyword argument 'degree'。
为什么即使参数以<estimator_name>__<parameter_name>格式命名,也会引发此错误?
发布于 2021-07-17 15:07:41
根据文档
**fit_paramsdict的字符串->对象参数传递给每个步骤的fit方法,其中每个参数名都有前缀,因此步骤s的参数p有键s__p。
这意味着以这种方式传递的参数直接传递给s step .fit()方法。如果检查PolynomialFeatures文档,则在构造PolynomialFeatures对象时使用degree参数,而不是在其.fit()方法中使用。
如果要尝试管道中的估值器/转换器的不同超参数,可以使用如图所示的GridSearchCV。下面是链接中的示例代码:
from sklearn.pipeline import Pipeline
from sklearn.feature_selection import SelectKBest
pipe = Pipeline([
('select', SelectKBest()),
('model', calibrated_forest)])
param_grid = {
'select__k': [1, 2],
'model__base_estimator__max_depth': [2, 4, 6, 8]}
search = GridSearchCV(pipe, param_grid, cv=5).fit(X, y)https://stackoverflow.com/questions/68421561
复制相似问题