Skip to main content
 首页 » 编程设计

python之将随机森林模型保存到文件

2024年11月01日9落叶无声

正在尝试保存随机森林模型。

所有的方法都失败了:

self.model = RandomForestClassifier(n_estimators=n_estimators,criterion='entropy', min_samples_leaf=2, max_depth=15,min_samples_split=5, max_features=None, n_jobs=-1, random_state=555) 
 
def save_model(self, fname): 
    with open(fname,'wb') as f : 
        dill.dumps(self.model, f) 
 
 
 
pickle: TypeError: can't pickle instancemethod objects 
 
joblib : PicklingError: Can't pickle <type 'instancemethod'>: it's not found as __builtin__.instancemethod 
 
cPickle : TypeError: can't pickle instancemethod objects 
 
dill : ValueError: pickle protocol must be <= 2 

 : type(r.model) 
 : sklearn.ensemble.forest.RandomForestClassifier 
 
 :with open('test.dill', 'wb') as f : dill.dump(r.model,f, protocol=2) 
 
  PicklingError: Can't pickle <class 'random_forest.RFWords'>: it's not the same object as random_forest.RFWords 

random_forest.RFWords 是包含 RF 的类! 它如何访问 self.model 所在的类


嗯……我认为这是 IPython 的问题……因为现在我正在更仔细地测试它……有时它可以工作!!

可能是自动重载问题!!

Yep the moment I modify the source code save_model() stops working ..

请您参考如下方法:

使用 joblib 来 pickle 你训练好的模型:

from joblib import dump, load 
from sklearn.ensemble import RandomForestClassifier 
 
#load data 
X, y = load_data(...) 
 
#fit the model 
estimator = RandomForestClassifier() 
estimator.fit(X,y) 
 
#pickle model to disk 
dump(estimator, 'my_randomforest_model.joblib')  
 
#loading saved model 
estimator = load('my_randomforest_model.joblib') 
 
estimator.predict(...) 

更新:

根据这个错误,你必须使用更高的协议(protocol)进行 pickeling (>= 2):

dill : ValueError: pickle protocol must be <= 2

尝试使用更高的协议(protocol)进行转储,如下所示:

dump(estimator, 'my_randomforest_model.joblib', protocol=2)