多分类

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import roc_auc_score, roc_curve, f1_score, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
import matplotlib.pyplot as plt
import numpy as np
import warnings

warnings.filterwarnings('ignore')

iris = load_iris()
x = iris.data
y = iris.target

y_one_hot = label_binarize(y, np.arange(len(iris.target_names)))

x_train, x_test, y_train, y_test = train_test_split(x, y_one_hot, test_size=0.2, random_state=2)

clf = LogisticRegression()
clf = OneVsRestClassifier(clf)
clf.fit(x_train, y_train)

y_pred = clf.predict(x_test)

print('f1_score: ', f1_score(y_test, y_pred, average='micro'))
print('roc_auc_score: ', roc_auc_score(y_test, y_pred, average='micro'))

fpr, tpr, _ = roc_curve(y_test.ravel(), y_pred.ravel())
auc = auc(fpr, tpr)

plt.plot(fpr, tpr, c='r', alpha=0.7, label=u'AUC=%.3f' % auc)
plt.plot((0, 1), (0, 1), c='gray', ls='--', alpha=0.7)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.xlim((-0.01, 1.02))
plt.ylim((-0.01, 1.02))
plt.grid(b=True, ls=':')
plt.legend()
plt.title('ROC CURVE')
plt.show()

在这里插入图片描述

发布了51 篇原创文章 · 获赞 74 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/weixin_44766179/article/details/93752519