混淆矩阵

在最近做表情识别的时候,需要用到混淆矩阵来评估模型的效果,也顺便回忆一下机器学习中的模型评价指标:

True Positive

真正类(TP),样本的真实类别是正类,并且模型预测的结果也是正类。

False Negative

假负类(FN),样本的真实类别是正类,但是模型将其预测成为负类。

False Positive

假正类(FP),样本的真实类别是负类,但是模型将其预测成为正类。

True Negative

真负类(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.

Args:
  • 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.
Returns:

Tensor of type dtype with shape [n, n] representing the confusion matrix, where n is the number of possible labels in the classification task.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值