【发布时间】:2022-07-29 17:34:31
【问题描述】:
我正在使用我训练并保存的随机森林。我想创建一个使用该模型并提供预测的类。 我是新来的课程,我做了如下:
from sklearn.ensemble import RandomForestClassifier
import os
import joblib
model_path = r"the path of the model"
model = joblib.load(os.path.join(model_path,'rf.pkl'))
class MODEL_RF(RandomForestClassifier):
def load_model(self):
self.model = model
def get_pred(self, df):
validation_features = np.array(df)
self.model = self.load_model()
pred = self.predict(validation_features)
predict_prob = self.predict_proba(validation_features)
return pred,predict_prob
model_m = MODEL_RF()
prediction, probs = model_m.get_pred(input_df)
然而, 我收到如下错误:
raise NotFittedError(msg % {"name": type(estimator).__name__})
sklearn.exceptions.NotFittedError: This MODEL_RF instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
【问题讨论】:
-
始终提供完整的错误回溯;它包含有价值的调试信息。
标签: python class scikit-learn random-forest production