scikit-learn: Error unpickling RandomizedSearchCV objects in 0.18 due to masked arrays

Description

In version 0.18, loading pickles of fitted RandomizedSearchCV objects results in a TypeError exception (from pickle also created with version 0.18).

The error seems related to the use of masked arrays in the RandomizedSearchCV.cv_results_ attribute - clearing this before pickling (i.e. setting to to None) allows pickling/unpickling to work.

Steps/Code to Reproduce

import pickle                                                                   
from sklearn.model_selection import RandomizedSearchCV                          
from sklearn.ensemble import RandomForestClassifier                             

X = [[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]]                            
y = [1, 1, 1, 0, 0, 0]                                                          

model = RandomizedSearchCV(RandomForestClassifier(),                            
                           {'n_estimators': [5, 10, 20]},                       
                           n_iter=3)                                            
model.fit(X, y)                                                                 

with open('model.pkl', 'wb') as fh:                                             
    pickle.dump(model, fh)                                                      

with open('model.pkl', 'rb') as fh:                                             
    model = pickle.load(fh)

print(model.predict(X))

Expected Results

[1, 1, 1, 0, 0, 0]

Actual Results

Traceback (most recent call last):
  File "./t.py", line 19, in <module>
    model = pickle.load(fh)
  File "/Users/dsc/miniconda3/envs/p3/lib/python3.5/site-packages/numpy/ma/core.py", line 5863, in __setstate__
    super(MaskedArray, self).__setstate__((shp, typ, isf, raw))
TypeError: object pickle not returning list

Versions

Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7 2015, 11:24:55) [GCC 4.2.1 (Apple Inc. build 5577)] NumPy 1.11.1 SciPy 0.18.1 Scikit-Learn 0.18

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 34 (30 by maintainers)

Commits related to this issue

Most upvoted comments

GaelVaroquaux: But “None” is also a valid option for parameters, right?

@amueller no problem at all, I’ve been keeping an eye on some of the discussions around it, so I knew that a solution was in the works anyway 😃

@raghavrv ah, great, I hadn’t seen that! Should be able to spend some time testing it towards the end of this week.