我正在使用 numpy(1.20.3) 在 Python(3.8) 中工作,并尝试在具有不同数据类型的结构化数组上执行简单的函数。
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:无法使用灵活类型执行 reduce”。
我试图创建一些东西,然后通过通用结构化数组并返回具有相同数据类型的每个字段数组的生成视图......但这似乎不起作用。
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。
有人有什么好主意吗?