tensorflow⼆分类、多分类指标评价
我⽤的是tensorflow 2.5,搜索⽹上的教程⼤部分说⽤的是ics中的api,但是经过实验发现都⽤不了,如今tensorflow 2.5可能不⽀持这些api了。
于是我采⽤sklearn库的函数实现⼆分类问题和多分类问题的评价指标计算。
f1_score, precision_score, recall_score, accuracy_score
⼆分类问题
# binary classify
ics 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, _ = _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])
ax = a()
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())
tensorflow入门教程colors = Params['axes.prop_cycle'].by_key()['color']
plot_roc("My Model", true_labels, test_scores, color=colors[0])
recall = recall_score(true_labels,und())
f1 = f1_score(true_labels,und())
precision = precision_score(true_labels,und())
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
ics import confusion_matrix, f1_score, precision_score, recall_score, accuracy_score
# 绘制正例ROC曲线
def plot_roc(name, labels, predictions, **kwargs):
fp, tp, _ = _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])
ax = a()
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())
colors = Params['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)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。