Using R, I am able to manually calculate [and plot] the AUC using the following code and for loop:
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))
I am attempting to write the same code in Python, but am running into the error "TypeError: 'Series' objects are mutable, thus they cannot be hashed"
I am very new to Python and am trying to become bilingual with R and Python. Can someone point me in the right direction in terms of solving this?
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])
The error seems to be occurring around the portion of code containing (i/100).