0
votes

I'm trying to load a prediction after unpickling but I'm getting this error

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release. from numpy.core.umath_tests import inner1d /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator DecisionTreeClassifier from version 0.20.2 when using version 0.19.2. This might lead to breaking code or invalid results. Use at your own risk. UserWarning) /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator RandomForestClassifier from version 0.20.2 when using version 0.19.2. This might lead to breaking code or invalid results. Use at your own risk. UserWarning) Traceback (most recent call last): File "rf_pred_model_tester.py", line 7, in print('Class: ',int(rf.predict(xx))) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/ensemble/forest.py", line 538, in predict proba = self.predict_proba(X) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/ensemble/forest.py", line 581, in predict_proba n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/ensemble/base.py", line 153, in _partition_estimators n_jobs = min(_get_n_jobs(n_jobs), n_estimators) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/init.py", line 464, in _get_n_jobs if n_jobs < 0: TypeError: '<' not supported between instances of 'NoneType' and 'int'

here is the code that i am trying to run

import pickle
import numpy as np
with open('rf_model_1','rb') as f:
    rf=pickle.load(f)

xx = np.array([67, 17832, 1, 1, 0, 33, 1941902452, 36, 33011.0, 19, 18, 0, 2, 1]).reshape(1,-1)
print('Class: ',int(rf.predict(xx)))

I'm expecting a result like this :

Class:  [0]

if i run the code on jupyter its working fine but, i'm getting error when i try to run on terminal.

1

1 Answers

4
votes

Your error put it bluntly:

UserWarning: Trying to unpickle estimator RandomForestClassifier from version 0.20.2 when using version 0.19.2. This might lead to breaking code or invalid results. Use at your own risk.

And indeed that is what happened; when pickling, your RandomForestClassifier's attribute n_jobs was kept at None. This is the default value for initialization, but behind the scenes this is usually set to 1. You can find more details on n_jobs here: https://scikit-learn.org/stable/glossary.html#term-n-jobs

For you, setting rf's n_jobs to 1 will do the trick:

import pickle
import numpy as np
with open('rf_model_1','rb') as f:
    rf=pickle.load(f)

rf.n_jobs = 1

xx = np.array([67, 17832, 1, 1, 0, 33, 1941902452, 36, 33011.0, 19, 18, 0, 2, 1]).reshape(1,-1)
print('Class: ',int(rf.predict(xx)))