【发布时间】:2026-02-14 08:50:01
【问题描述】:
我正在 Python(3.8) 和 numpy(1.20.3) 中工作,并尝试在具有不同数据类型的结构化数组上执行简单的函数。
def test_large_record():
x = numpy.array([0.0, 0.2, 0.3], dtype=numpy.float)
x_2 = numpy.array([0.01, 0.12, 0.82], dtype=numpy.float)
y = numpy.array([1, 5, 7], dtype=numpy.int)
rec_array = numpy.rec.fromarrays([x, x_2, y], dtype=[('x', '<f8'), ('x_2', '<f8'), ('y', '<i8')])
print(rec_array.min())
这会导致“TypeError: cannot perform reduce with flexible type”。
我尝试创建一些东西,然后通过通用结构化数组并返回具有相同数据类型的每个字段数组的生成视图....但这似乎不起作用。
def rec_homogeneous_generator(rec_array):
dtype = {}
for name, dt in rec_array.dtype.descr:
if dt not in dtype.keys():
dtype[dt] = []
dtype[dt].append(name)
for dt, cols in dtype.items():
r = rec_array[cols]
v = r.view(dt)
yield v
def test_large_record():
x = numpy.array([0.0, 0.2, 0.3], dtype=numpy.float)
x_2 = numpy.array([0.01, 0.12, 0.82], dtype=numpy.float)
y = numpy.array([1, 5, 7], dtype=numpy.int)
rec_array = numpy.rec.fromarrays([x, x_2, y], dtype=[('x', '<f8'), ('x_2', '<f8'), ('y', '<i8')])
for h_array in rec_homogeneous_generator(rec_array):
print(h_array.min(axis=0))
这导致 0.0 和 0 这不是我所期望的。我应该得到 [0, 0.01] 和 1。
有人有什么好主意吗?
【问题讨论】:
-
你检查过
h_array吗?为什么不只计算每个字段而不按 dtype 分组? -
注意多字段索引中的
view。在最近的 numpy 版本中,多字段索引会生成一个view,所有字段仍然存在,即使它们被“删除”。 -
我拥有的数据非常大,因此迭代每个字段可能是较慢的选择。我宁愿让 numpy 做这个提升。我还了解到,这种观点很难做到……似乎是一个奇怪的函数结果。
-
如果字段数与记录数相比较小,则对字段进行迭代还不错。大多数
recfunctions都这样做。
标签: python python-3.x numpy numpy-ufunc recarray