nai*_*bah 0 python arrays dictionary numpy
我将 2 个 Numpy 数组保存为字典。
当我从二进制文件加载数据时,我得到另一个ndarray. 我可以将加载的 Numpy 数组用作字典吗?
这是我的代码和脚本的输出:
import numpy as np
x = np.arange(10)
y = np.array([100, 101, 102, 103, 104, 105, 106, 107])
z = {'X': x, 'Y': y}
np.save('./data.npy', z)
z1 = np.load('./data.npy')
print(type(z1))
print(z1)
print(z1['X']) #this line will generate an error
Run Code Online (Sandbox Code Playgroud)
输出:{'X': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 'Y': array([100, 101, 102, 103, 104, 105 , 106, 107])}
是的,您可以在 0 维数组中访问底层字典。试试z1[()]。
这是一个演示:
np.save('./data.npy', z)
d = np.load('./data.npy')[()]
print(type(d))
<class 'dict'>
print(d['X'])
[0 1 2 3 4 5 6 7 8 9]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1091 次 |
| 最近记录: |