I have a numpy array ( X
)
1 2 3 4
3 4 5 6
5 6 7 8
and a Booelan index array ( ind
)
0 1 1 0
I would like to slice the numpy array based on the index. ie The resultant array after silicing should only have columns 2 and 3 (with corresponding index 1):
2 3
4 5
6 7
I tried X[:,ind]
, but it didn't work.
You need to convert ind
to a boolean array
. An easy way to do this is by using numpy.array()
:
numpy.array(ind, dtype=bool)
You can then use X[:,ind]
You need to convert ind
to dtype: bool
.
This can be done by masking with ind == 1
.
That is to say,
>>> X[:,ind == 1]
array([[2, 3],
[4, 5],
[6, 7]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.