scikit-learn:训练分类器、预测新数据、评价分类器

http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html


构建分类器,以NB为例:
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB().fit(X_tfidf, rawData.target)

要预测新样本,需要使用和训练集完全相同的特征提取方法,不同点是,只要call transform instead of fit_transform on the transformers,因为我们在训练集上已经fit过了。
docs_new = ['i love this book', 'the book is not so good']
X_new_counts = count_vect.transform(docs_new)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)

predicted = clf.predict(X_new_tfidf)
for doc, category in zip(docs_new, predicted):
    print('%r => %s' % (doc, rawData.target_names[category]))   
'i love this book' => positive_folder
'the book is not so good' => positive_folder

评估分类器效果:
y_new_target=[1, 0]
import numpy as np
np.mean(predicted == y_new_target)
Out[42]: 0.5

from sklearn import metrics
print(metrics.classification_report(y_new_target, predicted,
...     target_names=rawData.target_names))
                 precision    recall  f1-score   support

negative_folder       0.00      0.00      0.00         1
positive_folder       0.50      1.00      0.67         1

    avg / total       0.25      0.50      0.33         2

D:\Anaconda\lib\site-packages\sklearn\metrics\metrics.py:1771: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
  'precision', 'predicted', average, warn_for)

metrics.confusion_matrix(y_new_target, predicted)
Out[45]: 
array([[0, 1],
       [0, 1]])









你可能感兴趣的:(机器学习——文本挖掘,scikit-learn,scikit-learn,scikit-learn,机器学习,数据挖掘,训练分类器)