简体   繁体   English

使用 boolean 索引将向量作为对象添加到 numpy 数组 - 2D arrays 有效,1D arrays 无效

[英]Add vectors as objects to numpy array with boolean indexing - 2D arrays work, 1D arrays do not

I want to assign vectors to a numpy array to make convenient use of the boolean indexing.我想将向量分配给 numpy 数组以方便地使用 boolean 索引。 It worked, until I hit a corner case.它奏效了,直到我遇到一个角落案例。 Minimal working example:最小的工作示例:

a = np.array([None] * 2)
b = [np.ones(shape=[1, 3]), np.ones(shape=[2, 4])]
a[True, True] = b  # Works as intended

print(a)

However, when all arrays are 1D, I get an error:但是,当所有 arrays 都是一维时,我得到一个错误:

a = np.array([None] * 2)
b = [np.ones(shape=[1, 3]), np.ones(shape=[1, 4])]  # shape of second array changed!
a[True, True] = b  # ValueError: cannot copy sequence with size 2 to array axis with dimension 1

Any ideas how I can avoid this?有什么想法可以避免这种情况吗? (I checked on both numpy versions v1.19.5 and v1.21.1) (我检查了 numpy 版本 v1.19.5 和 v1.21.1)

I solved it.我解决了。 If you use a np.where, the code runs.如果您使用 np.where,代码就会运行。 Not sure why.不知道为什么。 Example:例子:

a = np.array([None] * 2)
b = [np.ones(shape=[1, 3]), np.ones(shape=[1, 4])]  # shape of second array changed!
a[np.where([True, True])] = b  # Works as intended.

print(a)
In [436]: a[True,True]=b
Traceback (most recent call last):
  File "<ipython-input-436-7c0409496970>", line 1, in <module>
    a[True,True]=b
ValueError: could not broadcast input array from shape (3,) into shape (1,)

Looks like this masked assignment is first converting b to an array:看起来这个掩码赋值首先将b转换为数组:

In [437]: np.array(b)
<ipython-input-437-cbcacfae87b2>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  np.array(b)
Traceback (most recent call last):
  File "<ipython-input-437-cbcacfae87b2>", line 1, in <module>
    np.array(b)
ValueError: could not broadcast input array from shape (3,) into shape (1,)

But (as experienced users know), trying to make an array from arrays that match in the first dimension (1), but not the second, raises this error.但是(如有经验的用户所知),尝试从 arrays 创建一个在第一个维度 (1) 中匹配但在第二个维度中不匹配的数组会引发此错误。

Your in your first case the b arrays differ in the first axis, and np.array(b) produces a 2 element object dtype array.在第一种情况下, b arrays 在第一个轴上有所不同, np.array(b)生成一个 2 元素 object dtype 数组。

These assignments don't, apparently, do the np.array(b) :显然,这些作业不会执行np.array(b)

In [439]: a[:]=b
In [440]: a
Out[440]: array([array([[1., 1., 1.]]), array([[1., 1., 1., 1.]])], dtype=object)
In [441]: a[[0,1]]=b
In [442]: a
Out[442]: array([array([[1., 1., 1.]]), array([[1., 1., 1., 1.]])], dtype=object)

Assigning a 'proper' object dtype array does work:分配一个“适当的” object dtype 数组确实有效:

In [447]: a[[True,True]]=a.copy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM