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.
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.
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.
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.
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.
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]])
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