2

我正在尝试将 .mat 文件中的数据加载到 python 中,以便可以将其保存在 HDF5 文件中。

问题是scipy.io.loadmat返回一个包含 dtype=[('counts', '|O4')]) 数组的字典

每当我尝试使用返回的数组之一创建数据集时,都会收到以下错误:

>> mat = scipy.io.loadmat('state-10.mat')
>> h = h5py.File('test.hdf5','w')
>> h.create_dataset('set', data=mat['ProteinComplex'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/h5py/_hl/group.py", line 69, in create_dataset
    dsid = dataset.make_new_dset(self, shape, dtype, data, **kwds)
  File "/usr/lib/python2.7/dist-packages/h5py/_hl/dataset.py", line 88, in make_new_dset
    tid = h5t.py_create(dtype, logical=1)
  File "h5t.pyx", line 1332, in h5py.h5t.py_create (h5py/h5t.c:11600)
  File "h5t.pyx", line 1373, in h5py.h5t.py_create (h5py/h5t.c:11209)
  File "h5t.pyx", line 1311, in h5py.h5t._c_compound (h5py/h5t.c:10695)
  File "h5t.pyx", line 1332, in h5py.h5t.py_create (h5py/h5t.c:11600)
  File "h5t.pyx", line 1402, in h5py.h5t.py_create (h5py/h5t.c:11465)
TypeError: Object dtype dtype('object') has no native HDF5 equivalent

有什么解决方法吗?

4

1 回答 1

6

原来的类型mat['ProteinComplex']是一个 scipy 多维数组(ndarray)。可以使用该方法将这些转换为列表ndarray.tolist()。如果我们将 nd 数组更改为列表,它将在使用 H5Py 创建数据集时起作用

>> mat = scipy.io.loadmat('state-10.mat')
>> h = h5py.File('test.hdf5','w')
>> protein_complex = mat['ProteinComplex'].tolist()
>> h.create_dataset('ProteinComplex', data=protein_complex)
于 2013-04-18T16:32:56.870 回答