SH_*_*V95 3 python matlab matrix-multiplication mat-file h5py
我需要在 python 或 matlab 中执行涉及 60000X70000 矩阵的乘法。我有一个 16GB 的 RAM,能够轻松加载矩阵的每一行(这是我所需要的)。我可以在 python 中创建整个矩阵,但不能在 matlab 中创建。无论如何我可以使用 h5py 或 scipy 将数组保存为 v7.3 的 .mat 文件,以便我可以分别加载每一行?
对于 MATLAB v7.3,您可以使用hdf5storagewhich requires h5py,在此处下载文件,解压缩,然后python setup.py install在命令提示符下键入:。
https://pypi.python.org/pypi/hdf5storage
import h5py
import hdf5storage
import numpy as np
matfiledata = {} # make a dictionary to store the MAT data in
matfiledata[u'variable1'] = np.zeros(100) # *** u prefix for variable name = unicode format, no issues thru Python 3.5; advise keeping u prefix indicator format based on feedback despite docs ***
matfiledata[u'variable2'] = np.ones(300)
hdf5storage.write(matfiledata, '.', 'example.mat', matlab_compatible=True)
Run Code Online (Sandbox Code Playgroud)
如果 MATLAB 无法一次加载整个内容,我认为您必须将其保存在不同的变量matfiledata[u'chunk1'] matfiledata[u'chunk2'] matfiledata[u'chunk3']等中。
然后在 MATLAB 中,如果您将每个块保存为变量
load(filename,'chunk1')
do stuff...
clear chunk1
load(filename,'chunk2')
do stuff...
clear chunk2
Run Code Online (Sandbox Code Playgroud)
等等。
hdf5storage.savemat 有一个参数,允许文件在未来正确读入 Python,所以值得一试,并遵循 scipy.io.loadmat 格式......尽管如果将数据从 MATLAB 保存到使导入回 Python 变得容易:
MATLAB
save('example.mat','-v7.3')
Python
matdata = hdf5storage.loadmat('example.mat')
Run Code Online (Sandbox Code Playgroud)
这将作为字典加载回 Python,然后您可以将其转换为您需要的任何数据类型。