如何解决来自SCIKIT的GridSearch示例学习用户指南尝试给出错误
试图按照Grid Search的SCIKIT用户指南运行相同的代码,但给出了错误。非常惊讶。
from sklearn.model_selection import GridSearchCV
from sklearn.calibration import CalibratedClassifierCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_moons
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_iris
X,y=make_moons()
calibrated_forest=CalibratedClassifierCV(base_estimator=RandomForestClassifier(n_estimators=10))
paramgrid={'base_estimator_max_depth':[2,4,6,8]}
search=GridSearchCV(calibrated_forest,paramgrid,cv=5)
search.fit(X,y)
错误消息如下:
ValueError: Invalid parameter base_estimator_max_depth for estimator CalibratedClassifierCV(base_estimator=RandomForestClassifier(n_estimators=10)). Check the list of available parameters with `estimator.get_params().keys()`.
我尝试了虹膜数据集,该数据集也给出了与上述相同的错误。
然后,我使用make_moon数据集X,y并运行如下所示的Random分类器。
clf = RandomForestClassifier(n_estimators=10,max_depth=2)
cross_val_score(clf,X,y,cv=5)
获得如下输出。
array([0.8,0.8,0.9,0.95,0.95])
看起来很奇怪,不确定发生了什么,我错在哪里。请寻求帮助。
解决方法
请注意__
和参数之间的双得分base_estimator
:
from sklearn.model_selection import GridSearchCV
from sklearn.calibration import CalibratedClassifierCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_moons
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_iris
X,y=make_moons()
calibrated_forest=CalibratedClassifierCV(base_estimator=RandomForestClassifier(n_estimators=10))
paramgrid={'base_estimator__max_depth':[2,4,6,8]}
search=GridSearchCV(calibrated_forest,paramgrid,cv=5)
search.fit(X,y)
GridSearchCV(cv=5,estimator=CalibratedClassifierCV(base_estimator=RandomForestClassifier(n_estimators=10)),param_grid={'base_estimator__max_depth': [2,8]})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。