keras迁移学习中的混淆矩阵

问题描述

我打算在模型中绘制混淆矩阵,并使用了基于深度学习模型的转移学习概念。

混淆矩阵的代码

def plot_confusion_matrix(cm,classes,normalize=False,title='Confusion Matrix',cmap=plt.cm.Blues):
  plt.imshow(cm,interpolation='nearest',cmap=cmap)
  plt.title(title)
  plt.colorbar()
  tick_marks = np.arange(len(classes))
  plt.xticks(tick_marks,rotation=45)
  plt.yticks(tick_marks,classes)

  if normalize:
    cm=cm.astype('float') / cm.sum(axis=1)[:,np.newaxis]
    print("Normalized Confusion Matrix")
  else:
    print("Confusion matrix,without normalization")
  print(cm)

  thresh = cm.max() / 2
  for i,j in itertools.product(range(cm.shape[0]),range(cm.shape[1])):
    plt.text(j,i,cm[i,j],horizontalalignment="center",color="white" if cm[i,j] > thresh else "black")
  plt.tight_layout()
  plt.ylabel('True Label')
  plt.xlabel('Predicted Label')

现在在 test_labels 预测的形状下方,

test_labels.shape
(12,)
predictions.shape
(10,2)

上面的代码可以正常工作,但是我在下面看到错误。因此,请关注以下代码,

cm = confusion_matrix(test_labels,predictions.argmax(axis=1))

这是错误

ValueError                                Traceback (most recent call last)
<ipython-input-40-79fd4e2e074c> in <module>()
----> 1 cm = confusion_matrix(test_labels,predictions.argmax(axis=1))

2 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
    210     if len(uniques) > 1:
    211         raise ValueError("Found input variables with inconsistent numbers of"
--> 212                          " samples: %r" % [int(l) for l in lengths])
    213 
    214 

ValueError: Found input variables with inconsistent numbers of samples: [12,10]
  

注意:这是值错误,对此我感到困惑,我尝试了越来越多,但失败了。因此,我需要帮助来解决此错误。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)