我正在使用 scikit-learn 练习一些文本。
为了更加熟悉 GridSearch,我从这里找到的一些示例代码开始:
###############################################################################
# define a pipeline combining a text feature extractor with a simple
# classifier
pipeline = Pipeline([
('vect', CountVectorizer())
])
parameters = {
'vect__max_df': (0.5, 0.75, 1.0)
}
grid_search.fit(X_train, y_train)
print("Best score: %0.3f" % grid_search.best_score_)
请注意,我在这里非常小心,我只有一个估计器和一个参数!
我发现当我运行它时,我得到了错误:
TypeError: If no scoring is specified, the estimator passed should have a 'score' method. The estimator Pipeline(steps=[('vect', CountVectorizer(analyzer=u'word', binary=False, decode_error=u'strict',
dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content',
lowercase=True, max_df=1.0, max_features=None, min_df=1,
ngram_range=(1, 1), preprocessor=None, stop_words=None,
strip_accents=None, token_pattern=u'(?u)\\b\\w\\w+\\b',
tokenizer=None, vocabulary=None))]) does not.
嗯...为什么我缺少某种“分数”属性?
当我检查可能的参数时,
print CountVectorizer().get_params().keys()
正如这个答案所暗示的那样,我看不到任何可以得分的地方。
文档说By default, parameter search uses the score function of the estimator to evaluate a parameter setting.
那么为什么我需要指定评分方法?
无论如何,我认为我可能需要明确地传递一个scoring
参数,但这并没有帮助并给我一个错误: grid_search.fit(X_train, y_train, scoring=None)
我不明白这个错误!