ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

can only convert an array of size 1 to a Python scalar

2026/9/25 10:17:54 拓冰建站 浏览量
can only convert an array of size 1 to a Python scalar

目录

报错代码:

解决方法:


can only convert an array of size 1 to a Python scalar

报错代码:

import numpy as npjoints_2d_data=[[1,2],[3,4]]joints_2d_array = np.array([joints_2d_data])
#
# np.savez_compressed('data_2d_cus.npz', joints_2d=[{"data":joints_2d_array}])
np.savez_compressed('data_2d_cus.npz', joints_2d={"data":joints_2d_array})
np.savez_compressed('data_2d_cus.npz', joints_2d=[joints_2d_array])# 加载数据
data_2d = np.load('data_2d_cus.npz', allow_pickle=True)['joints_2d']extracted_joints_2d_data = data_2d.item()print(extracted_joints_2d_data)

解决方法:

发现内容是字典是可以的,直接存numpy矩阵,不能用item()

import numpy as npjoints_2d_data=[[1,2],[3,4]]joints_2d_array = np.array([joints_2d_data])
#
# np.savez_compressed('data_2d_cus.npz', joints_2d=[{"data":joints_2d_array}])
np.savez_compressed('data_2d_cus.npz', joints_2d={"data":joints_2d_array})
np.savez_compressed('data_2d_cus.npz', joints_2d=[{"data":joints_2d_array}])# 加载数据
data_2d = np.load('data_2d_cus.npz', allow_pickle=True)['joints_2d']extracted_joints_2d_data = data_2d.item()print(extracted_joints_2d_data)