使用Imshow和cmap绘制不等列表/数组形状的值

时间:2017-01-10 00:49:16

标签: python-2.7 numpy matplotlib confusion-matrix

我想将我的价值观绘制为:

enter image description here

我在列表中有4x12个值。

values = [[2.78, 2.78, 2.78, 2.79, 2.79, 2.79, 2.8, 2.8, 2.81, 2.82, 2.82, 2.77], 
          [2.98, 2.98, 2.98, 2.99, 2.99, 2.99, 3.0, 3.0, 3.01, 3.02, 3.03, 2.98], 
          [3.01, 3.01, 3.01, 3.01, 3.01, 3.02, 3.02, 3.03, 3.04, 3.04, 3.05, 3.01], 
          [2.98, 2.98, 2.98, 2.99, 2.99, 2.99, 3.0, 3.01, 3.01, 3.02, 3.03, 2.99]]

我试图遵循混淆矩阵方法,但它似乎仅限于相同的数组形状Confusion Matrix with number of classified/misclassified instances on it (Python/Matplotlib)

任何建议?

1 个答案:

答案 0 :(得分:1)

只需复制您提出的问题的代码:Confusion Matrix with number of classified/misclassified instances on it (Python/Matplotlib)

from numpy import *
import matplotlib.pyplot as plt
from pylab import *

conf_arr = [[2.78, 2.78, 2.78, 2.79, 2.79, 2.79, 2.8, 2.8, 2.81, 2.82, 2.82, 2.77], 
          [2.98, 2.98, 2.98, 2.99, 2.99, 2.99, 3.0, 3.0, 3.01, 3.02, 3.03, 2.98], 
          [3.01, 3.01, 3.01, 3.01, 3.01, 3.02, 3.02, 3.03, 3.04, 3.04, 3.05, 3.01], 
          [2.98, 2.98, 2.98, 2.99, 2.99, 2.99, 3.0, 3.01, 3.01, 3.02, 3.03, 2.99]]

norm_conf = []
for i in conf_arr:
        a = 0
        tmp_arr = []
        a = sum(i,0)
        for j in i:
                tmp_arr.append(float(j)/float(a))
        norm_conf.append(tmp_arr)

plt.clf()
fig = plt.figure(figsize=(14, 5))
ax = fig.add_subplot(111)
#res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')
#cb = fig.colorbar(res)

res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')
for i, cas in enumerate(conf_arr):
    for j, c in enumerate(cas):
        if c>0:
            plt.text(j-.2, i+.2, c, fontsize=14)
cb = fig.colorbar(res)

enter image description here