+2 votes
in Programming Languages by (73.8k points)
I want to convert predicted probabilities to label 0 or 1 using threshold value 0.6 instead of 0.5 i.e. if the probability is >0.6, class label 1, otherwise 0. How can I do it?

E.g.

pred_probs = [0.12,0.34,0.54,0.98,0.67,0.21,0.43,0.47,0.78,0.90]

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

You can use if...else if your list is small. 

The following two approaches look more Pythonic. You can try one of them.

Approach 1

>>> pred_probs = [0.12,0.34,0.54,0.98,0.67,0.21,0.43,0.47,0.78,0.90]
>>> labels = [int(p>=0.6) for p in pred_probs]
>>> labels
[0, 0, 0, 1, 1, 0, 0, 0, 1, 1]

Approach 2

>>> import numpy as np
>>> labels=np.array(pred_probs)
>>> labels
array([0.12, 0.34, 0.54, 0.98, 0.67, 0.21, 0.43, 0.47, 0.78, 0.9 ])
>>> labels[labels>=0.6]=1
>>> labels[labels<0.6]=0
>>> labels
array([0., 0., 0., 1., 1., 0., 0., 0., 1., 1.])


...