【发布时间】:2015-07-31 04:30:37
【问题描述】:
我正在尝试使用 Scikit Learn 使用此处显示的方法对文本数据进行分类。 (http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html) 除了我正在加载我自己的数据集。
我得到了结果,但我想找到分类结果的准确性。
from sklearn.datasets import load_files
text_data = load_files("C:/Users/USERNAME/projects/machine_learning/my_project/train", description=None, categories=None, load_content=True, shuffle=True, encoding='latin-1', decode_error='ignore', random_state=0)
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier
text_clf = Pipeline([('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', LinearSVC(loss='hinge', penalty='l2',
random_state=42)),
])
_ = text_clf.fit(text_data.data, text_data.target)
docs_new = ["Some test sentence here.",]
predicted = text_clf.predict(docs_new)
print np.mean(predicted == text_data.target)
for doc, category in zip(docs_new, predicted):
print('%r => %s' % (doc, text_data.target_names[predicted]))
在这里,我得到的 np.mean 预测值为 0.566。
如果我尝试:
twenty_test = load_files("C:/Users/USERNAME/projects/machine_learning/my_project/testing", description=None, categories=None, load_content=True, shuffle=True, encoding='latin-1', decode_error='ignore', random_state=0)
docs_test = twenty_test.data
predicted = text_clf.predict(docs_test)
np.mean(predicted == twenty_test.target)
现在它打印为 1。
我不明白这是如何工作的,不明白 np.mean 到底是什么,以及为什么它在使用相同的数据进行训练时会显示不同的结果。
“train”文件夹有大约 15 个文档,text 文件夹也有大约 15 个文档,以防万一。总的来说,我对 Scikit Learn 和机器学习非常陌生,因此非常感谢任何帮助。谢谢!
【问题讨论】:
-
你的测试文件是什么样的?同时打印预测和二十测试目标。
标签: python machine-learning scikit-learn classification text-classification