随机森林的保存

9

我想保存和加载一个已拟合的随机森林分类器,但是出现了错误。

forest = RandomForestClassifier(n_estimators = 100, max_features = mf_val)
forest = forest.fit(L1[0:100], L2[0:100])
joblib.dump(forest, 'screening_forest/screening_forest.pkl')
forest2 = joblib.load('screening_forest/screening_forest.pkl')

错误提示为:
  File "C:\Users\mkolarek\Documents\other\TrackerResultAnalysis\ScreeningClassif
ier\ScreeningClassifier.py", line 67, in <module>
    forest2 = joblib.load('screening_forest/screening_forest.pkl')
  File "C:\Python27\lib\site-packages\sklearn\externals\joblib\numpy_pickle.py",
 line 425, in load
    obj = unpickler.load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python27\lib\site-packages\sklearn\externals\joblib\numpy_pickle.py",
 line 285, in load_build
    Unpickler.load_build(self)
  File "C:\Python27\lib\pickle.py", line 1217, in load_build
    setstate(state)
  File "_tree.pyx", line 2280, in sklearn.tree._tree.Tree.__setstate__ (sklearn\
tree\_tree.c:18350)
ValueError: Did not recognise loaded array layout
Press any key to continue . . .

我需要初始化forest2或其他东西吗?

保存一个随机森林! :) - ihadanny
2个回答

8

我用cPickle解决了这个问题:

with open('screening_forest/screening_forest.pickle', 'wb') as f:
    cPickle.dump(forest, f)

with open('screening_forest/screening_forest.pickle', 'rb') as f:
    forest2 = cPickle.load(f)

但是使用Joblib的解决方案也可能很有用。

尝试过了,但在我的情况下它无法在机器之间工作 :( 使用pickle和cPickle时得到完全相同的错误。 - ihadanny

3
以下是您可以尝试的方法:
model = RandomForestClassifier()

model.fit(data,lables)

import pickle

Model_file = 'model.pkl'

pickle.dump(model, open(Model_file, 'wb'))

'''Reloading the model
load the model from Saved file'''

loaded_model = pickle.load(open(Model_file, 'rb'))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接