多分类

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()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值