存储和加载numpy的数组作为文件 [英] Storing and loading numpy arrays as files

查看:1659
本文介绍了存储和加载numpy的数组作为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的计划,我有大小不等的各种numpy的阵列工作。我需要将它们存储到供以后使用XML文件。我没有把它们写到二进制文件,所以我必须在一个地方(XML文件)我的所有数据,并通过200个文件未散。

In my program I'm working with various numpy arrays of varying sizes. I need to store them into XML files for later use. I did not write them to binary files so I have all my data in one place (the XML file) and not scattered through 200 files.

于是,我就用numpy的的array_str()方法来变换数组为一个字符串。
生成的XML如下所示:

So I tried to use numpy's array_str() method to transform an array into a String. The resulting XML looks like this:

-<Test date="2013-07-10-17:19">
    <Neurons>5</Neurons>
    <Errors>[7.7642140551985428e-06, 7.7639131137987232e-06]</Errors>
    <Iterations>5000</Iterations>
    <Weights1>[[ 0.99845902 -0.70780512 0.26981375 -0.6077122 0.09639695] [ 0.61856711 -0.74684913 0.20099992 0.99725171 -0.41826754] [ 0.79964397 0.56620812 -0.64055346 -0.50572793 -0.50100635]]</Weights1>
    <Weights2>[[-0.1851452 -0.22036027] [ 0.19293429 -0.1374252 ] [-0.27638478 -0.38660974] [ 0.30441414 -0.01531598] [-0.02478953 0.01823584]]</Weights2>
</Test>

的权重是我想要存储的值。现在的问题是,numpy的的fromstring()方法显然不能重新装载这些...
我得到ValueError错误:字符串大小必须元素大小的倍数

The Weights are the values I want to store. Now the problem is that numpy's fromstring() method can't reload these apparently... I get "ValueError: string size must be a multiple of element size"

我写了他们与np.array_str(W1),并尝试用np.fromstring(w_str1)来阅读。
显然,结果只有一维数组,即使它的工作原理,所以我必须手动恢复形状。哎,这是一个痛苦已经因为我还必须保存它在某种程度上也。

I wrote them with "np.array_str(w1)" and try to read them with "np.fromstring(w_str1)". Apparently the result is only a 1D array even if it works, so I have to restore the shape manually. Ugh, that is a pain already since I'll also have to store it somehow too.

什么是正确做到这一点的最好方法是什么? preferably一个也节省了我的磁盘阵列的形状和数据类型,无需手动看家的每一件小事。

What is the best way to do this properly? Preferably one that also saves my array's shape and datatype without manual housekeeping for every little thing.

推荐答案

numpy的提供了一种简单的方法来存储很多阵列的com pressed文件:

Numpy provides an easy way to store many arrays in a compressed file:

a = np.arange(10)
b = np.arange(10)
np.savez_compressed('file.npz', a=a, b=b)

np.savez_com pressed('file.npz',纽瓦= A,福利局= B)您甚至可以做,例如更改保存时数组名, $ C>。

You can even change the array names when saving, by doing for example: np.savez_compressed('file.npz', newa=a, newb=b).

要读取保存的文件使用 np.load(),它返回就像一个一个 NpzFile 实例字典:

To read the saved file use np.load(), which returns a NpzFile instance that works like a dictionary:

loaded = np.load('file.npz')

要加载数组:

a_loaded = loaded['a']
b_loaded = loaded['b']

from operator import itemgetter
g = itemgetter( 'a', 'b' )
a_loaded, b_loaded = g(np.load('file.npz'))

这篇关于存储和加载numpy的数组作为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆