常用的sklearn在正常调用时只能用一维的pre和label计算得到ROC,但是二维及以上特征的没找到可以直接调用的库,因此自己写一个,不确定对不对。
`
import numpy as np
import matplotlib.pyplot as plt
import random
a1=random.sample(range(0,100),51)
b1=random.sample(range(0,100),51)
a2=random.sample(range(60,180),55)
b2=random.sample(range(50,150),55)
a=a1+a2
b=b1+b2
sp=[]
for i in range(len(a)):
sp.append([a[i], b[i]])
label1 = [0] * 51
label2 = [1] * 55
label = label1+label2
sort_a = sorted(list(set(a)))
sort_b = sorted(list(set(b)))
fin = [[]]*(len(set(a))+len(set(b))-1)
for numi,i in enumerate(sort_a ): # a阈值
for numj, j in enumerate(sort_b ): # b阈值
fin[numi+numj]=[(sp_result[0] >= i and sp_result[1] >= j) for sp_result in sp]
TP = 0
FP = 0
FN = 0
TN = 0
TPR = []
FPR = []
position =[]
for preid, pre in enumerate(fin):
for id, ele in enumerate(pre):
if label[id] == 1 and ele is True:
TP += 1
if label[id] == 0 and ele is True:
FP += 1
if label[id] == 1 and ele is False:
FN += 1
if label[id] == 0 and ele is False:
TN += 1
TPR.append(TP/(TP+FN))
FPR.append(FP/(FP+TN))
position.append([FP/(FP+TN),TP/(TP+FN)]) #[FPR,TPR]
TP = 0
FP = 0
FN = 0
TN = 0
position = np.array(position)
plt.plot(position[:, 0], position[:, 1], 'bo-')
plt.show()
`
代码中a1,b1为类别1的二维特征值,a2,b2为类别2的二维特征值。