how to create confusion matrix for classification in tensorflow

穿精又带淫゛_ 提交于 2019-12-04 08:30:31
vega

You can simply use Tensorflow's confusion matrix. I assume y are your predictions, and you may or may not have num_classes (which is optional)

y_ = placeholder_for_labels # for eg: [1, 2, 4]
y = mycnn(...) # for eg: [2, 2, 4]

confusion = tf.confusion_matrix(labels=y_, predictions=y, num_classes=num_classes)

If you print(confusion), you get

  [[0 0 0 0 0]
   [0 0 1 0 0]
   [0 0 1 0 0]
   [0 0 0 0 0]
   [0 0 0 0 1]]

If print(confusion) is not printing the confusion matrix, then use print(confusion.eval(session=sess)). Here sess is the name of your TensorFlow session.

import tensorflow as tf     
y = [1, 2, 4]
y_ = [2, 2, 4]

con = tf.confusion_matrix(labels=y_, predictions=y )
sess = tf.Session()
with sess.as_default():
        print(sess.run(con))

The output is :

[[0 0 0 0 0]
[0 0 0 0 0]
[0 1 1 0 0]
[0 0 0 0 0]
[0 0 0 0 1]]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!