繁体   English   中英

如何将稀疏矩阵转换为 Python 中的.mat 文件中的密集矩阵?

[英]How to convert a sparse matrix to a dense matrix from .mat file in Python?

import xlrd, xlwt
import scipy as sp
import scipy.io
import pandas as pd
import matplotlib.pyplot as plt
import pylab
from scipy.sparse import csc_matrix
%matplotlib inline

FileString = r'/content/drive/MyDrive/Thesis/EXIOBASE_3rx_aggLandUseExtensions_2015_pxp.mat'
MRIO = scipy.io.loadmat(FileString)

Regions = MRIO['IO']['A']

IN: Regions
OUT: array([[<42800x42800 sparse matrix of type '<class 'numpy.float64'>'
    with 5986549 stored elements in Compressed Sparse Column format>]],
      dtype=object)

IN: Regions.todense()
OUT: 
AttributeError                            Traceback (most recent call last)
<ipython-input-116-3ef413dd7ae9> in <module>()
----> 1 Regions.todense()

AttributeError: 'numpy.ndarray' object has no attribute 'todense'

我正在尝试将此稀疏矩阵从 MATLAB 文件转换为密集矩阵,因此我应用了 todense() function 但我不知道为什么它不起作用。 您的帮助将不胜感激谢谢

你需要解压:

array([[<42800x42800 sparse matrix of type '<class 'numpy.float64'>'
with 5986549 stored elements in Compressed Sparse Column format>]],
  dtype=object)

这是一个ndarray ,如错误所示。 它具有形状 (1,1), object 那一个 object 是稀疏矩阵。 我从显示中推断出, arrayobject和 [[...]]`。

x[0,0].todense()
x[0,0].A        # for array

应该管用。

我建议先显示

 x[0,0]

我怀疑在 MATLAB 这是一个cell ,大小为 (1,1)。 loadmat使用object dtype arrays 来保存 MATLAB cellstruct 只有纯矩阵才能成为数值数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM