如何读取关于矩阵的mat文件?问题:如何把这种数据变为array呢??
```**1.** import scipy.io as io
from scipy.sparse import csc_matrix
f = io.loadmat('D:\QQPCmgr\Desktop\实验代码\S40PI_n1.mat')
print(f.keys())
结果: dict_keys(['__header__', '__version__', '__globals__', 'Problem'])
print(f['Problem'].dtype)
结果: [('name', 'O'), ('title', 'O'), ('A', 'O'), ('id', 'O'), ('date', 'O'), ('author', 'O'), ('ed', 'O'), ('kind', 'O'), ('notes', 'O'), ('aux', 'O')]
print(f['Problem']['A'])
B = f['Problem']['A']
结果:[[<2028x2028 sparse matrix of type '<class 'numpy.float64'>'
with 5007 stored elements in Compressed Sparse Column format>]]
NOTE: 这里是CSC格式!
print(csc_matrix(B.any())
C = csc_matrix(B.any())
结果如下:
(14, 0) -1.0
(94, 1) -1.0
(174, 2) -1.0
(2012, 3) -1.0
(5, 5) -0.022506999298352277
(732, 5) -1.0
(1693, 5) 1.0
(6, 6) -0.04356867079702918
(732, 6) -1.0
(7, 7) -0.3504334461608543
(892, 7) -1.0
(8, 8) -0.01638899233125374
... ...
M = C.toarray()
**2.** import h5py
import pandas as pd
from scipy import sparse
f = h5py.File('D:\QQPCmgr\Desktop\实验代码\data\mycielskian7.mat','r')
print(f.keys())
结果:<KeysViewHDF5 ['#refs#', 'Problem']>
print(f['Problem'].keys())
结果:<KeysViewHDF5 ['A', 'author', 'date', 'ed', 'id', 'kind', 'name', 'notes', 'title']>
print(f['Problem']['A'].keys())
结果:<KeysViewHDF5 ['data', 'ir', 'jc']>
dset = pd.array(f['Problem/A/data'])
print(dset.shape)
结果:(1510,)
rset = pd.array(f['Problem/A/ir'])
cset = pd.array(f['Problem/A/jc'])
NOTE: 这里是CSC格式!
A = sparse.csc_matrix((dset,rset,cset)).toarray