python - RandomizedSearchCv 导致属性错误

标签 python numpy pandas machine-learning scikit-learn

在 RandomizedSearchCv 上执行 fit() 后:

        tfidf = TfidfVectorizer(strip_accents=None,lowercase=False,preprocessor=None)
        param_grid = 
            {'vect__ngram_range': [(1,1)],'vect__stop_words': [stop, None],
                       'vect__tokenizer': [tokenizer, tokenizer_porter],
                       'clf__penalty': ['l1', 'l2'],
            'clf__C': [1.0, 10.0, 100.0]},
lr_tfidf = Pipeline([('vect', tfidf),('clf',LogisticRegression(random_state=0))])
gs_lr_tfidf = RandomizedSearchCV(lr_tfidf,param_grid,cv=5,n_jobs=1)
gs_lr_tfidf.fit(X_train, y_train)

我收到以下错误:

    Traceback (most recent call last):
  File "G:/pythonprojectraschka/ch08/ch08-2.py", line 95, in <module>
    gs_lr_tfidf.fit(X_train, y_train)
  File "C:\Anaconda3\lib\site-packages\sklearn\grid_search.py", line 996, in fit
    return self._fit(X, y, sampled_params)
  File "C:\Anaconda3\lib\site-packages\sklearn\grid_search.py", line 553, in _fit
    for parameters in parameter_iterable
  File "C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 800, in __call__
    while self.dispatch_one_batch(iterator):
  File "C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 653, in dispatch_one_batch
    tasks = BatchedCalls(itertools.islice(iterator, batch_size))
  File "C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 68, in __init__
    self.items = list(iterator_slice)
  File "C:\Anaconda3\lib\site-packages\sklearn\grid_search.py", line 549, in <genexpr>
    delayed(_fit_and_score)(clone(base_estimator), X, y, self.scorer_,
  File "C:\Anaconda3\lib\site-packages\sklearn\grid_search.py", line 223, in __iter__
    for v in self.param_distributions.values()])
AttributeError: 'list' object has no attribute 'values'

但例如,Pipeline(lr_tfidf) 执行没有任何问题:

lr_tfidf.fit(X_train, y_train)

可能是什么原因? X_train(text) 和 y_train(binary) 是正确的(我猜)numpy 数组。

带有数据集的完整代码: https://github.com/kuba2111/untitled12

最佳答案

这里您使用的是 RandomizedSearchCV而不是GridSearchCV 。 所以看起来它认为其中一个参数是一个分布,并尝试从这个分布中采样。

因此,如果您愿意使用 GridSearchCV 对所有参数进行详尽的搜索,那么您的解决方案就是您的解决方案。

enter image description here

关于python - RandomizedSearchCv 导致属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488564/

相关文章:

python - 无法在 python 中从 sklearn 重现 GridSearch

python - 什么是 pycryptodomex,它与 pycryptodome 有何不同?

python - 如何使用python从sqlite中获取数据?

python - Python 指令中的预期类型 'Union[ndarray, Iterable]' 警告

python - 在 numpy 数组外切片

python - Pivot_table 来自列值中的列表

Python Pandas 比较两个数据帧以将国家/地区分配给电话号码

python - 零填充数据,直到其长度等于 2 的幂

python - 更改索引集

python - Pandas :无法导入名称邻接