Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to replace negative values with 0 and values greater than 1 with 1 in an array using Python?

I have a list (or some type of array) with nearly all values between 0 and 1, but I have the occasional value that is slightly negative or greater than 1.

list_values = [-0.01, 0, 0.5, 0.9, 1.0, 1.01]

I want to replace negatives with 0 and values greater than 1 with 1.

With only 1 condition, I would use np.where like this:

arr_values = np.where(pd.Series(list_values) < 0, 0, pd.Series(list_values))

To work with multiple conditions, I could define a function and then apply it using a lambda function:

def change_values(value):
    if value < 0:
        return 0
    elif value > 1:
        return 1
    else:
        return value

series_values = pd.Series(list_values).apply(lambda x: change_values(value=x))

Is there a faster way to accomplish this?

like image 832
Aaron England Avatar asked Dec 30 '22 14:12

Aaron England


2 Answers

You want to use np.clip:

>>> import numpy as np
>>> list_values = [-0.01, 0, 0.5, 0.9, 1.0, 1.01]
>>> arr = np.array(list_values)
>>> np.clip(arr, 0.0, 1.0)
array([0. , 0. , 0.5, 0.9, 1. , 1. ])

This is likely the fastest approach, if you can ignore the cost of converting to an array. Should be a lot better for larger lists/arrays.

Involving pandas in this operation isn't the way to go unless you eventually want a pandas data structure.

like image 114
juanpa.arrivillaga Avatar answered Jan 02 '23 02:01

juanpa.arrivillaga


This is a rather easy task with numpy.

import numpy as np

n = np.array([-0.01, 0, 0.5, 0.9, 1.0, 1.01])

n[n > 1] = 1
n[n < 0] = 0

>>> print(n)
    [0.  0.  0.5 0.9 1.  1. ]
like image 35
Camilo Martinez M. Avatar answered Jan 02 '23 02:01

Camilo Martinez M.