Generate array from comparison

Question:

I want TPP to be an array with the TPP value for each threshold.
The print should be like: TPP is: n1, n2…

threshold=[0, 6, 15]
df=[3,13,19,21]
y_true=[0,0,1,0]
y_pred=np.where(df<threshold,1,0)
cm=confusion_matrix(y_true,y_pred)
TP=cm[0,0]
FN=cm[0,1]
TPP=TP/(TP+FN)
print('TPP is:',TPP)
Asked By: Fernando Quintino

||

Answers:

It seems to me that this code achieves your goal:

from sklearn.metrics import confusion_matrix
import numpy as np

threshold = [0, 6, 15, 20]
df        = [3,13,19,21]
y_true    = [0,0,1,0]

print('TPP is:', end=' ')
for idx, i in enumerate(threshold):
    y_pred = np.where(np.array(df) < i,1,0)
    are_ones = y_true == np.ones(len(y_true))
    predicted_as_zero = y_pred == np.zeros(len(y_true))
    TP = sum( are_ones   & np.array(y_pred) )
    FN = sum( are_ones & predicted_as_zero )
    if idx+1 == len(threshold):
        print(TP/(TP+FN), end='')
    else:
        print(TP/(TP+FN), end=', ')
Answered By: Alexander Martins
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.