false在python什么意思_python – `x [False]`在numpy中做什么?

从技术上讲,没有要求掩码的维度与您索引的数组的维度相匹配. (在以前的版本中,限制更少,你可以摆脱一些极端的形状不匹配.)

docs将布尔索引描述为

A single boolean index array is practically identical to x[obj.nonzero()] where, as described above, obj.nonzero() returns a tuple (of length obj.ndim) of integer index arrays showing the True elements of obj.

但非零对于0维输入来说很奇怪,所以这种情况是“几乎完全相同”的方式之一并不相同:

the nonzero equivalence for Boolean arrays does not hold for zero dimensional boolean arrays.

NumPy有一个0维布尔索引的特例,其动机是希望产生以下行为:

In [3]: numpy.array(3)[True]

Out[3]: array([3])

In [4]: numpy.array(3)[False]

Out[4]: array([], dtype=int64)

我将在源代码中引用处理0维布尔索引的comment:

if (PyArray_NDIM(arr) == 0) {

/*

* This can actually be well defined. A new axis is added,

* but at the same time no axis is "used". So if we have True,

* we add a new axis (a bit like with np.newaxis). If it is

* False, we add a new axis, but this axis has 0 entries.

*/

虽然这主要用于0维数组的0维索引,但它也适用于使用布尔值索引多维数组.从而,

x[True]

相当于x [np.newaxis],产生一个前面有一个新的长度为1的轴的结果,和

x[False]

在长度为0的前面生成一个新轴的结果,不选择任何元素.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值