Search code examples
pythonconfusion-matrix

Model performance calculation when class labels are not as expected


Hi I'm training a neural network. The training dataset has its labels as benign or malignant. So I coveted it into numerical values using,

class_data= pd.factorize(class_data)[0]

So now the malignant has been given-0 (which is cancerous) and benign - 1 (non-cancerous)

Now the confusion matrix looked like below

enter image description here I need to calculate sensitivity, specificity. And it was calculated as below

tn, fp, fn, tp = confusion_matrix(test_y,y_pred).ravel()

# Accuracy : 
acc_ = (tp + tn) / (tp + tn + fn + fp)
print("Accuracy  : ", acc_)
# Sensitivity : 
sens_ = tp / (tp + fn)
print("Sensitivity  : ", sens_)
# Specificity 
sp_ = tn / (tn + fp)
print("Specificity  : ", sp_)
# False positive rate (FPR)
FPR = fp / (tn + fp)
print("False positive rate  : ", FPR)

Since my class labels are incorrectly labeled, can someone let me know the calculations are getting miss interpreted? PS:

...tn... 29
...fp... 15
...fn... 14
...tp... 85

Solution

  • To make sure your calculation is correct you can find F1 score manually as

    F1Score= 2tp/(2tp+fp+fn)
    

    Then compare your value with

    sklearn.metrics.f1_score(test_y, y_pred)
    

    You can also use the labels parameter to make sure the labels are correct.

    confusion_matrix(test_y,y_pred,labels=[0,1]).ravel()