今晚,笔者接到一个客户的请求,即:对多分类结果的每一类进行评价,即输出每一类的准确率(precision)、召回率(recall)和F1值(F1-score)。对于这个需求,我们可以使用sklearn来解决。方法不难。笔者在此仅作记录,以供本人及读者日后参考。我们模拟的数据如下:y_true=['北京','上海','成都','成都','上海','北京','上海','成都','北京','上海']y_pred=['北京','上海','成都','上海','成都','成都','上海','成都','北京','上海']其中y_true为真实数据,y_pred是多分类后的模拟数据。使用sklearn.metrics中的classification_report实现多分类中每个分类的指标评估。示例Python代码如下:#-*-coding:utf-8-*-fromsklearn.metricsimportclassification_reporty_true=['北京','上海','成都','成都','上海','北京','上海','成都','北京','上海']y_pred=['北京','上海','成都','上海','成都','成都','上海','Chengdu','北京','上海']t=classification_report(y_true,y_pred,target_names=['北京','上海','成都'])print(t)输出结果如下:precisionrecallf1-score支持北京0.750.750.754上海1.000.670.803成都0.500.670.573accuracy0.7010macroavg0.750.690.7110weightedavg0.750.700.7110需要注意的是输出结果的数据类型为str,如果需要输出结果,可以使用方法中的output_dict参数设置为True,输出结果如下:{'北京':{'precision':0.75,'recall':0.75,'f1-score':0.75,'support':4},'上海':{'精度':1.0,'recall':0.6666666666666666,'f1-score':0.8,'support':3},'成都':{'precision':0.5,'recall':0.6666666666666666,'f1-score':0.5714285714285715,'support':3},'accuracy':0.7,'macroavg':{'precision':0.75,'recall':0.6944444444444443,'f1-score':'0.70714285572,0.70714285572support':10},'weightedavg':{'precision':0.75,'recall':0.7,'f1-score':0.7114285714285715,'support':10}}使用confusion_matrix方法可以输出multi的混淆-分类问题矩阵,代码如下:fromsklearn.metricsimportconfusion_matrixy_true=['北京','上海','成都','成都','上海','北京','上海','成都','北京','上海']y_pred=['北京','上海','成都','上海','成都','成都','上海','成都','北京','上海'']print(confusion_matrix(y_true,y_pred,labels=['北京','上海','成都']))输出结果如下:[[201][031][012]]为了将混淆矩阵绘制成图片,可以使用如下Python代码:#-*-coding:utf-8-*-#author:Jclian91#pl王牌:北京大兴#时间:2019-11-1421:52fromsklearn.metricsimportconfusion_matriximportmatplotlib.pyplotaspltimportmatplotlibasmpl#支持中文字体显示,Mac系统使用zhfont=mpl.font_manager.FontProperties(fname="/Library/Fonts/Songti.ttc")y_true=['北京','上海','成都','成为Both','上海','北京','上海','成都','北京','上海']y_pred=['北京','上海','成都','上海','成都','成都','上海','成都','北京','上海']classes=['北京','上海','成都']confusion=confusion_matrix(y_true,y_pred)#绘制热力图,紧接着by指定色块,灰色也可以,gray_x反色也可以zhfont)plt.yticks(indices,classes,fontproperties=zhfont)plt.colorbar()plt.xlabel('y_pred')plt.ylabel('y_true')#显示范围内first_index的数据(len(confusion)):forsecond_indexinrange(len(confusion[first_index])):plt.text(first_index,second_index,confusion[first_index][second_index])#显示图片plt.show()生成的混淆矩阵图片如下:这是分享到此结束,感谢大家阅读,也感谢大家入住北京ing大兴,我当然要待会儿啦~
