tensorflow 二分类、多分类指标评价

我用的是tensorflow 2.5,搜索网上的教程大部分说用的是tf.keras.metrics中的api,但是经过实验发现都用不了,如今tensorflow 2.5可能不支持这些api了。

于是我采用sklearn库的函数实现二分类问题和多分类问题的评价指标计算。

f1_score, precision_score, recall_score, accuracy_score

二分类问题

# binary classify
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score, accuracy_score
import matplotlib as mpl
import matplotlib.pyplot as plt

# 绘制正例ROC曲线
def plot_roc(name, labels, predictions, **kwargs):
  fp, tp, _ = sklearn.metrics.roc_curve(labels, predictions, pos_label=0)

  plt.plot(100*fp, 100*tp, label=name, linewidth=2, **kwargs)
  plt.xlabel('False positives [%]')
  plt.ylabel('True positives [%]')
  plt.xlim([-0.5,100.5])
  plt.ylim([-0.5,100.5])
  plt.grid(True)
  ax = plt.gca()
  ax.set_aspect('equal')
  plt.legend(loc='lower right')
  plt.savefig("./img/multi_roc.png")

loss,acc= model.evaluate(x_test,y_test)
test_predictions = model.predict(x_test)
true_labels=y_test.astype('uint8')
test_scores = 1-(test_predictions - test_predictions.min())/(test_predictions.max() - test_predictions.min())
    
mpl.rcParams['figure.figsize'] = (12, 10)
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
plot_roc("My Model", true_labels, test_scores, color=colors[0])

recall = recall_score(true_labels,test_predictions.round())
f1 = f1_score(true_labels,test_predictions.round())
precision = precision_score(true_labels,test_predictions.round())
print('accuracy: ',acc)
print('loss: ',loss)
print('recall: ',recall)
print('precision: ',precision)
print('f1: ',f1)

多分类问题

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import sklearn
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score, accuracy_score

# 绘制正例ROC曲线
def plot_roc(name, labels, predictions, **kwargs):
  fp, tp, _ = sklearn.metrics.roc_curve(labels, predictions, pos_label=0)

  plt.plot(100*fp, 100*tp, label=name, linewidth=2, **kwargs)
  plt.xlabel('False positives [%]')
  plt.ylabel('True positives [%]')
  plt.xlim([-0.5,100.5])
  plt.ylim([-0.5,100.5])
  plt.grid(True)
  ax = plt.gca()
  ax.set_aspect('equal')
  plt.legend(loc='lower right')
  plt.savefig("./img/multi_roc.png")

y_pred = model.predict(x_test)
test_predictions = np.argmax(y_pred, axis=1)
y_true=y_test.astype('uint8')
test_scores = 1-(test_predictions - test_predictions.min())/(test_predictions.max() - test_predictions.min())

mpl.rcParams['figure.figsize'] = (12, 10)
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
plot_roc("My Model", y_test, test_scores, color=colors[0])

acc = accuracy_score(y_true,test_predictions)
recall = recall_score(y_true,test_predictions,average='micro')
precision = precision_score(y_true,test_predictions,average='micro')
f1 = f1_score(y_true,test_predictions,average='micro')
print('accuracy: ',acc)
print('recall: ',recall)
print('precision: ',precision)
print('f1: ',f1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值