Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and delete all-zero columns from Numpy array using fancy indexing

How do I find columns in a numpy array that are all-zero and then delete them from the array? I'm looking for a way to both get the column indices and then use those indices to delete.

like image 209
AAC Avatar asked Aug 09 '18 14:08

AAC


People also ask

How to remove all zeros from a NumPy N-D array?

Take a numpy n-d array. Remove rows that contain only zeroes using numpy.all () function. Print the n-d array. Take 20 random numbers between 0-10, using numpy.random.choice () method. Align them in rows and columns, using reshape () method. Explicitly mark some rows as completely 0. Remove rows having all zeroes. Print dataset.

How to access a NumPy array by specific column index?

Accessing a NumPy based array by specific Column index can be achieved by the indexing. Let’s discuss this in detail. NumPy follows standard 0 based indexing. For row : numpy_Array_name [row, …] Note: This is not a very practical method but one must know as much as they can.

How to determine the entire row containing 0’S in NumPy array?

Thus, to determine the entire row containing 0’s can be removed by specifying axis=1. It will traverse each row and will check for the condition given in first parameter. Take a numpy n-d array.

What is fancy indexing in Python?

Fancy indexing is like the simple indexing we've already seen, but we pass arrays of indices in place of single scalars. This allows us to very quickly access and modify complicated subsets of an array's values. Fancy indexing is conceptually simple: it means passing an array of indices to access multiple array elements at once.


1 Answers

You could use np.argwhere, with np.all to find your indices. To delete them, use np.delete.

Example:

Find your 0 columns:

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

idx = np.argwhere(np.all(a[..., :] == 0, axis=0))

>>> idx
array([[2],
       [4]])

Delete your columns

a2 = np.delete(a, idx, axis=1)

>>> a2
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
like image 193
sacuL Avatar answered Sep 28 '22 07:09

sacuL