Tulkkas
Tulkkas

Reputation: 1013

Delete specific column in an array

I have an numpy array of size NxD called X.

I have created a mask of size D represented by a numpy vector with 1 and 0 called ind_delete

I would like to delete all column of X corresponding to 1 in ind_delete.

I have tried:

X = np.delete(X,ind_delete,1)

but it obviously does not work. I have tried to find an easy way to to that on python but as it is trivial in matlab, it seems not as much here. Thanks for pointing out the best way to achieve it.

Upvotes: 2

Views: 75

Answers (2)

Joe Iddon
Joe Iddon

Reputation: 20424

Boolean array indexing:

>>> x = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>> d = np.array([1, 0, 1])
>>> x[:, d==1]
array([[1, 3],
       [4, 6]])

Upvotes: 2

You need to create a boolean array and you can select what you want:

X = X[ind_delete!=1]

Selects the positions for where the value is not 1.

Upvotes: 0

Related Questions