在分类任务下,预测结果(Predicted Condition)与正确标记(True Condition)之间存在四种不同的组合,构成混淆矩阵(适用于多分类)。

精确率(Precision)
精确率:预测结果为正例样本中真实为正例的比例(查的准)

召回率(Recall)
召回率:真实为正例的样本中预测结果为正例的比例(查的全,对正样本的区分能力)

分类模型评估API sklearn.metrics.classification_report

示例代码:贝叶斯模型文本分类
from sklearn.datasets import fetch_20newsgroups
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report
def naviebayes():
'''
朴素贝叶斯进行文本分类
:return: None
'''
news = fetch_20newsgroups(subset='all')
# 进行数据分割
x_train,x_test,y_train,y_test = train_test_split(news.data,news.target,test_size=0.25)
# 对数据集进行特征抽取
tf = TfidfVectorizer()
# 以训练集当中的词的列表进行每篇文章重要性统计
#print('x_train:\n',x_train,'x_test:\n',x_test)
x_train = tf.fit_transform(x_train)
print(tf.get_feature_names())
x_test = tf.transform(x_test)
# 进行朴素贝叶斯算法的预测
mlt = MultinomialNB(alpha=1.0)
print(x_train.toarray())
mlt.fit(x_train,y_train)
y_predict = mlt.predict(x_test)
print('预测的文章类别为:',y_predict)
# 得出准确率
print('准确率为:',mlt.score(x_test,y_test))
print('每个类别的精确率和召回率:\n',classification_report(y_test,y_predict,target_names=news.target_names))
return None
if __name__ == '__main__':
naviebayes()