简体   繁体   English

将numpy数组另存为字典

[英]Saving numpy arrays as a dictionary

I'm saving 2 Numpy arrays as a dictionary. 我将2个Numpy数组保存为字典。
When I load the data from the binary file, I get another ndarray . 当我从二进制文件加载数据时,我得到另一个ndarray Can I use the loaded Numpy array as a dictionary? 我可以将加载的Numpy数组用作字典吗?

Here is my code and the output of my script: 这是我的代码和脚本的输出:

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

Output: {'X': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 'Y': array([100, 101, 102, 103, 104, 105, 106, 107])} 输出:{'X':array([0,1,2,3,4,5,6,7,8,8,9]),'Y':array([100,101,102,103,104,105 ,106,107])}

Yes, you can access the underlying dictionary in a 0-dimensional array. 是的,您可以按0维数组访问基础字典。 Try z1[()] . 尝试z1[()]

Here's a demo: 这是一个演示:

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]

z1访问数据的另一种选择应该是:

z1.flatten()[0]['X']

An alternative and underused method of storing numpy arrays is HDF5. HDF5是存储numpy数组的另一种未充分利用的方法。 The benefits are: 好处是:

  • Transportability, ie not Python-specific like pickle 可运输性,即不是特定于Python的pickle
  • Ability to access data out-of-memory & chunking options for optimisation 能够访问内存不足和分块选项的数据以进行优化
  • Compression options to optimize read or write performance 压缩选项可优化读取或写入性能

Here's a demo: 这是一个演示:

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM