Issue
I have taken a look at this: RandomForestClassifier instance not fitted yet. Call 'fit' with appropriate arguments before using this method 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.
Solution
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.
Answered By - DataJanitor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.