【问题标题】:Why the colorbar is not normalized (0 to 1)? How to force it?为什么颜色条未标准化(0 到 1)?怎么强求?
【发布时间】:2017-08-13 06:11:45
【问题描述】:

我正在绘制一个混淆矩阵。我使用了 ScikitLearn 的功能。但是不知道为什么colorbar没有0到1的范围,有没有办法强制呢?

import itertools
def plot_confusion_matrix(cm, title='Confusion matrix RF', cmap=plt.cm.viridis):
plt.imshow(cm, interpolation='nearest', cmap=cmap) 
plt.title(title)
plt.colorbar() 
tick_marks = np.arange(len(np.unique(y))) 

plt.xticks(tick_marks, rotation=90)
ax = plt.gca()
ax.set_xticklabels(['s'+lab for lab in (ax.get_xticks()+1).astype(str)])
plt.yticks(tick_marks)
ax.set_yticklabels(['s'+lab for lab in (ax.get_yticks()+1).astype(str)])

plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')




cm_imp = confusion_matrix(y_true, y_pred)
cm_imp_normalized = cm_imp.astype('float') / cm_imp.sum(axis=1)[:, np.newaxis] 
plt.figure(figsize=(8,6)) 
plot_confusion_matrix(cm_imp_normalized)
plt.show() 
print("")
print("")

【问题讨论】:

    标签: python-3.x matplotlib scikit-learn visualization confusion-matrix


    【解决方案1】:

    您可以使用vmin, vmax 参数将颜色范围设置为imshow

    plt.imshow(data, cmap=cmap, vmin=0, vmax=1) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2020-12-27
      • 1970-01-01
      • 2019-01-01
      • 2014-08-29
      • 1970-01-01
      • 2013-12-10
      相关资源
      最近更新 更多