至少指定了一个标签y_true

时间:2018-09-10 03:47:14

标签: python deep-learning

我想根据y_test和pred_test得到一个混淆矩阵,但提出一个问题“至少指定一个标签必须在y_true中”,我不知道为什么

library(dplyr)
c <- a %>% semi_join(b, by = c("x", "y"))

> c
  x y   z
1 a 1 100
2 d 8 645

我创建了一个卷积神经网络。建模并使用交叉验证进行估计,最后生成一个混淆矩阵。现在在生成混淆矩阵方面存在问题。

数据集为enter link description here。完整代码如下:

metrics.confusion_matrix(np.argmax(y_test,axis=1),pred_test)


  y_test =  [[0. 1. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 1.]
     [0. 0. 0. 0. 1. 0.]
     ...
     [0. 0. 0. 1. 0. 0.]
     [0. 0. 1. 0. 0. 0.]
     [0. 0. 1. 0. 0. 0.]]

   pred_test = [1 4 5 ... 3 2 2]
   np.argmax(y_test,axis=1) = [1 5 4 ... 3 2 2]

  File "D:\Anaconda\lib\site-packages\sklearn\metrics\classification.py", line 259, in confusion_matrix
    raise ValueError("At least one label specified must be in y_true")
ValueError: At least one label specified must be in y_true

1 个答案:

答案 0 :(得分:2)

错误不在您的主代码中,而是在符号的定义中。当您将符号定义为

 sign = ['DOWNSTAIRS','JOGGING','SITTING','STANDING','UPSTAIRS','WALKING']

系统无法读取您的标签,因为它正在寻找标签0、1、2、3、4、5,这是错误所要表达的意思,即在y_pred登录中找不到任何标签。 将符号更改为

 sign = [1,2,3,4,5]

应修复该错误。至于您现在要做的事情,它非常简单,只需将结果映射为该数组,然后在实际的预测(部署)期间将标签的数字值换掉即可。

相关问题