python - NumPy dtype 强制结构化数组扩展为类型

标签 python numpy

我对具有如下结构的程序使用特殊的np.dtypes:

POINT = np.dtype([('vertices', '<f4', 2), ('center', '<f4', 2), ('bbox', '<f4', 4)])

我需要指定另一个仅使用上面最后一个字段的np.dtype,如下所示:

MBR = np.dtype([('bbox', '<f4', 4)])

这样我以后就可以像这样访问两个数组的该字段:

def intersection(s, t):

    sxmin, sxmax, symin, sxmax = s['bbox']
    txmin, txmax, tymin, tymax = t['bbox']

    # do stuff

但是,当我创建以下数组时,它正在扩展,我不确定为什么:

box = np.array([1, 2, 3, 4], dtype=MBR)
# expected output...
array([1., 2., 3., 4.], dtype=[('bbox', '<f4', 4)])
# actual output...
array([([1., 1., 1., 1.],), ..., ([4., 4., 4., 4.],)], dtype=[('bbox', '<f4', 4)])

快速测试返回了我所期望的结果......

np.empty([], dtype=MBR)
array(([nan, nan, inf, nan],), dtype=[('bbox', '<f4', 4)])

编辑:

执行以下操作将返回预期结果:

box = np.array(([1, 2, 3, 4],), dtype=MBR)

现在的问题是:为什么我必须将其包装在元组中以符合数据类型?

最佳答案

简短的回答是,具有嵌套列表和元组的输入格式必须与显示格式匹配:

In [59]: MBR = np.dtype([('bbox', '<f4', 4)])                                                    
In [60]: arr = np.zeros(3, dtype=MBR)                                                            
In [61]: arr                                                                                     
Out[61]: 
array([([0., 0., 0., 0.],), ([0., 0., 0., 0.],), ([0., 0., 0., 0.],)],
      dtype=[('bbox', '<f4', (4,))])
In [62]: arr[0]                                                                                  
Out[62]: ([0., 0., 0., 0.],)
In [63]: arr[0]=[1,2,3,4]                                                                        
In [64]: arr[1]=[10,11,12,13]                                                                    
In [65]: arr                                                                                     
Out[65]: 
array([([ 1.,  2.,  3.,  4.],), ([10., 11., 12., 13.],),
       ([ 0.,  0.,  0.,  0.],)], dtype=[('bbox', '<f4', (4,))])
In [66]: np.array([([1,2,3,4],)],MBR)                                                            
Out[66]: array([([1., 2., 3., 4.],)], dtype=[('bbox', '<f4', (4,))])

因此,对于典型的复合数据类型,我们说输入应该是一个元组列表,数组的每个“记录”一个元组。在元组中,每个字段一个元素。

您在字段中增加了尺寸 (4,) 尺寸的复杂性。

请注意,从数组中提取的字段形状将外部数组形状与内部字段形状相结合:

In [67]: arr['bbox']                                                                             
Out[67]: 
array([[ 1.,  2.,  3.,  4.],
       [10., 11., 12., 13.],
       [ 0.,  0.,  0.,  0.]], dtype=float32)

通常,按字段向结构化数组赋值比按记录更容易:

In [68]: arr['bbox']=np.arange(12).reshape(3,4)                                                  
In [69]: arr                                                                                     
Out[69]: 
array([([ 0.,  1.,  2.,  3.],), ([ 4.,  5.,  6.,  7.],),
       ([ 8.,  9., 10., 11.],)], dtype=[('bbox', '<f4', (4,))])

关于python - NumPy dtype 强制结构化数组扩展为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59951725/

相关文章:

python - 如何在Python中为视频添加白色背景?

python - 如何在 Linux 上使用 pyinstaller 生成 Windows 可执行文件?

python - 如何在 OpenCV Python 中识别图像中的不同对象

python - 按升序对 numpy 矩阵行值进行排序

python - IOError : [Errno 2] - Can permissions cause an IOError Errno 2 when using open()

python - Django 应用找不到环境变量

python - 名称错误 : name 'install' is not defined when installing packages using pip

python - 更快地求解由具有多个变量参数的多个非线性方程控制的多个参数的方法?

numpy - 如何在 Pandas DataFrame 中设置值的时区?

python - 计算多个 numpy 屏蔽数组的平均值 (masked_all)