Python 属性错误:';随机回归器';对象没有属性';最佳参数';
我在使用随机林对我的分类进行网格搜索时遇到此错误Python 属性错误:';随机回归器';对象没有属性';最佳参数';,python,scikit-learn,random-forest,grid-search,Python,Scikit Learn,Random Forest,Grid Search,我在使用随机林对我的分类进行网格搜索时遇到此错误 from sklearn.ensemble import RandomForestRegressor rf2 = RandomForestRegressor(random_state = 50) rf2.fit(X_train1, y_train1) ### Grid Search ### num_leafs = [1, 5, 10, 20, 50, 100] parameters3 = [{'n_estimators' : range(10
from sklearn.ensemble import RandomForestRegressor
rf2 = RandomForestRegressor(random_state = 50)
rf2.fit(X_train1, y_train1)
### Grid Search ###
num_leafs = [1, 5, 10, 20, 50, 100]
parameters3 = [{'n_estimators' : range(100,800,20),
'max_depth': range(1,20,2),
'min_samples_leaf':num_leafs
}]
gs3 = GridSearchCV(estimator=rf2,
param_grid=parameters3,
cv = 10,
n_jobs = -1)
gs3 = rf2.fit(X_train1, y_train1)
gs3.best_params_ # <- thats where I get the Error
从sklearn.employ导入随机森林回归器
rf2=随机森林回归器(随机_状态=50)
rf2.装配(X_系列1,y_系列1)
###网格搜索###
num_leafs=[1,5,10,20,50,100]
参数3=[{'n_估计量]:范围(100800,20),
“最大深度”:范围(1,20,2),
“min\u示例叶”:num\u叶
}]
gs3=GridSearchCV(估计器=rf2,
参数网格=参数3,
cv=10,
n_作业=-1)
gs3=rf2.配合(X\U列1,y\U列1)
gs3.best_params#替换此:
gs3=rf2.fit(X\U列1,y\U列1)
据此:
gs3.fit(X\U列1,y\U列1)
然后您将能够使用:
gs3.最佳参数
您的错误是由于您将gs3
重新分配给RandomForest()
调用而导致的,因此gs3
不再是GridSearchCV
对象。嗯,您不适合GridSearch
对象,而是适合模型(rf2)然后将其分配给gs3
参数。
你有:
gs3 = GridSearchCV(estimator=rf2,
param_grid=parameters3,
cv = 10,
n_jobs = -1)
gs3 = rf2.fit(X_train1, y_train1)
gs3.best_params_ # <- thats where I get the Error
gs3=GridSearchCV(估计器=rf2,
参数网格=参数3,
cv=10,
n_作业=-1)
gs3=rf2.配合(X\U列1,y\U列1)
谢谢,这就是问题所在!
gs3 = GridSearchCV(estimator=rf2,
param_grid=parameters3,
cv = 10,
n_jobs = -1)
gs3.fit(X_train1, y_train1) # fit the GridSearchCV object
gs3.best_params_ # <- thats where I get the Error