2012-11-19 103 views
2

我有一个运行在pypy中的主程序,可以创建三个2D numpy数组。我想将它们保存到一个文件中,然后使用python打开它们并使用matplotlib.pyplot绘制它们。是否有替代numpy.save(文件,ARR),将与pypy一起工作?

当前pypy不能与numpy.save一起使用,是否有一种简单的替代方法可以在使用pypy的同时将一组numpy数组保存到文件中?

回答

3

库腌菜与pypy一起使用。他是我如何保存/加载numpy的阵列

import pickle 
import numpy 

保存(使用pypy):

outfile1 = open(r'C:\pythontmp\numpyArray.pkl', 'w+b') 
pickle.dump(numpyArray.tolist(), outfile1) 
outfile1.close() 

负载(使用python):

infile1 = open(r'C:\pythontmp\numpyArray.pkl', 'r+b') 
file1 = pickle.load(infile1)       # This is a list 
infile1.close() 

numpyArray = numpy.array(file1)      # This is a numpy array 
1

您可能可以使用ndarray.tofile()和numpy.fromfile()。这会失去在具有不同字节顺序的机器之间移动数据的能力,但应该比save()更快。

例子:

a = numpy.zeros((5,5)) 
a.tofile('a.dat') 
b = numpy.fromfile('a.dat') 
+4

'tofile'和'fromfile'也[不支持](http://buildbot.pypy.org/numpy-status/latest.html)通过pypy。 – tiago

1

您可以尝试使用Python的struct模块。在答案herehere中有一些例子。

另一种选择是使用外部库,如pyfits或pytables。但我怀疑他们可能无法在pypy上使用。

相关问题