Python 使用 Pickle 保存 Numpy 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52444921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:06:52  来源:igfitidea点击:

Save Numpy Array using Pickle

pythonnumpypickle

提问by Anant

I've got a Numpy array that I would like to save (130,000 x 3) that I would like to save using Pickle, with the following code. However, I keep getting the error "EOFError: Ran out of input" or "UnsupportedOperation: read" at the pkl.load line. This is my first time using Pickle, any ideas?

我有一个 Numpy 数组,我想使用 Pickle 保存它(130,000 x 3),并使用以下代码。但是,我一直在 pkl.load 行收到错误“EOFError:输入不足”或“UnsupportedOperation:读取”。这是我第一次使用 Pickle,有什么想法吗?

Thanks,

谢谢,

Anant

一只蚂蚁

import pickle as pkl
import numpy as np

arrayInput = np.zeros((1000,2)) #Trial input
save = True
load = True

filename = path + 'CNN_Input'
fileObject = open(fileName, 'wb')

if save:
    pkl.dump(arrayInput, fileObject)
    fileObject.close()

if load:
    fileObject2 = open(fileName, 'wb')
    modelInput = pkl.load(fileObject2)
    fileObject2.close()

if arrayInput == modelInput:
    Print(True)

回答by piokuc

You should use numpy.saveand numpy.load.

您应该使用numpy.savenumpy.load

回答by hpaulj

I have no problems using pickle:

我使用没有问题pickle

In [126]: arr = np.zeros((1000,2))
In [127]: with open('test.pkl','wb') as f:
     ...:     pickle.dump(arr, f)
     ...:     
In [128]: with open('test.pkl','rb') as f:
     ...:     x = pickle.load(f)
     ...:     print(x.shape)
     ...:     
     ...:     
(1000, 2)

pickleand np.save/loadhave a deep reciprocity. Like I can load this pickle with np.load:

picklenp.save/load有着深厚的互惠关系。就像我可以加载这个泡菜np.load

In [129]: np.load('test.pkl').shape
Out[129]: (1000, 2)

If I open the pickle file in the wrong I do get your error:

如果我以错误的方式打开泡菜文件,我会收到您的错误:

In [130]: with open('test.pkl','wb') as f:
     ...:     x = pickle.load(f)
     ...:     print(x.shape)
     ...:    
UnsupportedOperation: read

But that shouldn't be surprising - you can't read a freshly opened write file. It will be empty.

但这不足为奇 - 您无法读取新打开的写入文件。它将是空的。

np.save/loadis the usual pair for writing numpy arrays. But pickle uses saveto serialize arrays, and saveuses pickle to serialize non-array objects (in the array). Resulting file sizes are similar. Curiously in timings the pickle version is faster.

np.save/load是用于编写 numpy 数组的常用对。但是pickle 用于save序列化数组,而savepickle 用于序列化非数组对象(在数组中)。生成的文件大小相似。奇怪的是,pickle 版本在时间上更快。

回答by TomF

It's been a bit but if you're finding this, Pickle completes in a fraction of the time.

这已经有点过了,但如果你发现了这个,Pickle 会在很短的时间内完成。

with open('filename','wb') as f: pickle.dump(arrayname, f)

with open('filename','rb') as f: arrayname1 = pickle.load(f)

numpy.array_equal(arrayname,arrayname1) #sanity check

On the other hand, by default numpy compress took my 5.2GB down to .4GB and Pickle went to 1.7GB.

另一方面,默认情况下 numpy 压缩将我的 5.2GB 降到 .4GB,而 Pickle 降到 1.7GB。

回答by Lucky Suman

You should use numpy.save()for saving numpy matrices.

您应该numpy.save()用于保存 numpy 矩阵。