How do I remove rows in a list containing numpy arrays based on a condition?

Viewed 125

I have the following numpy array arr_split:

import numpy as np

arr1 = np.array([[1.,2,3], [4,5,6], [7,8,9]])

arr_split = np.array_split(arr1,
                           indices_or_sections = 4,
                           axis = 0)

arr_split

Output:

[array([[1., 2., 3.]]),
 array([[4., 5., 6.]]),
 array([[7., 8., 9.]]),
 array([], shape=(0, 3), dtype=float64)]

How do I remove rows which are "empty" (ie. in the above eg., it's the last row). The array arr_split can have any number of "empty" rows. The above eg. just so happens to have only one row which is "empty".

I have tried using list comprehension, as per below:

arr_split[[(arr_split[i].shape[0] != 0) for i in range(len(arr_split))]]

but this doesn't work because the list comprehension [(arr_split[i].shape[0] != 0) for i in range(len(arr_split))] part returns a list, when I actually just need the elements in the list to feed into arr_split[] as indices.

Anyone know how I could fix this or is there another way of doing this? If possible, looking for the easiest way of doing this without too many loops or if statements.

3 Answers

you can change the indices_or_sections value to length of the first axis, this will prevent any empty arrays from being produced

import numpy as np

arr1 = np.array([[1.,2,3], [4,5,6], [7,8,9]])

arr_split = np.array_split(arr1,
                           indices_or_sections = arr1.shape[0],
                           axis = 0)

arr_split

>>> [
    array([[1., 2., 3.]]),
    array([[4., 5., 6.]]),
    array([[7., 8., 9.]])
]

Just loop through and check the size. Only add them to the new list if they have a size greater than 0.

arr_split_new = [arr for arr in arr_split if arr.size > 0]

You can use enumerate to get the indexes and size to check if empty

indexes = [idx for idx, v in enumerate(arr_split) if v.size != 0]

[0, 1, 2]

Related