lerner adams lerner adams - 1 month ago 5
Python Question

How to reduce the sparseness of a 2D numpy array?

I have a very sparse array, for illustration like the following:

arr = array([[0, 1, 0, 0, 0, 2],
[0, 0, 2, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 7, 0, 0, 4, 0]])


Since the columns indexed 0 and 3 are all zeros I want to delete them and get the result as this:

array([[1, 0, 0, 2],
[0, 2, 0, 0],
[0, 0, 0, 0],
[7, 0, 4, 0]])


I thought I can check every column by

for i in len(arr):
if arr[:, i] != 0:
newarr = np.column_stack((newarr, arr[:, i]))


But I encounter an error which teaches me to use a.all()..

Answer

Use a simple indexing, by picking the columns that have at least one non-zero item (using any() over the first axis):

In [9]: arr[:, arr.any(0)]
Out[9]: 
array([[1, 0, 0, 2],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [7, 0, 4, 0]])
Comments