我在RandomSearchCV中不断收到AttributeError [英] I keep getting AttributeError in RandomSearchCV

查看:245
本文介绍了我在RandomSearchCV中不断收到AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

x_tu = data_cls_tu.iloc[:,1:].values
y_tu = data_cls_tu.iloc[:,0].values

classifier = DecisionTreeClassifier()
parameters = [{"max_depth": [3,None],
               "min_samples_leaf": np.random.randint(1,9),
               "criterion": ["gini","entropy"]}]
randomcv = RandomizedSearchCV(estimator=classifier, param_distributions=parameters,
                              scoring='accuracy', cv=10, n_jobs=-1,
                              random_state=0)
randomcv.fit(x_tu, y_tu)



---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-fa8376cb54b8> in <module>()
     11                               scoring='accuracy', cv=10, n_jobs=-1,
     12                               random_state=0)
---> 13 randomcv.fit(x_tu, y_tu)

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    616         n_splits = cv.get_n_splits(X, y, groups)
    617         # Regenerate parameter iterable for each fit
--> 618         candidate_params = list(self._get_param_iterator())
    619         n_candidates = len(candidate_params)
    620         if self.verbose > 0:

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in __iter__(self)
    236         # in this case we want to sample without replacement
    237         all_lists = np.all([not hasattr(v, "rvs")
--> 238                             for v in self.param_distributions.values()])
    239         rnd = check_random_state(self.random_state)
    240 

AttributeError: 'list' object has no attribute 'values'

我在RandomSearchCV的fit方法上总是遇到错误.

Hi, I keep getting error on the fit method for RandomSearchCV.

当我在GridSearchCV上使用它们时,它可以工作,但是GridSearchCV需要5个小时才能完成.

It worked when I used them on GridSearchCV, but GridSearchCV took 5 hours to complete.

x_tu,y_tu都是numpy.ndarray类型.

x_tu, y_tu are both numpy.ndarray type.

推荐答案

param_distributions必须是dict对象(

param_distributions must be dict object (documentation) but you are passing a list containing single dict. Remove outer square brackets then it should work fine.

应该是这样的:

parameters = {"max_depth": [3,None],
               "min_samples_leaf": [np.random.randint(1,9)],
               "criterion": ["gini","entropy"]}

这篇关于我在RandomSearchCV中不断收到AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆