Skip to main content
 首页 » 编程设计

python之spark_sklearn GridSearchCV __init__ 因参数错误而失败

2024年08月19日40sky-heaven

我正在尝试使用 spark_sklearn.GridSearchCV,但遇到了 init 参数错误。

TypeError: __init__() takes at least 4 arguments (4 given) 

这是代码:

from spark_sklearn import GridSearchCV 
 
gsearch2 = GridSearchCV(estimator=ensemble.GradientBoostingRegressor(**params), param_grid=param_test2, n_jobs=1) 

如果我给GridSearchCV更多的参数,比如添加cv=5,那么错误就变成了

TypeError: __init__() takes at least 4 arguments (5 given) 

有什么建议吗?

谢谢。

请您参考如下方法:

GridSearchCV.__init__ 需要 3 obligatory arguments :

  • sc - SparkContext。
  • 估算器
  • param_grid。

您忘记了 SparkContext:

GridSearchCV( 
    sc=SparkContext.getOrCreate(), 
    estimator=ensemble.GradientBoostingRegressor(**params),  
    param_grid=param_test2, n_jobs=1)