0
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'

I am trying to convert this sparse matrix from a MATLAB file into a dense matrix, thus I applied todense() function but I don't know why it doesn't work. Your help will be highly appreciated Thanks

akif
  • 1
  • 1

1 Answers1

0

You need to unpack:

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

This is a ndarray as shown by the error. It has shape (1,1), object dtype. That one object is the sparse matrix. I deduced that from the display, the array, object and [[...]]`.

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

should work.

I'd suggest first displaying

 x[0,0]

I suspect in MATLAB this is a cell, with size (1,1). loadmat makes a liberal use of object dtype arrays to hold MATLAB cell and struct. Only a pure matrix becomes a numeric array.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Thank you so very much for your advice, In MATLAB it's a "42800x42800 sparse double" – akif Apr 23 '21 at 07:46