python data_error ai_generated true

ValueError: 用序列设置数组元素。在 1 维之后请求的数组形状不均匀。检测到的形状为 (3,) + 不均匀部分。

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

ID: python/numpy-visible-deprecation-ragged

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-02-14首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

对长度不一致的列表的列表调用 np.array()。自 NumPy 1.24 起会报错而不是创建 object 数组。

English

np.array() called on a list of lists with differing lengths. Since NumPy 1.24 this raises instead of creating an object array.

generic

解决方案

  1. 95% 成功率
    np.array(data, dtype=object) to explicitly request an object array
  2. 85% 成功率
    Pad sequences first: from itertools import zip_longest; np.array(list(zip_longest(*data, fillvalue=0))).T

无效尝试

常见但无效的做法:

  1. 90% 失败

    Still cannot broadcast ragged data

  2. 85% 失败

    np.stack requires identical shapes too