如何正确保存和加载numpy.array()数据? [英] How to save and load numpy.array() data properly?

查看:204
本文介绍了如何正确保存和加载numpy.array()数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何正确保存和加载numpy.array数据.目前,我正在使用numpy.savetxt()方法.例如,如果我得到一个数组markers,它看起来像这样:

I wonder, how to save and load numpy.array data properly. Currently I'm using the numpy.savetxt() method. For example, if I got an array markers, which looks like this:

我尝试通过以下方式保存它:

I try to save it by the use of:

numpy.savetxt('markers.txt', markers)

在其他脚本中,我尝试打开以前保存的文件:

In other script I try to open previously saved file:

markers = np.fromfile("markers.txt")

这就是我得到的...

And that's what I get...

首先保存的数据如下:

0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00

但是当我使用相同的方法保存刚刚加载的数据时,即numpy.savetxt()看起来像这样:

But when I save just loaded data by the use of the same method, ie. numpy.savetxt() it looks like this:

1.398043286095131769e-76
1.398043286095288860e-76
1.396426376485745879e-76
1.398043286055061908e-76
1.398043286095288860e-76
1.182950697433698368e-76
1.398043275797188953e-76
1.398043286095288860e-76
1.210894289234927752e-99
1.398040649781712473e-76

我做错了什么? PS没有执行其他后台"操作.只需保存和加载,这就是我得到的.预先谢谢你.

What am I doing wrong? PS there are no other "backstage" operation which I perform. Just saving and loading, and that's what I get. Thank you in advance.

推荐答案

我发现最可靠的方法是将np.savetxtnp.loadtxt一起使用,而不是np.fromfile,它更适合于编写的二进制文件使用tofile. np.fromfilenp.tofile方法写入和读取二进制文件,而np.savetxt写入文本文件. 因此,例如:

The most reliable way I have found to do this is to use np.savetxt with np.loadtxt and not np.fromfile which is better suited to binary files written with tofile. The np.fromfile and np.tofile methods write and read binary files whereas np.savetxt writes a text file. So, for example:

In [1]: a = np.array([1, 2, 3, 4])
In [2]: np.savetxt('test1.txt', a, fmt='%d')
In [3]: b = np.loadtxt('test1.txt', dtype=int)
In [4]: a == b
Out[4]: array([ True,  True,  True,  True], dtype=bool)

或者:

In [5]: a.tofile('test2.dat')
In [6]: c = np.fromfile('test2.dat', dtype=int)
In [7]: c == a
Out[7]: array([ True,  True,  True,  True], dtype=bool)

即使速度较慢并且有时会创建更大的文件,我也使用前一种方法:二进制格式可能取决于平台(例如,文件格式取决于系统的字节序).

I use the former method even if it is slower and creates bigger files (sometimes): the binary format can be platform dependent (for example, the file format depends on the endianness of your system).

NumPy数组有一种与平台无关的格式,可以使用np.savenp.load保存和读取:

There is a platform independent format for NumPy arrays, which can be saved and read with np.save and np.load:

In  [8]: np.save('test3.npy', a)    # .npy extension is added if not given
In  [9]: d = np.load('test3.npy')
In [10]: a == d
Out[10]: array([ True,  True,  True,  True], dtype=bool)

这篇关于如何正确保存和加载numpy.array()数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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