python boolean numpy array_Python使用较小的布尔数组索引numpy数组

https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

If obj.ndim == x.ndim, x[obj] returns a 1-dimensional array filled

with the elements of x corresponding to the True values of obj. The

search order will be row-major, C-style. If obj has True values at

entries that are outside of the bounds of x, then an index error will

be raised. If obj is smaller than x it is identical to filling it with

False.

我从numpy参考资料中读到,我可以使用较小的布尔数组为较大的数组编制索引,其余条目将自动用False填充.

Example :

From an array, select all rows which sum up to less or equal two:

>>> x = np.array([[0, 1], [1, 1], [2, 2]])

>>> rowsum = x.sum(-1)

>>> x[rowsum <= 2, :]

array([[0, 1],[1, 1]])

But if rowsum would have two dimensions as well:

>>> rowsum = x.sum(-1, keepdims=True)

>>> rowsum.shape

(3, 1)

>>> x[rowsum <= 2, :] # fails

IndexError: too many indices

>>> x[rowsum <= 2]

array([0, 1])

The last one giving only the first elements because of the extra

dimension.

但是该示例根本不起作用,它说“ IndexError:布尔索引与沿维度1的索引数组不匹配;维度为2,但相应的布尔维度为1”

如何使其工作?我正在使用python 3.6.3和numpy 1.13.3.

eecb1938171e459d7194d9f93a77ef20.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值