Python 如何读取关于矩阵的mat文件?

如何读取关于矩阵的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
  ...             ...
# 转化为array
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'])
# print(dset.shape)

cset = pd.array(f['Problem/A/jc'])
# print(cset.shape)
NOTE: 这里是CSC格式!
# 转化为array
A = sparse.csc_matrix((dset,rset,cset)).toarray


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值