【问题标题】:TypeError: 'numpy.uint32' object is not iterable in using numpy.full functionTypeError:“numpy.uint32”对象在使用 numpy.full 函数时不可迭代
【发布时间】: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


【解决方案1】:

prop_shape 在原始代码中进行迭代。有时它是一个整数(对于标量,例如 74537000),有时是一个二元素列表(对于向量,例如 [74537000, 3])。我假设它被固定为一个二元素列表(因此是一个向量)。这就是为什么如果标量不是float32 dtype 的单个标量的元组会给出这样的错误。为了避免这种情况,我创建了一个if statement,每次在创建数组之前检查变量的dtype:

if isinstance(prop_shape, list):
    my_array = np.full(tuple(prop_shape), -1, prop_in_dtype)
else:
    my_array = np.full(prop_shape, -1, prop_in_dtype)

但是,正如我在对 Warren 建议的评论中所说,我最终得到如下 MemoryError:

Traceback (most recent call last):
  File "DLA_DM.py", line 96, in <module>
    gizmo.plot.Image.plot_image(settings_centroid.part, 'dark', 'mass', settings_centroid.image_kind, [0,1], settings_centroid.dimensions_select, settings_centroid.distance_max, settings_centroid.distance_bin_width_generic, distance_bin_number=settings_centroid.distance_bin_number, part_indices=part_indices, write_plot=write_plot, plot_directory=plot_directory, background_color=background_color, use_column_units=False, center_position=settings_centroid.center_position, rotation=settings_centroid.rotation, property_select={'mass.bound':[1e9,1e13]}, subsample_factor=subsample_factor, image_limits=settings_centroid.image_limits_xy, hal=settings_centroid.hal, hal_indices=settings_centroid.hal_indices, hal_position_kind=hal_position_kind, hal_radius_kind=hal_radius_kind, return_halo_info=settings_centroid.return_halo_info_value_dark)
  File "/usr5/username/simulation/gizmo/gizmo_plot.py", line 825, in plot_image
    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 1171, in read_particles
    part[spec_name][prop] = np.full(prop_shape, -1, prop_in_dtype)
  File "/usr/local/anaconda3/lib/python3.5/site-packages/numpy/core/numeric.py", line 309, in full
    a = empty(shape, dtype, order)
MemoryError

【讨论】:

    【解决方案2】:

    我可以通过以下方式重现错误消息:

    In [174]: tuple(np.uint32(1))                                                        
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-174-3420675a1788> in <module>
    ----> 1 tuple(np.uint32(1))
    
    TypeError: 'numpy.uint32' object is not iterable
    

    所以我猜prop_shape 变量有问题。

    【讨论】:

    • 我只知道 prop_shape 是一个包含位置 3d 坐标的列表对象。
    • 我的意思是第一个元素是位置的数量,第二个元素是维度。
    • 自行测试tuple(prop_shape)。将稀疏调用更改为 full 时出现相同错误的事实进一步证明问题出在 tuple 函数参数上。
    • 在上述行之前, print(tuple(prop_shape)) 给出了预期的元组 (74537000, 3)。
    猜你喜欢
    • 2015-09-29
    • 2015-08-02
    • 2018-04-30
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多