【发布时间】:2019-10-18 00:47:36
【问题描述】:
在一个巨大的 python 脚本中的某些行中,有一个由 74537000 个元素组成的 3d 数组,这些元素都是浮点数:
import numpy as np
prop_shape = [74537000, 3] # to produce this however a long script is written and this will be printed (just before the controversial line below) in the first run of the code assuming it's a vector quantity like position.
prop_in_dtype = np.float32 # position array with the shape (74537000, 3) is made up of float32 values. In general, however, prop_in_dtype is changing across the script due to the nature of the array
当我们试图创建一个元素都是 -1 的相同数组时,我们可以使用 numpy 如下:
.
.
.
my_array = np.full(tuple(prop_shape), -1, prop_in_dtype)
.
.
.
运行此程序时,我收到以下错误消息:
Traceback (most recent call last):
File "DLA_DM.py", line 19, in <module>
settings_centroid.init()
File "/usr5/username/settings_centroid.py", line 43, in init
part=gizmo.io.Read.read_snapshots(species, snapshot_value_kind, snapshot_number, simulation_directory='.', snapshot_directory='output/', simulation_name='', properties=properties, element_indices=None, particle_subsample_factor=0, separate_dark_lowres=True, sort_dark_by_id=False, convert_float32=True, host_number=1, assign_host_coordinates=True, assign_host_principal_axes=False, assign_host_orbits=False, assign_formation_coordinates=False, assign_pointers=False, check_properties=True)
File "/usr5/username/simulation/gizmo/gizmo_io.py", line 649, in read_snapshots
element_indices, convert_float32, header)
File "/usr5/username/simulation/gizmo/gizmo_io.py", line 1164, in read_particles
part[spec_name][prop] = np.full(tuple(prop_shape), - 1, prop_in_dtype)
TypeError: 'numpy.uint32' object is not iterable
【问题讨论】:
-
嗯?您在生成异常的行之后添加了一个打印,并声称显示该打印的输出 - 由于上一行的异常,这怎么可能是真的?
-
嗨,Jason,我不明白在线上产生的异常。什么是“numpy.uint32”?
-
如果您提供minimal reproducible example,别人会更容易帮助您。您可能不需要在示例中使用您的大数组。您应该能够使用形状为 (5, 3) 的数组重现问题。
-
另外,当你解决了这个问题后,你很快就会遇到另一个问题:你不能从
dia_matrix中减去一个非零标量。如果你尝试,你会得到错误NotImplementedError: subtracting a nonzero scalar from a sparse matrix is not supported。如果你真的需要一个满 -1 的数组,请使用常规的 numpy 数组。 -
满值 -1 的
dia_matrix将比常规的 numpy 数组需要 更多 内存。当矩阵中的大多数值为 0 时,稀疏矩阵很有用。
标签: python-3.x numpy matrix scipy typeerror