在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详尽搜索所有参数,则可以解决此问题。
关于python - RandomizedSearchCv导致属性错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36488564/