RandomizedSearchCV和GridSearchCV,在调用fit方法的时候产生'list' object has no attribute 'values'错误之处理方法
来源:互联网 发布:网易云网络连接不可用 编辑:程序博客网 时间:2024/05/16 01:59
【pyhon 版本 3.5.0 skit-learn版本<0.18.1>】
昨天发现的问题,RandomizedSearchCV怎么都调不通:
# Split the dataset in two equal partsX_train, X_test, y_train, y_test = train_test_split( data,label, test_size=0.25, random_state=0) # Set the parameters by cross-validationtuned_parameters = [{'n_neighbors': range(2,7)}, {'leaf_size':range(9,100,3)}, {'p':range(1,5)}] svr=KNeighborsClassifier() scores = ['precision', 'recall'] for score in scores: print("# Tuning hyper-parameters for %s" % score) print() labels=y_train.values aa c, r = labels.shape labels = labels.reshape(c,) clf = RandomizedSearchCV(svr, tuned_parameters,cv=5,n_jobs=-1,verbose=3)# clf = GridSearchCV(svr, tuned_parameters,cv=5,n_jobs=-1,verbose=3)clf.fit(X_train, labels)
报错如下:
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/gzhuangzhongyi/Desktop/NetEase/test/RandomSearchCV_Functional.py", line 46, in <module> clf.fit(X_train, labels) File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 1190, in fit return self._fit(X, y, groups, sampled_params) File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 564, in _fit for parameters in parameter_iterable File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__ while self.dispatch_one_batch(iterator): File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 603, in dispatch_one_batch tasks = BatchedCalls(itertools.islice(iterator, batch_size)) File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 127, in __init__ self.items = list(iterator_slice) File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 557, in <genexpr> )(delayed(_fit_and_score)(clone(base_estimator), X, y, self.scorer_, File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 230, in __iter__ for v in self.param_distributions.values()]) AttributeError: 'list' object has no attribute 'values'
经过查看fit方法,发现无论如何调整fit方法的参数,都没法运行。
但是如果换成GridSearchCV就可以运行。
经过查看类实现,发现两种类调用了相同的,fit方法,但是,fit方法有隐含传入的参数:
sampled_params = ParameterSampler(self.param_distributions, self.n_iter, random_state=self.random_state) return self._fit(X, y, groups, sampled_params)
其中,sampled_params为传入参数之采样。
其传入参数在初始化的时候传入:
clf = RandomizedSearchCV(svr, tuned_parameters,cv=5,n_jobs=-1,verbose=3)
而,这个参数由:
tuned_parameters = [{'n_neighbors': range(2,7)}, {'leaf_size':range(9,100,3)}, {'p':range(1,5)}]
语句设定,这里有三个字典。而正确的是:
tuned_parameters = [{'n_neighbors': range(2,7), 'leaf_size':range(9,100,3), 'p':range(1,5)}]
Grid的时候会遍历字典中所有参数的组合,所以字典的划分不重要。
for p in self.param_grid: # Always sort the keys of a dictionary, for reproducibility items = sorted(p.items()) if not items: yield {} else: keys, values = zip(*items) for v in product(*values): params = dict(zip(keys, v)) yield params
但是Randomlize,当传入字典的时候,会作为带分布的进行处理,对字典取值
# Always sort the keys of a dictionary, for reproducibility items = sorted(self.param_distributions.items()) for _ in six.moves.range(self.n_iter): params = dict() for k, v in items: if hasattr(v, "rvs"): if sp_version < (0, 16): params[k] = v.rvs() else: params[k] = v.rvs(random_state=rnd) else: params[k] = v[rnd.randint(len(v))] yield params
Random会检查传入的参数,如果可以遍历就认为是分布。
于是传入作为fit的参数集的时候,不是作为可遍历的对象的字典,可以.values,而是一个一个把分布元素组合成字典的list,但因为传入的不是一个分布而是一个list,所以不能对分布取值。
上面的两段函数GridSearchCV产生的参数集:
RandomizeSearchCV产生的参数集因为debug调不出来,无法展示。
0 0
- RandomizedSearchCV和GridSearchCV,在调用fit方法的时候产生'list' object has no attribute 'values'错误之处理方法
- 'GridSearchCV' object has no attribute 'cv_results_' 这一问题的解决
- 'numpy.ndarray' object has no attribute 'values'
- psutil在rlimit方法在rhel6上报错AttributeError: 'Process' object has no attribute 'rlimit'
- AttributeError: 'list' object has no attribute 'write_pdf'
- AttributeError: 'list' object has no attribute 'write_pdf'
- AttributeError: 'NoneType' object has no attribute 'execute' 错误的解决
- 'function' object has no attribute '_name_'错误
- js调用java方法的遇到的坑Uncaught TypeError: Object [object Object] has no method
- web.py 启动时候出现AttributeError: 'module' object has no attribute 'inet_pton'错误
- web.py 启动时候出现AttributeError: 'module' object has no attribute 'inet_pton'错误
- GNU Radio自定义模块在添加到流图后运行报‘module’ object has no attribute错误的解决方法
- python 使用 pip 安装 三方库的时候报错 “AttributeError: '_socketobject' object has no attribute...”
- sqlalchemy enum AttributeError: 'list' object has no attribute 'replace'
- scrapy出现:AttributeError: 'list' object has no attribute 'xpath'
- yum错误'module' object has no attribute 'iterparse'解决方法
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- openstack:''Client'' object has no attribute ''service_catalog''错误
- CyclicBarrier
- javascript 中==和===运算符
- [LeetCode]315. Count of Smaller Numbers After Self
- 1005. Spell It Right (20)
- redis-server运行后,控制台不能继续输入命令解决方法
- RandomizedSearchCV和GridSearchCV,在调用fit方法的时候产生'list' object has no attribute 'values'错误之处理方法
- Python面试题大全(二)
- 欢迎使用CSDN-markdown编辑器
- springMVC 中部分注解的使用
- 89. Gray Code
- TEW-654TR路由器漏洞分析和挖掘
- iOS10下的推送(简单处理)
- 使用Redis Desktop Manager图形工具连接SentOS-redis
- 状态选择