NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator

huangapple go评论131阅读模式
英文:

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 RandomForestRegressors. 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.

huangapple
  • 本文由 发表于 2023年5月24日 23:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定