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

查看:32
本文介绍了如何正确保存和加载 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...

首先保存的数据如下所示:

Saved data first looks like this:

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:

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

或者:

a.tofile('test2.dat')
c = np.fromfile('test2.dat', dtype=int)
c == a
# 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:

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

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

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