真正类(TP),样本的真实类别是正类,并且模型预测的结果也是正类。
假负类(FN),样本的真实类别是正类,但是模型将其预测成为负类。
假正类(FP),样本的真实类别是负类,但是模型将其预测成为正类。
真负类(TN),样本的真实类别是负类,并且模型将其预测成为负类。
从混淆矩阵中,可以衍生出各种评价的指标:
可以借助sklearn中的confusion_matrix可以快速计算出混淆矩阵只需要:
from sklearn.metrics import confusion_matrix
在tensorflow中也只需要添加几行代码就可快速计算出混淆矩阵:
pred = BiRNN(x, Weight, bias) y_p = tf.argmax(pred, 1)#for confusion_matrix cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost, global_step=global_step) correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accurancy
........
test_data, test_label = fun.get_test_data(test_file_path) y_true = np.argmax(test_label, 1) acc_test, loss_test, y_pre = sess.run([accurancy, cost, y_p], feed_dict={x: test_data, y: test_label, keep_prob1: 1.0, keep_prob2: 1.0}) print('Loss= %.6f' % (loss_test) + ', Test Accurancy= %.5f' % (acc_test)) print("confusion_matrix") cm = confusion_matrix(y_true, y_pre) print(cm)
同时tensorflow中本身有tf.confusion_matrix()可以计算混淆矩阵,参数为:
tf.confusion_matrix(
labels,
predictions,
num_classes=None,
dtype=tf.int32,
name=None,
weights=None
)
以下摘自tensorflow API:
For example:
tf.confusion_matrix([1, 2, 4], [2, 2, 4]) ==>
[[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]]
Note that the possible labels are assumed to be [0, 1, 2, 3, 4]
, resulting in a 5x5 confusion matrix.
labels
: 1-D Tensor
of real labels for the classification task.predictions
: 1-D Tensor
of predictions for a given classification.num_classes
: The possible number of labels the classification task can have. If this value is not provided, it will be calculated using both predictions and labels array.dtype
: Data type of the confusion matrix.name
: Scope name.weights
: An optional Tensor
whose shape matches predictions
.A Tensor
of type dtype
with shape [n, n]
representing the confusion matrix, where n
is the number of possible labels in the classification task.