How to subset Numpy array with exclusion
One way is to generate the index list yourself:
>>> a[:,list(i for i in range(a.shape[1]) if i not in set((2,1,3,4)))]
array([[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.]])
or to exclude a single column (following your edit):
>>> a[:,list(i for i in range(a.shape[1]) if i != 1)]*= 0
or if you use this often, and want to use a function (which will not be called except
, since that is a Python keyword:
def exclude(size,*args):
return [i for i in range(size) if i not in set(args)] #Supports multiple exclusion
so now
a[:,exclude(a.shape[1],1)]
works.
@jdehesa mentions from Numpy 1.13 you can use
a[:, np.isin(np.arange(a.shape[1]), [2, 1, 3, 4], invert=True)]
as well for something within Numpy itself.