且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何正确保存和加载numpy.array()数据?

更新时间:2023-11-30 23:46:34

我发现最可靠的方法是将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)