【问题标题】:Reading MatLab files in python w/ scipy在带有 scipy 的 python 中读取 MatLab 文件
【发布时间】:2014-01-19 21:45:29
【问题描述】:

我正在使用带有 scipy 包的 python 来读取 MatLab 文件。

但是它花费的时间太长并且崩溃了。

Dataset 大小约为 50~ MB

有没有更好的方法来读取数据并形成边缘列表?

我的python代码

import scipy.io as io
data=io.loadmat('realitymining.mat')
print data

【问题讨论】:

  • 没有错误。只是这需要很多时间。
  • 你的问题说“它崩溃了”。
  • 文件是如何创建的?你能把它贴在某个地方吗?
  • 只是未来的一个建议:对大型数据集使用 HDF5。 Matlab 有它的支持函数,Python 以 PyTables 的形式提供了很棒的支持。

标签: python matlab scipy


【解决方案1】:

您可以将结构的每个字段保存在不同的文本文件中,例如:

save('friends.txt', '-struct', 'network', 'friends', '-ascii')

并从 python 中分别加载每个文件

friends = numpy.loadtxt('friends.txt')

立即加载。

【讨论】:

    【解决方案2】:

    解压后可以加载。但它会拉长内存。

    当我尝试使用 octave 加载它时,我得到:

    octave:1> load realitymining.mat
    error: memory exhausted or requested size too large for range of Octave's index type -- trying to return to prompt
    

    在 Ipython 中

    In [10]: data.keys()
    Out[10]: ['network', 's', '__version__', '__header__', '__globals__']
    In [14]: data['__header__']
    Out[14]: 'MATLAB 5.0 MAT-file, Platform: MACI, Created on: Tue Sep 29 20:13:23 2009'
    In [15]: data['s'].shape
    Out[15]: (1, 106)
    In [17]: data['s'].dtype
    Out[17]: dtype([('comm', 'O'), ('charge', 'O'), ('active', 'O'), ('logtimes', 'O'),...  
       ('my_intros', 'O'), ('home_nights', 'O'), ('comm_local', 'O'), ('data_mat', 'O')])
    # 58 fields
    In [24]: data['s']['comm'][0,1].shape
    Out[24]: (1, 30)
    In [31]: data['s']['comm'][0,1][0,1]
    Out[31]: ([[732338.8737731482]], [[355]], [[-1]], [u'Packet Data'], [u'Outgoing'], 
        [[40]], [[nan]])
    In [33]: data['s']['comm'][0,1]['date']
    Out[33]: 
    array([[array([[ 732338.86915509]]), array([[ 732338.87377315]]),
        ...
        array([[ 732340.48579861]]), array([[ 732340.52778935]])]], dtype=object)
    

    看看碎片。简单地尝试print dataprint data['s'] 需要太长时间。显然它的结构太大而无法快速格式化。

    要实际获取这些数据,我建议在 Python 或 Matlab 中加载一次,然后将有用的部分保存到一个或多个文件中。

    【讨论】:

      【解决方案3】:

      也许您可以先将部分数据作为结构中的network 处理,我已使用 MATLAB 将其解压缩为here

      仍在研究如何整理其余更大的结构。

      【讨论】:

        猜你喜欢
        • 2011-02-21
        • 2018-02-05
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-05
        • 1970-01-01
        相关资源
        最近更新 更多