英文:
NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator
问题
这是错误信息的中文翻译部分:
"这个RandomForestRegressor实例尚未拟合。在使用此估算器之前,请调用'fit'并提供适当的参数。"
英文:
I have taken a look at this: https://stackoverflow.com/questions/51397611/randomforestclassifier-instance-not-fitted-yet-call-fit-with-appropriate-argu not helped.
I was running RandomForest Regressor model with RandomizedSearchCV. It was running for 3 hr and suddenly it gave this error.
The relevant part of my code:
rf = RandomForestRegressor()
rs_rf = RandomizedSearchCV(rf, param, cv=2, n_jobs=-1, verbose=1)
rs_rf.fit(X_train, Y_train)
rs_rf_train = rf.predict(X_train)
rs_rf_test = rf.predict(X_test)
The error:
---------------------------------------------------------------------------
NotFittedError Traceback (most recent call last)
<ipython-input-8-de03d0ce1f81> in <cell line: 141>()
139 rs_rf = RandomizedSearchCV(rf, param, cv=2, n_jobs=-1, verbose=1)
140 rs_rf.fit(X_train, Y_train)
--> 141 rs_rf_train = rf.predict(X_train)
142 rs_rf_test = rf.predict(X_test)
143
1 frames
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
1388
1389 if not fitted:
-> 1390 raise NotFittedError(msg % {"name": type(estimator).__name__})
1391
1392
NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
答案1
得分: 1
你将RandomForestRegressor
实例传递给了RandomizedSearchCV
实例。
在拟合RandomizedSearchCV
时,实际上会拟合多个随机森林回归器(RFR)并对它们进行交叉验证。然后,你的rs_rf
实例是RandomizedSearchCV
的结果,其中包含一个或多个已拟合的RandomForestRegressor
。你可以通过调用best_rf = rs_rf.best_estimator_
来访问其中最佳的一个。
当你调用rf
对象时,实际上是在调用一个未拟合的实例,因为它已被传递给了RandomizedSearchCV
,而不是RandomForestRegressor
。
希望这个解释对你更清晰。
英文:
You pass the RandomForestRegressor
instance to the RandomizedSearchCV
instance.
When fitting the RandomizedSearchCV
, you actually fit multiple RFR and cross-validate them. Your rs_rf
instance then is the result of the RandomizedSearchCV
, which contains one or more fitted RandomForestRegressor
s. You can access the best of these by calling best_rf = rs_rf.best_estimator_
When you call the rf
object, you call an unfitted instance, because it has been passed to RandomizedSearchCV
, which in turn has been fit, not RandomForestRegressor
.
I hope this explanation makes it clearer for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论