JaredS
JaredS

Reputation: 240

How to turn a list of probabilities into binary values

I have a list of probabilities between 0 and 1 stored in a NumPy array. How can I covert these values so that any values >0.5 become 1 and any <0.5 become 0?

What I have is basically:

    model = [0.123,0.789,0.456,0.654]  

and what I want is:

    model = [0,1,0,1]

This just changes the whole array to 1s and I also tried

I've tried doing it using loops:

for i in range(len(model)):
if i<0.5:
    model[i]=0
elif i>0.5:
    model[i]=1

This just changes the whole array to 1s and I also tried

i = 0
while i <len(model):
    if model[i] < 0.5:
        model[i] = 0
        i + 1
    elif model[i] >0.5:
        model[i] = 1
        i + 1

but that didn't work either.

Upvotes: 2

Views: 4089

Answers (5)

Rajat Jain
Rajat Jain

Reputation: 2032

Use np.where as follows:

list(np.where(np.array(model) > 0.5, 1, 0 ))

Also, a simple %timeit comparison of all above/below:

In [24]: %timeit np.round(model)                                                                                                                     
12.9 µs ± 69.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [25]: %timeit list(np.where(np.array(model) > 0.5, 1, 0 ))                                                                                        
10.7 µs ± 26.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [26]: %timeit [int(i > .5) for i in model]                                                                                                        
1.28 µs ± 5.02 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Upvotes: 3

sakost
sakost

Reputation: 267

Just use numpy.round function

import numpy as np
model = np.round(model)

Upvotes: 0

user2209008
user2209008

Reputation:

Your first example doesn't work because you're not evaluating the values of the model array (model[i]), you're evaluating the indices of the iterator (i).

Your second example doesn't work because i + 1 doesn't actually change the value of i. What you want is: i += 1.

There's also a subtle error in your second example, namely that a value of exactly 0.5 will be unchanged, which I assume is not wanted.

You can use Python's list comprehension syntax to easily convert the array to the format you want:

model = [random.random() for _ in range(5)]
model = [1 if n >= 0.5 else 0 for n in model]

Otherwise, here is a traditional loop that does the same thing:

for i in range(len(model)):
    if model[i] < 0.5:
        model[i] = 0
    else:
        model[i] = 1

Upvotes: 2

Chris Farr
Chris Farr

Reputation: 3759

Without any additional libraries, you could go with...

model = [int(i > .5) for i in model]

Upvotes: 6

vurmux
vurmux

Reputation: 10020

Use np.vectorize function:

model = np.array([0.123,0.789,0.456,0.654], dtype='float')
np.vectorize(lambda x: int(x >= 0.5))(model)

will return:

array([0, 1, 0, 1])

Upvotes: 1

Related Questions