9

I want to save and load a fitted Random Forest Classifier, but I get an error.

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')

The error is:

  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 . . .

Do I have to initialize forest2 or something?

1
  • save a random forest! :)
    – ihadanny
    Sep 23, 2015 at 19:59

2 Answers 2

8

I solved it with cPickle instead:

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)

but a joblib solution could be useful as well.

1
  • Tried it and it doesn't work between machines in my case :( got the exact same error with pickle and cPickle
    – ihadanny
    Sep 23, 2015 at 20:26
3

Here is the method that you can try

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'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.