Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

My goal is to take a list with my:

predictions = [0, 0.2, 0.9, 0.7]

If higher than 0.5 it should become 0 if not 1

I tried:

predictions = np.where(predictions>=0.5,1, 0).tolist()

But when taking first element it is:

[0]

And not only 0

What is the best way to do what I want?

1 Answer

0 votes
by (17.6k points)

You can use the np.array.round method:

>>> predictions = np.array([0, 0.2, 0.9, 0.7])

>>> predictions.round().tolist()

[0, 0, 1, 1]

>>> 

For getting a list comprehension, do this:

>>> predictions = [0, 0.2, 0.9, 0.7]

>>> [int(i >= 0.5) for i in predictions]

[0, 0, 1, 1]

Browse Categories

...