在用metrics.roc_curve()函数计算tpr的时候,出现tpr为nan的情况,主要是因为label里面没有正样本的标签。
下面是roc_curve()里面的一段源码,其中tps[-1]存放的是所有的正样本。同理,如果fpr出现nan的情况是因为label里面没有负样本。
if fps[-1] <= 0:
warnings.warn("No negative samples in y_true, "
"false positive value should be meaningless",
UndefinedMetricWarning)
fpr = np.repeat(np.nan, fps.shape)
else:
fpr = fps / fps[-1]
if tps[-1] <= 0:
warnings.warn("No positive samples in y_true, "
"true positive value should be meaningless",
UndefinedMetricWarning)
tpr = np.repeat(np.nan, tps.shape)
else:
tpr = tps / tps[-1]
我们来看个?,正样本的标签为2,负样本的标签为1:
from sklearn import metrics
import numpy as np
y = np.array([1,1,1,1])
scores = scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
因为样本中没有怎样本,所以tpr的值就为nan
tpr
Out[16]: array([nan, nan, nan])