Scikit learn 网格搜索学习曲线误差

Scikit learn 网格搜索学习曲线误差,scikit-learn,pickle,Scikit Learn,Pickle,运行以下代码时出现此错误。罪魁祸首似乎是一行代码“sp_randint(1,sqrt(X.shape[1]))”。如果我用一个列表对象替换它,那么它看起来很好。有人知道这个错误吗 import numpy as np import matplotlib.pyplot as plt import sklearn from sklearn import cross_validation from sklearn.datasets import load_digits from sklearn.lea

运行以下代码时出现此错误。罪魁祸首似乎是一行代码“
sp_randint(1,sqrt(X.shape[1]))
”。如果我用一个列表对象替换它,那么它看起来很好。有人知道这个错误吗

import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn import cross_validation
from sklearn.datasets import load_digits
from sklearn.learning_curve import learning_curve
from scipy.stats import randint as sp_randint
from math import sqrt
from sklearn.ensemble import RandomForestClassifier
from sklearn.grid_search import RandomizedSearchCV

print sklearn.__version__

digits = load_digits()
X, y = digits.data, digits.target
forest = RandomForestClassifier(n_estimators=1000,  random_state=9)
# specify parameters and distributions to sample from
param_dist = {"max_depth": [3, 5, 7, 9, None],
                       "min_samples_split": sp_randint(1, sqrt(X.shape[1])),
                       "bootstrap": [True, False]}
# run randomized search
n_iter_search = 2
n_cores = 2
cv = 3
random_search = RandomizedSearchCV(estimator=forest,
                                   param_distributions=param_dist,
                                   # scoring='roc_auc',
                                   cv=cv,
                                   n_iter=n_iter_search,
                                   n_jobs=1,
                                   random_state=9)
train_sizes=np.linspace(.1, 1.0, 2)
train_sizes, train_scores, test_scores = learning_curve(random_search, X, y, cv=cv, n_jobs=n_cores, train_sizes=train_sizes)
以下是错误:

Exception in thread Thread-5:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 808, in __bootstrap_inner
    self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 761, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 342, in _handle_tasks
    put(task)
  File "/Users/zech/env/tmp/lib/python2.7/site-packages/sklearn/externals/joblib/pool.py", line 372, in send
    CustomizablePickler(buffer, self._reducers).dump(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 562, in save_tuple
    save(element)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 562, in save_tuple
    save(element)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 419, in save_reduce
    save(state)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 681, in _batch_setitems
    save(v)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 681, in _batch_setitems
    save(v)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 419, in save_reduce
    save(state)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 681, in _batch_setitems
    save(v)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 419, in save_reduce
    save(state)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 681, in _batch_setitems
    save(v)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 396, in save_reduce
    save(cls)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 748, in save_global
    (obj, module, name))
PicklingError: Can't pickle <type 'instancemethod'>: it's not found as __builtin__.instancemethod
线程5中的异常: 回溯(最近一次呼叫最后一次): 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py”,第808行,在bootstrap\u内部 self.run() 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py”,第761行,运行中 自我目标(*自我参数,**自我参数) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py”,第342行,在任务处理中 放置(任务) 文件“/Users/zech/env/tmp/lib/python2.7/site packages/sklearn/externals/joblib/pool.py”,第372行,在send中 可定制卸料机(缓冲器、自减速器)。卸料(obj) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,转储文件第224行 自我保存(obj) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,保存中第286行 f(self,obj)#用显式self调用未绑定方法 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第562行,在save_tuple中 保存(元素) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,保存中第286行 f(self,obj)#用显式self调用未绑定方法 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第562行,在save_tuple中 保存(元素) 保存中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第331行 自我保存(obj=obj,*rv) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第419行,在save_中 保存(状态) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,保存中第286行 f(self,obj)#用显式self调用未绑定方法 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第649行,在save_dict中 self.\u batch\u setitems(obj.iteritems()) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第681行,在批处理设置项中 保存(v) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,保存中第286行 f(self,obj)#用显式self调用未绑定方法 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第649行,在save_dict中 self.\u batch\u setitems(obj.iteritems()) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第681行,在批处理设置项中 保存(v) 保存中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第331行 自我保存(obj=obj,*rv) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第419行,在save_中 保存(状态) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,保存中第286行 f(self,obj)#用显式self调用未绑定方法 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第649行,在save_dict中 self.\u batch\u setitems(obj.iteritems()) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第681行,在批处理设置项中 保存(v) 保存中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第331行 自我保存(obj=obj,*rv) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第419行,在save_中 保存(状态) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,保存中第286行 f(self,obj)#用显式self调用未绑定方法 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第649行,在save_dict中 self.\u batch\u setitems(obj.iteritems()) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第681行,在批处理设置项中 保存(v) 保存中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第331行 自我保存(obj=obj,*rv) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第396行,在save\u中 保存(cls) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,保存中第286行 f(self,obj)#用显式self调用未绑定方法 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”,第748行,在save_global中 (对象、模块、名称)) PicklingError:无法pickle:找不到它作为_内置的_.instancemethod
我不确定是否会分别对每个样本数进行网格搜索,但这肯定是可能的。你能提出一个问题吗?对于像这里这样的离散分布,用列表替换它是很好的。只有对于连续分布,它才真正起作用。@AndreasMueller:谢谢你的回复。我看到了网格搜索或仅使用随机/默认模型参数的优缺点。我将在github上打开一个票证。我的意思是,如果你用一个列表替换它,它将对具有有限支持的离散发行版执行完全相同的操作,就像你在这里所做的那样。但它仍然需要修复。