2

How is it possible to delete all the columns that have the same values in a NumPy array?

For example if I have this matrix:

[0 1 2 3 1]  
[0 2 2 1 0]  
[0 4 2 3 4]  
[0 1 2 3 4]  
[0 1 2 4 5]

I want to get a new matrix that looks like this:

[1 3 1]  
[2 1 0]  
[4 3 4]  
[1 3 4]  
[1 4 5]

2 Answers 2

5

You can compare the array with the shifted version of itself, if all pairs are equal for a column, then the column contains only one unique value, which can be removed with boolean indexing:

a[:, ~np.all(a[1:] == a[:-1], axis=0)]

#array([[1, 3, 1],
#       [2, 1, 0],
#       [4, 3, 4],
#       [1, 3, 4],
#       [1, 4, 5]])
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming

import numpy
a = numpy.array([[0, 1, 2, 3, 1],
                 [0, 2, 2, 1, 0],
                 [0, 4, 2, 3, 4],
                 [0, 1, 2, 3, 4],
                 [0, 1, 2, 4, 5]])

then

b = a == a[0,:]   # compares first row with all others using broadcasting
# b: array([[ True,  True,  True,  True,  True],
#           [ True, False,  True, False, False],
#           [ True, False,  True,  True, False],
#           [ True,  True,  True,  True, False],
#           [ True,  True,  True, False, False]], dtype=bool)

using all along the rows acts as a row-wise and operation (thanks Divakar!):

c = b.all(axis=0)
# c: array([ True, False,  True, False, False], dtype=bool)

which you can use for boolean indexing

a[:, ~c]
Out: 
array([[1, 3, 1],
       [2, 1, 0],
       [4, 3, 4],
       [1, 3, 4],
       [1, 4, 5]])

As an ugly oneliner:

a[:, ~(a == a[0,:]).all(0)]

2 Comments

Good idea to start with. But, why not simply do : ~b.all(0) and use that to mask out columns? Should be more performant and also shorter than prod-reduce and type conversion.
@Divakar thanks, that's actually the command I was looking for :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.