## 手动计算Python中的AUC内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (369)

``````test = data.frame(cbind(dt\$DV, predicted_prob))
colnames(test)[1] = 'DV'
colnames(test)[2] = 'DV_pred_prob'

TP = rep(NA,101)
FN = rep(NA,101)
FP = rep(NA,101)
TN = rep(NA,101)
Sensitivity = rep(NA,101)
Specificity = rep(NA,101)
AUROC = 0

for(i in 0:100){
test\$temp = 0
test[test\$DV_pred_prob > (i/100),"temp"] = 1
TP[i+1] = nrow(test[test\$DV==1 & test\$temp==1,])
FN[i+1] = nrow(test[test\$DV==1 & test\$temp==0,])
FP[i+1] = nrow(test[test\$DV==0 & test\$temp==1,])
TN[i+1] = nrow(test[test\$DV==0 & test\$temp==0,])
Sensitivity[i+1] = TP[i+1] / (TP[i+1] + FN[i+1] )
Specificity[i+1] = TN[i+1] / (TN[i+1] + FP[i+1] )
if(i>0){
AUROC = AUROC+0.5*(Specificity[i+1] - Specificity[i])*(Sensitivity[i+1] +
Sensitivity[i])
}
}

data = data.frame(cbind(Sensitivity,Specificity,id=(0:100)/100))
``````

``````predictions = pd.DataFrame(predictions[1])
actual = pd.DataFrame(y_test)
test = pd.concat([actual.reset_index(drop=True), predictions], axis=1)
# Rename column Renew to 'actual' and '1' to 'predictions'
test.rename(columns={"Renew": "actual", 1: "predictions"}, inplace=True)

TP = np.repeat('NA', 101)
FN = np.repeat('NA', 101)
FP = np.repeat('NA', 101)
TN = np.repeat('NA', 101)
Sensitivity = np.repeat('NA', 101)
Specificity = np.repeat('NA', 101)
AUROC = 0

for i in range(100):
test['temp'] = 0
test[test['predictions'] > (i/100), "temp"] = 1
TP[i+1] = [test[test["actual"]==1 and test["temp"]==1,]].shape[0]
FN[i+1] = [test[test["actual"]==1 and test["temp"]==0,]].shape[0]
FP[i+1] = [test[test["actual"]==0 and test["temp"]==1,]].shape[0]
TN[i+1] = [test[test["actual"]==0 and test["temp"]==0,]].shape[0]
Sensitivity[i+1] = TP[i+1] / (TP[i+1] + FN[i+1])
Specificity[i+1] = TN[i+1] / (TN[i+1] + FP[i+1])
if(i > 0):
AUROC = AUROC+0.5*(Specificity[i+1] - Specificity[i])*
(Sensitivity[i+1] + Sensitivity[i])
``````

### 1 个回答

Pandas索引不按您期望的方式工作。您不能`df[rows, cols]`使用`.loc`https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html

`test[test['predictions'] > (i/100), "temp"] = 1`

`test.loc[test['predictions'] > (i/100), "temp"] = 1`

...然后你会遇到以下4行的问题：

`TP[i+1] = test[test["actual"]==1 and test["temp"]==1,].shape[0]`

`TP[i+1] = len(test[(test["actual"]==1) & (test["temp"]==1)])`

``````predictions = pd.DataFrame(predictions[1])
actual = pd.DataFrame(y_test)
test = pd.concat([actual.reset_index(drop=True), predictions], axis=1)

# Rename column Renew to 'actual' and '1' to 'predictions'

test.columns = ['actual', 'predictions'] #<- You can assign column names using a list

TP = np.zeros(101)
FN = np.zeros(101)
FP = np.zeros(101)
TN = np.zeros(101)
Sensitivity = np.zeros(101)
Specificity = np.zeros(101)
AUROC = 0

for i in range(10):
test['temp'] = 0
test.loc[test['predictions'] > (i / 100), 'temp'] = 1
TP[i+1] = len(test[(test["actual"]==1) & (test["temp"]==1)])
FN[i+1] = len(test[(test["actual"]==1) & (test["temp"]==0)])
FP[i+1] = len(test[(test["actual"]==0) & (test["temp"]==1)])
TN[i+1] = len(test[(test["actual"]==0) & (test["temp"]==0)])
Sensitivity[i+1] = TP[i+1] / (TP[i+1] + FN[i+1])
Specificity[i+1] = TN[i+1] / (TN[i+1] + FP[i+1])
if i > 0:
AUROC += 0.5 * (Specificity[i+1] - Specificity[i]) *  (Sensitivity[i+1] + Sensitivity[i])
``````