You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建嵌套numpy记录数组遇维度错误:求正确实现方案

嵌套NumPy记录数组的维度与dtype设置问题

问题背景

创建嵌套NumPy记录数组时遭遇维度不匹配错误,参考示例后仍存在误解,以下是最小可复现示例(数组由脚本生成,非CSV导入):

生成数组代码

import numpy as np

arr1 = np.array([4, 5, 4, 5])
arr2 = np.array([0, 0, -1, -1])
arr3 = np.array([0.51, 0.89, 0.59, 0.94])
arr4 = np.array(
    [[0.52, 0.80, 0.62, 1.1], [0.41, 0.71, 0.46, 0.77], [0.68, 1.12, 0.78, 1.19]]
).T
arr5 = np.repeat(np.array([0.6, 0.2, 0.2]), 4).reshape(3, 4).T
arrs = (arr1, arr2, arr3, arr4, arr5)

for i in arrs:
    print(i.shape, i)

数组打印结果

(4,) [4 5 4 5]
(4,) [ 0  0 -1 -1]
(4,) [0.51 0.89 0.59 0.94]
(4, 3) [[0.52 0.41 0.68]
 [0.8  0.71 1.12]
 [0.62 0.46 0.78]
 [1.1  0.77 1.19]]
(4, 3) [[0.6 0.2 0.2]
 [0.6 0.2 0.2]
 [0.6 0.2 0.2]
 [0.6 0.2 0.2]]

错误代码与信息

执行以下代码时触发错误:

dtypes = [
        ("state", "f8"),
        ("variability", "f8"),
        ("target", "f8"),
        ("measured", [("mean", "f8"), ("low", "f8"), ("hi", "f8")], (4,)),
        ("var", [("mid", "f8"), ("low", "f8"), ("hi", "f8")], (4,)),
]
ans = np.column_stack(arrs).view(dtype=dtypes)

错误信息:

ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.

问题1:如何得到目标嵌套结构的数组输出?

np.column_stack(arrs)的输出为:

[[ 4.    0.    0.51  0.52  0.41  0.68  0.6   0.2   0.2 ]
 [ 5.    0.    0.89  0.8   0.71  1.12  0.6   0.2   0.2 ]
 [ 4.   -1.    0.59  0.62  0.46  0.78  0.6   0.2   0.2 ]
 [ 5.   -1.    0.94  1.1   0.77  1.19  0.6   0.2   0.2 ]]

期望的嵌套结构输出:

[[4 0 0.51 (0.52, 0.41, 0.68) (0.6, 0.2, 0.2)]
 [5 0 0.89 (0.8, 0.71, 1.12) (0.6, 0.2, 0.2)]
 [4 -1 0.59 (0.62, 0.46, 0.78) (0.6, 0.2, 0.2)]
 [5 -1 0.94 (1.1, 0.77, 1.19) (0.6, 0.2, 0.2)]]

问题2:如何正确设置dtype.names?

要求:

  • print(rec_array.dtype.names)返回:('state', 'variability', 'target', 'measured', 'var')
  • print(rec_array['measured'].dtype.names)返回:('mean', 'low', 'hi')
  • 其他嵌套数组的名称需正确设置

解决方案

1. 修正dtype定义

原dtype中为measured和var指定(4,)形状是错误的,每个嵌套字段对应输入中(4,3)数组的每一行,应仅定义内部子字段类型,无需指定外层形状。

正确的dtype定义:

dtypes = [
    ("state", "f8"),
    ("variability", "f8"),
    ("target", "f8"),
    ("measured", [("mean", "f8"), ("low", "f8"), ("hi", "f8")]),
    ("var", [("mid", "f8"), ("low", "f8"), ("hi", "f8")]),
]

2. 用np.rec.fromarrays构造记录数组

直接使用column_stack后view会因内存布局不匹配报错,np.rec.fromarrays可自动匹配嵌套dtype结构:

ans = np.rec.fromarrays(arrs, dtype=dtypes)

验证结果

查看数组结构

print(ans)

输出:

[(4., 0., 0.51, (0.52, 0.41, 0.68), (0.6, 0.2, 0.2))
 (5., 0., 0.89, (0.8 , 0.71, 1.12), (0.6, 0.2, 0.2))
 (4., -1., 0.59, (0.62, 0.46, 0.78), (0.6, 0.2, 0.2))
 (5., -1., 0.94, (1.1 , 0.77, 1.19), (0.6, 0.2, 0.2))]

验证dtype.names

print(ans.dtype.names)
# 输出:('state', 'variability', 'target', 'measured', 'var')

print(ans['measured'].dtype.names)
# 输出:('mean', 'low', 'hi')

关键说明

  • np.rec.fromarrays是专门用于将多个数组组合为记录数组的工具,能自动处理嵌套dtype的映射,避免手动view导致的内存布局错误。
  • 嵌套dtype无需指定外层形状,外层数组长度由输入单个数组的长度(此处为4)决定,每个嵌套字段自动对应输入中(4,3)数组的每一行,形成包含3个子字段的结构。

内容的提问来源于stack exchange,提问作者a11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 03:55:13