I have a trained RandomForestRegressor model I would like to save to a file for re-use. I'm following the instructions on the scikit-learn persistence page, and can save the trained model. The problem is that I cannot seem to re-use it because scikit-learn does not recognize it as trained.
model = RandomForestRegressor(n_estimators=100, max_features='sqrt', max_depth=12, n_jobs=24)
model.fit(training_input,training_target_values)
joblib.dump(model, './trained_model/tree.pkl')
But when I try to re-use the model:
model = joblib.load('./trained_model/tree.pkl')
prediction = np.array(model.predict(patient_arr))
I get the error:
File "/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/forest.py", line 614, in predict check_is_fitted(self, 'n_outputs_') File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 627, in check_is_fitted raise NotFittedError(msg % {'name': type(estimator).name}) sklearn.utils.validation.NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
I've also tried:
trained_model = model.fit(training_input,training_target_values)
joblib.dump(trained_model, './trained_model/tree.pkl')
with the same results.