我将2个Numpy数组保存为字典
当我从二进制文件加载数据时,我得到另一个TotalYearWages = If(DB.interview_earnings.
Where(Function(t) t.IID = Input.ID AndAlso t.EarningsYear.Equals(Input.EarningsYear)).
Select(Function(t) t.Amount).Sum(), 0)
。我可以将加载的Numpy数组用作字典吗?
这是我的代码和我的脚本的输出:
ndarray
输出:{' X':数组([0,1,2,3,4,5,6,7,8,9]),' Y':数组([100,101,102,103,104,105,106,107])}
答案 0 :(得分:2)
是的,您可以在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]
答案 1 :(得分:1)
从z1
访问数据的另一个选项应该是:
z1.flatten()[0]['X']
答案 2 :(得分:1)
存储numpy
数组的另一种未充分利用的方法是HDF5。好处是:
pickle
这是一个演示:
import h5py, numpy as np
x = np.arange(10)
y = np.array([100, 101, 102, 103, 104, 105, 106, 107])
z = {'X': x, 'Y': y}
with h5py.File('file.h5', 'w', libver='latest') as f: # use 'latest' for performance
for k, v in z.items():
f.create_dataset('dict/'+str(k), data=v)
with h5py.File('file.h5', 'r', libver='latest') as f:
x_read = f['dict']['X'][:] # [:] syntax extracts numpy array into memory
y_read = f['dict']['Y'][:]
print(x_read)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])