【问题标题】:I keep getting AttributeError in RandomSearchCV我在 RandomSearchCV 中不断收到 AttributeError
【发布时间】:2018-12-03 17:01:44
【问题描述】:
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 的拟合方法上不断出错。

当我在 GridSearchCV 上使用它们时它起作用了,但是 GridSearchCV 需要 5 个小时才能完成。

x_tu, y_tu 都是 numpy.ndarray 类型。

【问题讨论】:

    标签: machine-learning scikit-learn jupyter-notebook data-science sklearn-pandas


    【解决方案1】:

    param_distributions 必须是 dict 对象 (documentation) 但您传递的列表包含单个 dict。删除外部方括号,然后它应该可以正常工作。

    应该是这样的:

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

    【讨论】:

    • 感谢您的回答!但是现在我遇到了另一个错误:“TypeError:'int'类型的对象没有len()”在同一个fit方法中。我在此处粘贴了错误消息:pastebin.com/7zW7RU2u
    • @JasonKim,我猜问题出在"min_samples_leaf": np.random.randint(1,9), 行。 RandomSearchCV 期望它是一个列表,但这里给出了 int。它应该像[np.random.randint(1,9)](单个随机数)或[np.random.randint(1,9) for i in range(10)](10 个随机数)
    猜你喜欢
    • 2018-12-03
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 2021-03-02
    • 2021-04-23
    相关资源
    最近更新 更多