Python节省大型模拟

时间:2016-03-26 13:42:37

标签: python matlab

我来自Matlab背景,最近开始用Python编写代码。我使用蒙特卡罗生成大量数据,这些数据有时是10维数组。在Matlab中,我一直使用.mat文件格式来存储这些数组,以及使用struct的输入和其他参数。

Python推荐的存储空间或等效内容是什么?

2 个答案:

答案 0 :(得分:2)

python中的标准对象序列化是pickle。来自文档

  

“Pickling”是Python对象层次结构的过程   转换为字节流,“unpickling”是反向的   操作,即字节流(来自二进制文件或类似字节)   对象)被转换回对象层次结构。

一个例子是

import pickle
mydata = [1, 2, 3, 5]
pickle.dump(mydata, open("mydata.p", "wb"))
mydata2 = pickle.load(open("mydata.p", "rb"))

另一种方法是使用类似h5py的东西,它是用于将数据写入HDF5格式的第三方模块。根据您的应用,这可能是一个更高性能的解决方案,因为HDF5在设计时考虑了大型数值数据集。实际上,最新的.mat文件实际上是在hdf5文件source之上设计的。

答案 1 :(得分:1)

你可以使用python中的序列化对象与pickle库或cplicke for(python 2.x),

https://docs.python.org/2/library/pickle.html 例: 如果你想保存为文件

 import pickle
 filetocreate = open('newfile.txt','w')
 pickle.dump(arrayxD, filetocreate, -1 )

然后使用你可以使用

 pickle.load(file)

- #使用可用的最高协议来挑选列表。 = -1 - #if not = 0

请参阅: pickle.HIGHEST_PROTOCOL

An integer, the highest protocol version available. This value can be passed as a protocol value to functions dump() and dumps() as well as the Pickler constructor.

pickle.DEFAULT_PROTOCOL

An integer, the default protocol version used for pickling. May be less than HIGHEST_PROTOCOL. Currently the default protocol is 3, a new protocol designed for Python 3.
相关问题