赞
踩
我用的是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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。