TensorFlow Estimator混淆矩阵

时间:2017-12-25 22:24:43

标签: python tensorflow

我是Tensorflow的新手,我刚刚运行了我的第一个神经网络分类器,我从https://www.tensorflow.org/get_started/estimator获得了代码。 它运作成功,但它只显示精度。 如何输出混淆矩阵?我只有2个标签。 1和0。

这是代码的最后一部分。它与链接相同。

  # Train model.
  classifier.train(input_fn=train_input_fn, steps=2000)

  # Define the test inputs
  test_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": np.array(test_set.data)},
      y=np.array(test_set.target),
      num_epochs=1,
      shuffle=True)

  # Evaluate accuracy.
  accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]

2 个答案:

答案 0 :(得分:3)

您可以使用tf.confusion_matrix生成混淆矩阵。特别是,以下内容应该有效:

labels = list(test_set.target)
predictions = list(classifier.predict(input_fn=test_input_fn))
confusion_matrix = tf.confusion_matrix(labels, predictions)

答案 1 :(得分:0)

可能还有一点。

labels = list(test_set[label_column])
raw_predictions = regressor.predict(input_fn=get_input_fn(test_set)
predictions = [p['class_ids'][0] for p in raw_predictions]
confusion_matrix = tf.confusion_matrix(labels, predictions)

原始预测输出是标签和概率的字典。你需要获得标签。

您可能还需要添加以下内容:

with tf.Session():
    print('\nConfusion Matrix:\n', tf.Tensor.eval(confusion_matrix,feed_dict=None, session=None))

为了打印矩阵