我有一個訓練有素的RandomForestRegressor模型,我想保存到一個文件以供重複使用。我遵循scikit-learn持久性頁面上的說明,並可以保存訓練有素的模型。問題是我似乎無法重新使用它,因爲scikit-learn並不認爲它是經過培訓的。爲什麼我無法保存訓練有素的RandomForestRegressor模型?
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')
但是,當我嘗試重新使用該模型:
model = joblib.load('./trained_model/tree.pkl')
prediction = np.array(model.predict(patient_arr))
我得到的錯誤:
文件「/usr/local/lib/python2.7/dist-packages /sklearn/ensemble/forest.py「,行614,在預測 check_is_fitted(self,'n_outputs_') 文件」/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py「 ,第627行,check_is_fitted raise NotFittedError(msg%{'name':type(estimator)。name}) sklearn.utils.validation.NotFittedError:此RandomForestRegressor實例尚未安裝。在使用這種方法之前,用適當的參數調用'fit'。
我也試過:
trained_model = model.fit(training_input,training_target_values)
joblib.dump(trained_model, './trained_model/tree.pkl')
具有相同的結果。
根據提供的信息無法回答此問題。在保存之前需要看到模型預測成功,以確定模型是否成功。看起來這不是RandomForestRegressor中的錯誤,而是海報的代碼。建議關閉這個。 – aquraishi
該帖子的要點是該模型沒有錯誤,但保存的模型不能在沒有上面列出的適合性錯誤的情況下運行。該錯誤明確指出,如果已經發布的代碼清楚說明該模型實際上是合適的,則該模型不適用。相關的代碼已經發布。 – user2188329