在numpy的投掷例外逻辑指数 [英] Logical indices in numpy throwing exception

查看:1644
本文介绍了在numpy的投掷例外逻辑指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一些code使用逻辑numpy的数组索引一个更大的数组,类似于MATLAB是如何允许使用逻辑阵列数组索引。

I am trying to write some code that uses logical numpy arrays to index a larger array, similar to how MATLAB allows array indexing with logical arrays.

import numpy as np
m = 4
n = 4
unCov = np.random.randint(10, size = (m,n) )
rowCov = np.zeros( m, dtype = bool )
colCov = np.ones( n, dtype = bool )
>>> unCov[rowCov, rowCov] 
[]  # as expected
>>> unCov[colCov, colCov]
[0 8 3 3]  # diagonal values of unCov, as expected
>>> unCov[rowCov, colCov]
ValueError: shape mismatch: objects cannot be broadcast to a single shape

有关这最后的评价,我期望一个空数组,类似于MATLAB返回。我宁愿没有检查rowCov / colCov索引之前,真正的元素。这是怎么回事,和有没有更好的方式来做到这一点?

For this last evaluation, I expected an empty array, similar to what MATLAB returns. I'd rather not have to check rowCov/colCov for True elements prior to indexing. Why is this happening, and is there a better way to do this?

推荐答案

据我了解,numpy的将您的2D逻辑指标转化为实际的指数向量:改编[真,假],[假,真] 将成为改编[] 的形状0,1的 ndarray (2,2)。然而,在你最后的情况下,第二个索引数组已满,因此它对应于长度为0的索引数组这与其他全是真索引向量,对应长度为4的数组索引。

As I understand it, numpy will translate your 2d logical indices to actual index vectors: arr[[True,False],[False,True]] would become arr[0,1] for an ndarray of shape (2,2). However, in your last case the second index array is full False, hence it corresponds to an index array of length 0. This is paired with the other full True index vector, corresponding to an index array of length 4.

从<一个href=\"http://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html#indexing-multi-dimensional-arrays\"相对=nofollow>在 numpy的手动:

如果索引阵列不具有相同的形状,有企图
  它们广播给相同的形状。如果它们不能被广播到
  相同的形状,将引发异常:

If the index arrays do not have the same shape, there is an attempt to broadcast them to the same shape. If they cannot be broadcast to the same shape, an exception is raised:

在您的情况下,该错误正是由于这一点:

In your case, the error is exactly due to this:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1411-28e41e233472> in <module>()
----> 1 unCov[colCov,rowCov]

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (4,) (0,)

MATLAB,在另一方面,如果自动索引数组是沿任意给定尺寸空,则返回一个空数组。

MATLAB, on the other hand, automatically returns an empty array if the index array is empty along any given dimension.

这实际上是强调了MATLAB和numpy的逻辑索引之间的根本区别。在MATLAB中,下标索引向量总是切出一个子数组。即,既

This actually highlights a fundamental difference between the logical indexing in MATLAB and numpy. In MATLAB, vectors in subscript indexing always slice out a subarray. That is, both

arr([1,2],[1,2])

arr([true,true],[true,true])

将返回 2×2 矩阵的子矩阵改编。如果逻辑索引矢量比阵列的给定的尺寸短,缺少的索引元件被假定为。有趣的事实:指数向量还可以的的比给定尺寸,只要多余的元素都是。所以上面的也相当于

will return the 2 x 2 submatrix of the matrix arr. If the logical index vectors are shorter than the given dimension of the array, the missing indexing elements are assumed to be false. Fun fact: the index vector can also be longer than the given dimension, as long as the excess elements are all false. So the above is also equivalent to

arr([true,true,false,false],[true,true])

arr([true,true,false,false,false,false,false],[true,true])

4×4 数组(参数的缘故)。

在numpy的,但是,与这样布尔值numpy的数组索引会尝试提取的向量。此外,布尔索引矢量应该是相同的长度,因为它们是进行索引的尺寸。在你的 4×4 例如:

In numpy, however, indexing with boolean-valued numpy arrays in this way will try to extract a vector. Furthermore, the boolean index vectors should be the same length as the dimension they are indexing into. In your 4 x 4 example,

unCov[np.array([True,True]),np.array([True,True])]

unCov[np.array([True,True,False,False,False]),np.array([True,True,False,False,False])]

都返回的两个第一对角线元素的,所以不是一个子矩阵而是一个载体。此外,他们还提供沿的

both return the two first diagonal elements, so not a submatrix but rather a vector. Furthermore, they also give the less-then-encouraging warning along the lines of

/usr/bin/ipython:1: VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 4 but corresponding boolean dimension is 5

因此​​,在numpy的,你的逻辑索引向量应该是相同长度的 ndarray 相应的尺寸。然后,我上面写的也是如此:逻辑值转化为指标,结果预计将是一个载体。这个向量的长度真数在各项指标向量元素,所以如果你的布尔指标矢量有不同数量的真实的元素,则引用是没有意义的,你得到你的错误。

So, in numpy, your logical indexing vectors should be the same length as the corresponding dimensions of the ndarray. And then what I wrote above holds true: the logical values are translated into indices, and the result is expected to be a vector. The length of this vector is the number of True elements in every index vector, so if your boolean index vectors have a different number of True elements, then the referencing doesn't make sense, and you get the error that you get.

这篇关于在numpy的投掷例外逻辑指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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