我正在这样使用 GridSearchCV:
corpus = load_files('corpus')
with open('stopwords.txt', 'r') as f:
stop_words = [y for x in f.read().split('\n') for y in (x, x.title())]
x = corpus.data
y = corpus.target
pipeline = Pipeline([
('vec', CountVectorizer(stop_words=stop_words)),
('classifier', MultinomialNB())])
parameters = {'vec__ngram_range': [(1, 1), (1, 2)],
'classifier__alpha': [1e-2, 1e-3],
'classifier__fit_prior': [True, False]}
gs_clf = GridSearchCV(pipeline, parameters, n_jobs=-1, cv=5, scoring="f1", verbose=10)
gs_clf = gs_clf.fit(x, y)
joblib.dump(gs_clf.best_estimator_, 'MultinomialNB.pkl', compress=1)
然后,在另一个文件中,为了分类新的文档(不是来自语料库的),我会这样做:
classifier = joblib.load(filepath) # path to .pkl file
result = classifier.predict(tokenlist)
我的问题是:我从哪里获取需要
classification_report
的值?在许多其他示例中,我看到人们将语料库分成训练集和测试集。然而,由于我正在使用带有kfold交叉验证的
GridSearchCV
,所以我不需要这样做。那么我该如何从GridSearchCV
获取这些值?
gs_clf.fit(x, y)
是否会返回None
? - BallpointBen