如何打印大维度的混淆矩阵

Posted

技术标签:

【中文标题】如何打印大维度的混淆矩阵【英文标题】:How to print a big dimension of confusion matrix 【发布时间】:2019-04-27 14:39:51 【问题描述】:

我正在尝试打印一个混淆矩阵,但得到了它的部分打印(使用 ...),我如何强制它打印我的维度 = 100 的所有值?

在训练过程中打印准确率,最后打印混淆矩阵。

尝试了两种打印混淆矩阵的方法:sklearn 和 tensorflow,但都没有打印完整的矩阵输出

混淆矩阵:

0 0 0 ... 0 0 0

1 0 0 ... 0 0 0

0 1 0 ... 0 0 0

...

0 1 0 ... 0 0 0

0 0 1 ... 0 0 0

0 0 0 ... 0 0 0

我是深度学习的新手,正在尝试实现 fizzbuzz 游戏的 tensorflow 示例。 给定一个从 1 到 100 的数字数组,需要打印: - 所有 i mod 3==0 的嘶嘶声 - 所有 i mod 5==0 的嗡嗡声 - 所有 i mod 15 的 fizzbuzz==0

import numpy as np
import tensorflow as tf
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report

NUM_DIGITS = 10
VICTOR_SIZE = 4
# Represent each input by an array of its binary digits.
def binary_encode(i, num_digits):
    # print (np.array([i >> d & 1 for d in range(num_digits)]))
    return np.array([i >> d & 1 for d in range(num_digits)])

# One-hot encode the desired outputs: [number, "fizz", "buzz", "fizzbuzz"]
def fizz_buzz_encode(i):
    if   i % 15 == 0: return np.array([0, 0, 0, 1])
    elif i % 5  == 0: return np.array([0, 0, 1, 0])
    elif i % 3  == 0: return np.array([0, 1, 0, 0])
    else:             return np.array([1, 0, 0, 0])


# Our goal is to produce fizzbuzz for the numbers 1 to 100. So it would be
# unfair to include these in our training data. Accordingly, the training data
# corresponds to the numbers 101 to (2 ** NUM_DIGITS - 1).
trX = np.array([binary_encode(i, NUM_DIGITS) for i in range(101, 2 ** NUM_DIGITS)])
trY = np.array([fizz_buzz_encode(i)          for i in range(101, 2 ** NUM_DIGITS)])

# We'll want to randomly initialize weights.
def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

# Our model is a standard 1-hidden-layer multi-layer-perceptron with ReLU
# activation. The softmax (which turns arbitrary real-valued outputs into
# probabilities) gets applied in the cost function.
def model(X, w_h, w_o):
    h = tf.nn.relu(tf.matmul(X, w_h))
    return tf.matmul(h, w_o)

# Our variables. The input has width NUM_DIGITS, and the output has width 4.
X = tf.placeholder("float", [None, NUM_DIGITS])
Y = tf.placeholder("float", [None, VICTOR_SIZE])

# How many units in the hidden layer.
NUM_HIDDEN = 100

# Initialize the weights.
w_h = init_weights([NUM_DIGITS, NUM_HIDDEN])
w_o = init_weights([NUM_HIDDEN, VICTOR_SIZE])

# Predict y given x using the model.
py_x = model(X, w_h, w_o)

# We'll train our model by minimizing a cost function.
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y)) ##TODO
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = py_x, labels=Y))
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) #consider adam optimizer

# And we'll make predictions by choosing the largest output.
predict_op = tf.argmax(py_x, 1)

# Finally, we need a way to turn a prediction (and an original number)
# into a fizz buzz output
def fizz_buzz(i, prediction):
    return [str(i), "fizz", "buzz", "fizzbuzz"][prediction]


def analyze_results(actual, predicted):
    #print("\nAnalyze results.....................................................")

    #print("Accuracy Score :", accuracy_score(actual, predicted, normalize=False))
    print("Confusion Matrix :\n", confusion_matrix(actual, predicted))
    # to evaluate the accuracy of a classification (performance of classifier)
    # print("Report :", classification_report(actual, predicted))


BATCH_SIZE = 128 #consider smaller batches

# Launch the graph in a session
with tf.Session() as sess:
    ##TODO
    #tf.initialize_all_variables().run()
    sess.run(tf.global_variables_initializer())

    for epoch in range(10000):
        # Shuffle the data before each training iteration.
        p = np.random.permutation(range(len(trX))) #random order of input
        trX, trY = trX[p], trY[p]

        # Train in batches of 128 inputs.
        for start in range(0, len(trX), BATCH_SIZE):
            end = start + BATCH_SIZE
            sess.run(train_op, feed_dict=X: trX[start:end], Y: trY[start:end])

        # And print the current accuracy on the training data.
        print("current accuracy on the training data: ", epoch, np.mean(np.argmax(trY, axis=1) ==
                             sess.run(predict_op, feed_dict=X: trX, Y: trY)))

    # And now for some fizz buzz
    numbers = np.arange(1, 101)

    teX = np.transpose(binary_encode(numbers, NUM_DIGITS))
    teY = sess.run(predict_op, feed_dict=X: teX)
    output = np.vectorize(fizz_buzz)(numbers, teY)

    print("\nOutput = ", output)

    # print("\naccuracy = ", np.mean(np.argmax(trY, axis=1) == sess.run(predict_op, feed_dict=X: trX, Y: trY)))

    # conf_matrix = tf.confusion_matrix(labels=numbers, predictions=teY) #, num_classes=None, dtype=None, name=None)
    # print("\nConfusion Matrix :\n", sess.run(conf_matrix, feed_dict=X: trX, Y: trY))
    # print("\nConfusion Matrix :\n", confusion_matrix(numbers, teY)) # Using Sklearn


    analyze_results(numbers, teY)

【问题讨论】:

【参考方案1】:

您是否尝试过使用 numpy.set_printoptions ?

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)

【讨论】:

再想想有没有办法以视觉方式(作为情节)打印它。我得到了很多 0/1 并且阅读起来不舒服:)

以上是关于如何打印大维度的混淆矩阵的主要内容,如果未能解决你的问题,请参考以下文章

如何打印混淆矩阵的标签和列名?

Scikit 学习如何打印混淆矩阵的标签?

带有阈值python的混淆矩阵

如何获取多类的所有混淆矩阵术语(TPR、FPR、TNR、FNR)?

无法打印正确的混淆矩阵,并且热图中的值也在示例 2e+2、e+4 等中打印

使用 cross_validate 生成混淆矩阵