def plot_confusion_matrix(cm, labels, title='Confusion Matrix'): plt.imshow(cm, interpolation='nearest', cmap='Blues') plt.title(title) plt.colorbar() xlocations = np.array(range(len(labels))) plt.xticks(xlocations, labels, rotation=90) plt.yticks(xlocations, labels) plt.ylabel('True...
然后调用plot_confusion_matrix()绘制该分类应用于该数据集的的测试集时的混淆矩阵。 # Example 1: Using sklearn plot_confusion_matrix # Ref: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_confusion_matrix.html import matplotlib.pyplot as plt from sklearn.datasets import make_...
defplot_confusion_matrix(cm, classes, normalize=True, title='Confusion matrix', cmap=plt.cm.hot): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ ifnormalize: cm=cm.astype('float')/cm.sum(axis=1)[:, np.newaxis] ...
现在来看多分类模型,我们知道了求混淆矩阵的函数返回值是长宽为类别个数的二维矩阵,下面是个三分类的例子: >>> from sklearn.metrics import confusion_matrix >>> y_true = [2, 0, 2, 2, 0, 1] >>> y_pred = [0, 0, 2, 2, 0, 2] >>> confusion_matrix(y_true, y_pred) array([[2, ...
scikit-plot是一个基于sklearn和Matplotlib的库,主要的功能是对训练好的模型进行可视化,功能比较简单易懂。 https://scikit-plot.readthedocs.io pip install scikit-plot 功能1:评估指标可视化 scikitplot.metrics.plot_confusion_matrix快速展示模型预测结果和标签计算得到的混淆矩阵。
('none')plt.gca().yaxis.set_ticks_position('none')plt.grid(True,which='minor',linestyle='-')plt.gcf().subplots_adjust(bottom=0.15)plot_confusion_matrix(cm_normalized,title='Normalized confusion matrix')# show confusion matrixplt.savefig('../Data/confusion_matrix.png',format='png')plt....
tick_marks = np.array(range(len(labels))) +0.5defplot_confusion_matrix(cm, title='Confusion Matrix', cmap=plt.cm.binary): plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() xlocations = np.array(range(len(labels))) ...
计算混淆矩阵——confusion_matrix将变量转换为数据框——pd.DataFrame创建热图——sn.heatmap最后,将图保存到文件中 -cfm_plot.figure.savefig 从sklearn.metrics导入pandas作为pd导入chaos_matrix导入seaborn作为sn如果__name__ == '__main__' : predictions = [ "None","Dog","Cat",...] true_l...
例如:一个二分类样本中0:1占比为9:1,当一个分类器将所有的样本都识别为0,此时模型的错误率为10%,准确率为90%,如果仅仅看错误率是比较低的,但是根本没有起到数据分类的效果。因此在机器学习中,有一个普遍适用的称为混淆矩阵(confusionmatrix)的工具,它可以帮助人们更好地了解分类中的错误。
from sklearn.metrics import plot_confusion_matrix plot_confusion_matrix(random_search, X_test, y_test, cmap=plt.cm.Greens, normalize='true') plt.show() ## 混淆矩阵 5 全文小结 本案例基于sklearn和pipline对数据进行建模; 还展示了CV、调参和混淆矩阵等常规操作; 以及随机森林算法; titanic数据类型...