Is there an efficient way to remove Nones from numpy arrays and resize the array to its new size?
For example, how would you remove the None from this frame without iterating through it in python. I can easily iterate through it but was working on an api call that would be potentially called many times.
a = np.array([1,45,23,23,1234,3432,-1232,-34,233,None])
The correct way to remove multiple elements is to: Add the indices to a sequence, such as a list. Call the numpy. delete() function on the array with the given index sequence.
To delete multiple elements from a numpy array by index positions, pass the numpy array and list of index positions to be deleted to np. delete() i.e. It deleted the elements at index position 1,2 and 3 from the numpy array. It returned a copy of the passed array by deleting multiple element at given indices.
The Python filter() function is the most concise and readable way to perform this particular task. It checks for any None value in list and removes them and form a filtered list without the None values.
50x Faster NumPy = CuPy.
In [17]: a[a != np.array(None)] Out[17]: array([1, 45, 23, 23, 1234, 3432, -1232, -34, 233], dtype=object)
The above works because a != np.array(None)
is a boolean array which maps out non-None values:
In [20]: a != np.array(None) Out[20]: array([ True, True, True, True, True, True, True, True, True, False], dtype=bool)
Selecting elements of an array in this manner is called boolean array indexing.
I use the following which I find simpler than the accepted answer:
a = a[a != None]
Caveat: PEP8 warns against using the equality operator with singletons such as None
. I didn't know about this when I posted this answer. That said, for numpy arrays I find this too Pythonic and pretty to not use. See discussion in comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With