一、引言
如果想弄懂AUC和ROC曲线,一定要彻底理解混淆矩阵的概念!!!
混淆矩阵中有Postitive(阳性)、Negative(阴性)、False(伪)、True(真)的概念
- 预测类别为0的为Negative(阴性),预测类别为1的为Postitive(阳性)
- 预测错误的为False(伪)、预测正确为True(真)
对上述概念进行组合,就有了混淆矩阵!
二、sklearn.metrics.roc_curve详解
sklearn中的metrics.roc_curve评价指标
三、sklearn.metrics.roc_auc_curve详解
sklearn中的metrics.roc_auc_score评价指标
四、鸢尾花数据集
import numpy as np
import pandas as pd
import matplotlib as mpl
import warnings
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import label_binarize
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegressionCV
from sklearn.svm import SVC
from itertools import cycle
from sklearn.metrics import roc_curve, roc_auc_score
# 获取数据
def loaddata():
data = pd.read_csv('data/iris.data', header=None)
# 将类别数据转换为数值数据
iris_types = data[4].unique()
for i, iris_type in enumerate(iris_types):
data._set_value(data[4] == iris_type, 4, i)
data = data.values
X = data[:, :2]
y = data[:, -1].astype(np.int)
return X, y
if __name__ == '__main__':
# 消除警告
warnings.filterwarnings(action='ignore')
# 设置样式格式
# 不用科学计数法,使用小数
np.set_printoptions(suppress=True)
# 设置显示宽度
pd.set_option('display.width', 400)
# 获取数据
X, y = loaddata()
# 获取类别个数
c = np.unique(y).size
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=0)
# 标签二值化—one-hot编码
y_one_hot = label_binarize(y_test, classes=np.arange(c))
# 逻辑回归C
alpha = np.logspace(-2, 2, 20)
# 模型
models = [
['Knn', KNeighborsClassifier(n_neighbors=7)],
['lgc', LogisticRegressionCV(Cs=alpha, fit_intercept=False, cv=5)],
['SVM(linear)', GridSearchCV(SVC(kernel='linear', decision_function_shape='ovr'), param_grid={'C': alpha})],
['SVM(RBF)',
GridSearchCV(SVC(kernel='rbf', decision_function_shape='ovr'), param_grid={'C': alpha, 'gamma': alpha})]
]
# 颜色迭代器
colors = cycle('gmcr')
# 画每个图的ROC曲线
mpl.rcParams['font.sans-serif'] = ['simHei']
mpl.rcParams['axes.unicode_minus'] = False
fig = plt.figure(facecolor='w', figsize=(7, 6))
for (name, model), color in zip(models, colors):
model.fit(X_train, y_train)
if hasattr(model, 'C_'):
print(model.C_)
if hasattr(model, 'best_params_'):
print(model.best_params_)
if hasattr(model, 'predict_proba'): # 概率
y_score = model.predict_proba(X_test)
else:
y_score = model.decision_function(X_test) # 决策函数(GridSearchCV中)
fpr, tpr, threshold = roc_curve(y_one_hot.ravel(), y_score.ravel())
auc = roc_auc_score(y_one_hot.ravel(), y_score.ravel())
plt.plot(fpr, tpr, c=color, lw=2, alpha=0.7, label='%s,AUC=%.3f' % (name, auc))
plt.plot((0, 1), (1, 0), c='#808080', lw=2, ls='--', alpha=0.7)
# 设置坐标范围
plt.xlim((-0.01, 1.02))
plt.ylim((-0.01, 1.02))
# 设置坐标精度
plt.xticks(np.arange(0, 1.1, 0.1))
plt.yticks(np.arange(0, 1.1, 0.1))
# 设置坐标标签
plt.xlabel('FPR', fontsize=13)
plt.ylabel('TPR', fontsize=13)
plt.grid(b=True, ls=':')
# 图例
plt.legend(loc='lower right', fontsize=12)
# 标题
plt.title('鸢尾花数据不同分类器的ROC与AUC', fontsize=17)
plt.show()
import numpy as np
import pandas as pd
import matplotlib as mpl
import warnings
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import label_binarize
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegressionCV
from sklearn.svm import SVC
from itertools import cycle
from sklearn.metrics import roc_curve, roc_auc_score, confusion_matrix
# 获取数据
def loaddata():
data = pd.read_csv('data/iris.data', header=None)
# 将类别数据转换为数值数据
iris_types = data[4].unique()
for i, iris_type in enumerate(iris_types):
data._set_value(data[4] == iris_type, 4, i)
data = data.values
X = data[:, :2]
y = data[:, -1].astype(np.int)
return X, y
if __name__ == '__main__':
# 消除警告
warnings.filterwarnings(action='ignore')
# 设置样式格式
# 不用科学计数法,使用小数
np.set_printoptions(suppress=True)
# 设置显示宽度
pd.set_option('display.width', 400)
# 获取数据
X, y = loaddata()
# 获取类别个数
c = np.unique(y).size
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=0)
# 标签二值化—one-hot编码
y_one_hot = label_binarize(y_test, classes=np.arange(c))
# print(y_one_hot)
# 逻辑回归C
alpha = np.logspace(-2, 2, 20)
# 模型
models = [
['Knn', KNeighborsClassifier(n_neighbors=7)],
['lgc', LogisticRegressionCV(Cs=alpha, fit_intercept=False, cv=5)],
['SVM(linear)', GridSearchCV(SVC(kernel='linear', decision_function_shape='ovr'), param_grid={'C': alpha})],
['SVM(RBF)',
GridSearchCV(SVC(kernel='rbf', decision_function_shape='ovr'), param_grid={'C': alpha, 'gamma': alpha})]
]
# 颜色迭代器
colors = cycle('gmcr')
# 画每个图的ROC曲线
mpl.rcParams['font.sans-serif'] = ['simHei']
mpl.rcParams['axes.unicode_minus'] = False
fig = plt.figure(facecolor='w', figsize=(7, 6))
for (name, model), color in zip(models, colors):
model.fit(X_train, y_train)
if hasattr(model, 'C_'):
print(model.C_)
if hasattr(model, 'best_params_'):
print(model.best_params_)
if hasattr(model, 'predict_proba'): # 概率
y_score = model.predict(X_test)
else:
y_score = model.predict(X_test) # 决策函数(GridSearchCV中)
confmat = confusion_matrix(y_test, y_score)
fig, ax = plt.subplots(figsize=(2.5, 2.5))
ax.matshow(confmat, cmap=plt.cm.Blues, alpha=0.3)
for i in range(confmat.shape[0]):
for j in range(confmat.shape[1]):
ax.text(x=j, y=i, s=confmat[i, j], va='center', ha='center')
plt.xlabel('predicted label')
plt.ylabel('true label')
plt.show()