我想构建一个对文本进行分类的算法:火腿或垃圾邮件;我有每类文本的训练/测试数据。 (我的列车数据适用于每个类别8000 sentences
,测试时每个类别包含2000 sentences
)
X_train看起来像['please, call me asap!', 'watch out the new sales!', 'hello jim can we talk?', 'only today you can buy this', 'don't miss our offer!']
y_train看起来像[1 0 1 0 0]
,其中1 =火腿,0 =垃圾邮件
与X_test和y_test相同。
这是我的代码片段:
# classifier can be LogisticRegression, MultinomialNB, RandomForest, DecisionTree
text_clf = Pipeline([('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', classifier),
])
model = text_clf.fit(X_train, y_train)
y_predict = model.predict(X_test)
这些是我测量的参数:
print(accuracy_score(y_test, y_predict))
print(f1_score(y_test, y_predict, average="weighted"))
print(recall_score(y_test, y_predict, pos_label=1, average="binary"))
print(precision_score(y_test, y_predict, average="weighted"))
如果我不使用任何优化(remove stop words, remove punctuation, stem words, lemmatize words
),我会获得每个参数约95%的结果。如果我使用这些优化,精度,f1分数和精度会急剧下降到50-60%。召回功能保持不变,为95%。
为什么会这样?我错在哪里?我是否正确计算了这些参数?或者这是正常行为?
答案 0 :(得分:0)
我只知道出了什么问题:不合适。我进行了交叉验证
scores = cross_val_score(model, X_train, y_train, cv=10, scoring='accuracy')
现在一切都很好,我得到了我期待的结果。