机器学习系列专栏
选自 Python-Machine-Learning-Book On GitHub
作者:Sebastian Raschka
翻译&整理 By Sam
上一篇文章讲了混淆矩阵的概念以及相关的评价指标,这篇文章就讲一下这么指标如何用python代码来实现。
一、混淆矩阵实现
1fig, ax = plt.subplots(figsize=(2.5, 2.5))
2ax.matshow(confmat, cmap=plt.cm.Blues, alpha=0.3)
3for i in range(confmat.shape[0]):
4 for j in range(confmat.shape[1]):
5 ax.text(x=j, y=i, s=confmat[i, j], va='center', ha='center')
6
7plt.xlabel('predicted label')
8plt.ylabel('true label')
9
10plt.tight_layout()
11plt.show()
二、相关评价指标实现
分别是准确度、recall以及F1指标的实现。
三、指定评价指标自动选出最优模型
可以通过在make_scorer中设定参数,确定需要用来评价的指标(这里用了fl_score),这个函数可以直接输出结果。