ValueError: Invalid parameter

老规矩,先上源代码:from sklearn.model_selection import GridSearchCVparam_grid =[ {'n_eatimatiors':[3,10,30],'max_features':[2,4,6,8]}, {'bootstrap':[False],'n_estimators':[3,10],'max_features':[2,3,4]},]forest_reg=RandomForestRegressor(random_state=42)
摘要由CSDN通过智能技术生成

老规矩,先上源代码:

from sklearn.model_selection import GridSearchCV
param_grid =[
    {
   'n_eatimatiors':[3,10,30],'max_features':[2,4,6,8]},
    {
   'bootstrap':[False],'n_estimators':[3,10],'max_features':[2,3,4]},
]

forest_reg=RandomForestRegressor(random_state=42)
grid_search=GridSearchCV(forest_reg,param_grid,cv=5,
                        scoring='neg_mean_squared_error',return_train_score=True)
grid_search.fit(housing_prepared,housing_labels)

介绍下这段代码的应用方向和实现效果,
模型调参和网格搜索
1.手动调整超参数,找到很好的组合时很困难的
2.使用GridSearchCV替你进行搜索,告诉GridSearchCV,进行试验的超参数是什么,以及需要尝试的值,他会使用交叉验证评估所有超参数的可能组合

上述代码运行后的效果:

ValueError                                Traceback (most recent call last)
<ipython-input-353-54613827b5ad> in <module>
      8 grid_search=GridSearchCV(forest_reg,param_grid,cv=5,
      9                         scoring='neg_mean_squared_error',return_train_score=True)
---> 10 grid_search.fit(housing_prepared,housing_labels)

d:\python3.8.5\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     70                           FutureWarning)
     71         kwargs.update({
   k: arg for k, arg in zip(sig.parameters, args)})
---> 72         return f(**kwargs)
     73     return inner_f
     74 

d:\python3.8.5\lib\site-packages\sklearn\model_selection\_search.py 
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这个错误通常是因为您尝试使用一个未定义的参数,或者参数名称拼写错误,导致模型构建失败。 在您的代码中,您使用了 `'alpha': [0.0001, 0.001, 0.01]` 作为参数,但是这个参数实际上是用于设置 `SGDClassifier` 模型的,而不是 `Perceptron` 模型。因此,您需要将这个参数从参数网格中移除,或者将其替换为 `Perceptron` 模型支持的参数。 例如,以下代码演示了如何使用正确的参数设置来构建一个 `Pipeline` 模型: ``` from sklearn.preprocessing import StandardScaler from sklearn.linear_model import Perceptron from sklearn.pipeline import Pipeline from sklearn.model_selection import GridSearchCV, StratifiedKFold # 数据预处理 scaler = StandardScaler() merged_train_norm_vec = scaler.fit_transform(merged_train_norm_vec) # 参数设置 param_grid = {'penalty': ['l1', 'l2', 'elasticnet', 'none'], 'alpha': [0.0001, 0.001, 0.01], 'max_iter': [100, 500, 1000], 'tol': [0.001, 0.01, 0.1]} # 模型构建 model = Pipeline([('scaler', scaler), ('Perceptron', Perceptron())]) # 交叉验证 k = 10 cv = StratifiedKFold(n_splits=k, shuffle=True) # 网格搜索 grid = GridSearchCV(model, param_grid, cv=cv, scoring='accuracy') grid.fit(merged_train_norm_vec, y_merged_train) # 输出最优参数和分类准确率 print('Best params:', grid.best_params_) print('Best score:', grid.best_score_) ``` 在这个例子中,我们使用了 `Perceptron` 模型,并设置了 `penalty`、`max_iter` 和 `tol` 等参数。您可以根据您的需要修改这些参数,或者添加其他支持的参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值