RandomizedSearchCv导致属性错误

时间:2016-04-07 22:48:09

标签: python numpy pandas machine-learning scikit-learn

在RandomizedSearchCv上执行fit()之后:

2014-15
2014-15
2014-15
2013-14
2013-14
2013-14
2012-13
2012-13
etc

我收到以下错误:

        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)

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

    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'

可能是什么原因? X_train(文本)和y_train(二进制)是正确的(我猜)numpy数组。

包含数据集的整个代码: https://github.com/kuba2111/untitled12

1 个答案:

答案 0 :(得分:1)

在这里,您使用RandomizedSearchCV代替GridSearchCV。 所以看起来它认为其中一个参数是一个分布并试图从这个分布中进行采样。

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

enter image description here