x [False]`在numpy中做什么? [英] What does `x[False]` do in numpy?

查看:139
本文介绍了x [False]`在numpy中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个数组x = np.arange(6).reshape(3, 2).

x[False]x[np.asanyarray(False)]是什么意思?两者都导致array([], shape=(0, 3, 2), dtype=int64),这是意外的.

What is the meaning of x[False], or x[np.asanyarray(False)]? Both result in array([], shape=(0, 3, 2), dtype=int64), which is unexpected.

由于口罩尺寸不合适,我希望得到一个IndexError,例如x[np.ones((2, 2), dtype=np.bool)]之类的东西.

I expected to get an IndexError because of an improperly sized mask, as from something like x[np.ones((2, 2), dtype=np.bool)].

此行为 x[True]x[np.asanyarray(True)]是一致的,因为这两者都会导致附加的维度:array([[[0, 1], [2, 3], [4, 5]]]).

This behavior is consistent for x[True] and x[np.asanyarray(True)], as both result in an additional dimension: array([[[0, 1], [2, 3], [4, 5]]]).

我正在使用numpy 1.13.1.看来行为最近有所改变,因此很高兴能获得旧版本的答案,但请在答案中提及您的版本.

I am using numpy 1.13.1. It appears that the behavior has changed recently, so while it is nice to have answers for older versions, please mention your version in the answers.

编辑

出于完整性考虑,我提出了 https://github.com/numpy/numpy/issues /9515 基于对该问题的评论.

Just for completeness, I filed https://github.com/numpy/numpy/issues/9515 based on the commentary on this question.

编辑2

并立即将其关闭.

推荐答案

从技术上讲,掩码的维数与您为其索引的数组的维数不匹配. (在以前的版本中,限制甚至更少,您可以避免一些极端的形状不匹配.)

There's technically no requirement that the dimensionality of a mask match the dimensionality of the array you index with it. (In previous versions, there were even fewer restrictions, and you could get away with some extreme shape mismatches.)

docs 将布尔索引描述为

单个布尔索引数组实际上与x [obj.nonzero()]相同,如上所述,其中obj.nonzero()返回整数索引数组(长度为obj.ndim)的元组,该数组显示了的True元素. obj.

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.

但是nonzero对于0维输入是很奇怪的,因此这种情况是实际上相同"结果不相同的一种方式:

but nonzero is weird for 0-dimensional input, so this case is one of the ways that "practically identical" turns out to be not identical:

布尔数组的非零等价不适用于零维布尔数组.

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

NumPy具有0维布尔索引的特殊情况,其动机是希望具有以下行为:

NumPy has a special case for a 0-dimensional boolean index, motivated by the desire to have the following behavior:

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

In [4]: numpy.array(3)[False]
Out[4]: array([], dtype=int64)

我将引用 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维索引,但它也适用于使用布尔值索引多维数组.因此,

While this is primarily intended for a 0-dimensional index to a 0-dimensional array, it also applies to indexing multidimensional arrays with booleans. Thus,

x[True]

等效于x[np.newaxis],产生的结果是在前面有一个新的length-1轴,并且

is equivalent to x[np.newaxis], producing a result with a new length-1 axis in front, and

x[False]

产生一个结果,其新轴的长度为0,不选择任何元素.

produces a result with a new axis in front of length 0, selecting no elements.

这篇关于x [False]`在numpy中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆