+1 vote
in Programming Languages by (9k 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

0 votes
by (18k points)

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.])

...