sourav
sourav

Reputation: 3

This RandomForestClassifier instance is not fitted yet

I am using Rendom Forest for prediction

from sklearn.ensemble import RandomForestClassifier
forest_clf = RandomForestClassifier(random_state=42)
y_train_pred = cross_val_predict(forest_clf, X_train, y_train, cv=3)

for y_train_pred system is showing 95% precision

I am using same model for actual prediction

test_stocks        = test_set.drop("is4PercPrft", axis=1)
test_stocks_labels = test_set["is4PercPrft"].copy()
test_prepared = full_pipeline.transform(test_stocks)
test_stocks_labels = test_stocks_labels.astype(np.uint8)
test_stocks_labels.value_counts()

for the below line I am getting error

final_predictions = forest_clf.predict(test_prepared)

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

Upvotes: 0

Views: 770

Answers (1)

Josh Briegal
Josh Briegal

Reputation: 46

From the source code of cross_val_predict, it would appear that the estimator is cloned when calculating the predictions in this method, and so forest_clf will not be fitted after running cross_val_predict.

You'll need to train the actual estimator used:

forest_clf.fit(X_train, y_train)

before calling

final_predictions = forest_clf.predict(test_prepared)

Upvotes: 3

Related Questions