Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

efficient way of removing None's from numpy array

Tags:

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]) 
like image 889
Michael WS Avatar asked Aug 12 '14 01:08

Michael WS


People also ask

How do I remove multiple elements from a NumPy array?

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.

How do I remove an element from a NumPy array?

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.

How do I get rid of none values?

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.

Is CuPy faster than NumPy?

50x Faster NumPy = CuPy.


2 Answers

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.

like image 60
John1024 Avatar answered Sep 18 '22 15:09

John1024


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.

like image 35
eric Avatar answered Sep 19 '22 15:09

eric