简体   繁体   English

为什么记录中的零维numpy数组不支持项目分配?

[英]Why don't zero-dimensional numpy arrays in records support item assignment?

First, a shape (4,) numpy array can be assigned: 首先,可以分配一个形状(4,) numpy数组:

In [33]: a = np.zeros((10,), dtype=[('k', '<u8'), ('t', '<f4'), ('d', np.bool, (4,))])

In [34]: b = np.ones((), dtype=np.bool)

In [35]: a[0][2][3] = b

But, a shape () cannot if it's in a record: 但是,形状()如果在记录中,则不能:

In [36]: a = np.zeros((10,), dtype=[('k', '<u8'), ('t', '<f4'), ('d', np.bool, ())])

In [37]: a[0][2][()] = b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-075e4ba95b60> in <module>()
----> 1 a[0][2][()] = b

TypeError: 'numpy.bool_' object does not support item assignment

But if it's not in a record, it works just fine: 但是,如果它不在记录中,它就可以正常工作:

In [38]: a = np.zeros((), np.bool)

In [39]: a[()] = b

Why? 为什么?


What a record type looks like: 记录类型如下所示:

In [4]: a
Out[4]:
array([(0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False]),
       (0,  0., [False, False, False, False])],
      dtype=[('k', '<u8'), ('t', '<f4'), ('d', '?', (4,))])

In [2]: a
Out[2]:
array([(0,  0., False), (0,  0., False), (0,  0., False), (0,  0., False),
       (0,  0., False), (0,  0., False), (0,  0., False), (0,  0., False),
       (0,  0., False), (0,  0., False)],
      dtype=[('k', '<u8'), ('t', '<f4'), ('d', '?')])

The first example: 第一个例子:

a = np.zeros((10,), dtype=[('k', '<u8'), ('t', '<f4'), ('d', np.bool, (4,))])

Creates a 1-dimensional numpy array where each element contains Tuple(_, _, List[np.bool]) 创建一维numpy数组,其中每个元素都包含Tuple(_, _, List[np.bool])

The index a[0] references: 索引a[0]引用:

(0,  0., [False, False, False, False])

Therefore a[0][2][3] references the np.bool inside ** below 因此, a[0][2][3]引用下面**中的np.bool

(0,  0., [False, False, False, **False**])

and acts how you expected. 并按照您的期望行事。

The second example: 第二个例子:

a = np.zeros((10,), dtype=[('k', '<u8'), ('t', '<f4'), ('d', np.bool, ())])

Creates a 1-dimensional numpy array where each element contains Tuple(_, _, np.bool) eg 创建一个一维的numpy数组,其中每个元素包含Tuple(_, _, np.bool)例如

(0,  0., False)

So a[0][2] references: 因此, a[0][2]引用:

(0, 0., **False**)

The third index a[0][2]**[()]** acts on the np.bool object, which is not indexable. 第三个索引a[0][2]**[()]**作用于np.bool对象,该对象不可索引。

A simplified example that throws the same error is shown below: 引发相同错误的简化示例如下所示:

a = [False]
a[0] = True    # works
a = True       # changed type from [bool] to bool
a[0] = True    # Fails b/c bools are not scriptable

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bool' object does not support item assignment
array([(0,  0., False), (0,  0., False), (0,  0., False), (0,  0., False),
       (0,  0., False), (0,  0., False), (0,  0., False), (0,  0., False),
       (0,  0., False), (0,  0., False)],
      dtype=[('k', '<u8'), ('t', '<f4'), ('d', '?')])

Note that ('d', '?') is the same as though you had defined the field as ('d', np.bool) . 请注意, ('d', '?')与将字段定义为('d', np.bool)

In my own example 以我自己的例子

In [621]: x = np.ones((3,),dtype=[('f0',int),('f1',int,()),('f2',int,(1,))])
In [622]: x
Out[622]: 
array([(1, 1, [1]), (1, 1, [1]), (1, 1, [1])],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4', (1,))])

'f0' and 'f1' are indexed in the same way; “ f0”和“ f1”的索引方式相同; they have the same description (except for name). 它们具有相同的描述(名称除外)。 np.dtype constructor has stripped off or ignored the () . np.dtype构造函数已剥离或忽略了()

In [623]: x['f0']=np.arange(3)
In [624]: x['f1']=np.arange(3)
In [625]: x['f2']=np.arange(3)
....
ValueError: could not broadcast input array from shape (3) into shape (3,1)

So the problem, if there is one, isn't with indexing, but with dtype ignoring the () part of the descriptor. 因此,如果有的话,问题不在于索引,而在于dtype忽略了描述符的()部分。


In your first case, the 'd'` field can be accessed with the triple index, or with a 2d index: 在第一种情况下,可以使用三重索引或2d索引访问“ d”字段:

In [649]: a['d'].shape
Out[649]: (10, 4)
In [650]: a[0]['d'][3]
....
In [651]: a['d'][0,3]

My x['f1'] field with '()' remains 1d 我的x['f1']字段为'()'仍为1d

In [652]: x['f1'].shape
Out[652]: (3,)
In [653]: x['f2'].shape
Out[653]: (3, 1)

I can't think of a meaningful way of creating or indexing a 0d dimension. 我想不出一种有意义的方式来创建或索引0d维度。 We don't want (3,0) shape, and can't have (3,()) shape or [:,()]` indexing. 我们不需要(3,0)形状,也不能具有(3,()) shape or [:,()]`索引。

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

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