confusion matrix with threshold python

Viewed 3730

How to print the confusion matrix for a logistic regression if change the value of threshold between [0.5,0.6,0.9] once 0.5 and once 0.6 and so one

from sklearn.linear_model import LogisticRegression
import pandas as pd  
import numpy as np  
import matplotlib.pyplot as plt  

X = [[0.7,0.2],[0.9,0.4]]
y = [1,-1]

model = LogisticRegression()
model = model.fit(X,y)

threshold = [0.5,0.6,0.9]

CM = confusion_matrix(y_true, y_pred)

TN = CM[0][0]
FN = CM[1][0]
TP = CM[1][1]
FP = CM[0][1]
3 Answers

Let try this!

for i in threshold:
   y_predicted = model.predict_proba(X)[:1] > i
   print(confusion_matrix(y, y_predicted))

predict_proba() returns a numpy array of two columns. The first column is the probability that target=0 and the second column is the probability that target=1. That is why we add [:,1] after predict_proba() in order to get the probabilities of target=1

I think an easy approach in pseudo code (based a bit on python) would be:

1 - Predict a set of known value (X) y_prob = model.predict_proba(X) so you will get the probability per each input in X.

2 - Then for each threshold calculate the output. i.e. If y_prob > threshold = 1 else 0

3 - Now get the confussion matrix of each vector obtained.

If you need a deeper explanation on any point let me know!

def predict_y_from_treshold(model,X,treshold):  
    return np.array(list(map(lambda x : 1 if x > treshold else 0,model.predict_proba(X)[:,1])))
Related