8
$\begingroup$

I have a large sparse matrix as the output and I want to save the output and open it as a matrix in python. The output is a 6000 x 6000 matrix and I don't know how to make changes to the file such that it opens as a matrix in python with the correct format i.e. [[1 0 0 ...],[1 ... 0],[...]]

$\endgroup$
3
  • $\begingroup$ So your matrix is sparse, right? $\endgroup$ Jul 15, 2016 at 13:13
  • $\begingroup$ Yeah, the matrix is sparse. It is an incidence matrix that I got using data from a bipartite graph. $\endgroup$ Jul 16, 2016 at 5:37
  • 1
    $\begingroup$ Shoulda mentioned that to begin with. $\endgroup$ Jul 17, 2016 at 9:02

2 Answers 2

8
$\begingroup$

I think for this kind of matrix it is better to use some of the dedicated matrix formats, like, "MTX" (of Matrix Market) or "HarwellBoeing". Below are two examples using "MTX": one with a dense 6000x6000 matrix and one with a sparse matrix.

Dense matrix

Mathematica

mat = RandomReal[{0, 1}, {6000, 6000}];
Export["/path/RandomMat.mtx", mat, "MTX"]

Python

>>> from numpy import array, matrix
>>> from scipy.io import mmread, mmwrite
>>> A = mmread("/path/RandomMat.mtx")
A = mmread("/path/RandomMat.mtx")
>>> A
A
array([[ 0.78126179,  0.35847388,  0.68474351, ...,  0.583692  ,
         0.80156363,  0.04288801],
       [ 0.28195085,  0.25645955,  0.25975606, ...,  0.757864  ,
         0.77155917,  0.23542805],
       [ 0.60189668,  0.99310095,  0.25748816, ...,  0.13496221,
         0.8505471 ,  0.49982663],
       ..., 
       [ 0.46228175,  0.72703853,  0.16218388, ...,  0.34214427,
         0.58023789,  0.06454413],
       [ 0.21502725,  0.01252058,  0.06348292, ...,  0.79366921,
         0.0165515 ,  0.36357833],
       [ 0.43135125,  0.38405678,  0.02897838, ...,  0.69190792,
         0.51890882,  0.52854666]])

Sparse matrix

Mathematica

gr = RandomGraph[{100, 200}];
grmat = AdjacencyMatrix[gr]
Export["/path/RandomGraphMat.mtx", grmat, "MTX"]

Python

>>> from numpy import array, matrix
>>> from scipy.sparse import csr_matrix, lil_matrix
>>> from scipy.io import mmread, mmwrite
>>> B = mmread("/path/RandomGraphMat.mtx")
B = mmread("/path/RandomGraphMat.mtx")
>>> B
B
<100x100 sparse matrix of type '<type 'numpy.int32'>'
$\endgroup$
2
  • $\begingroup$ I'm getting this error. I had to used a rectangular matrix. IncidenceMatrix[SparseArray[Automatic, {6613, 6700}, 0, {<<1>>}]] cannot be exported to the MatrixMarket format $\endgroup$ Jul 18, 2016 at 3:56
  • $\begingroup$ @jazzytortoise It seems that the object you are trying to export is not a matrix. $\endgroup$ Jul 18, 2016 at 11:21
4
$\begingroup$
SetDirectory[NotebookDirectory[]]
(mat = RandomReal[1, {5, 5}]) // MatrixForm

Mathematica graphics

Export["mat.txt", mat, "CSV"]

Open python and type

>python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2

>>> f = open ( 'mat.txt' , 'r')
>>> mat =  [ map(float,line.split(',')) for line in f ]
>>> print(mat)

[[0.2801766771080665, 0.616846438123569, 0.5567596750781629,    0.9343768938387196, 0.6557793334016284], 
[0.18935521644817044, 0.3006971747527434, 0.8805922006965157, 0.4281395790014586, 0.6450351079034391],
[0.7002938047712715, 0.9699463636247314, 0.7421654618136742, 0.7459397473691496, 0.8102422840022152], 
[0.753607490381512, 0.37223385719235136, 0.5181277708575103, 0.9934363269082769, 0.9389620252730952], 
[0.7270038190913004, 0.33165312990950047, 0.42576541711433946, 0.9697810988851399, 0.5567071842453715]]
>>> mat[0][0]
0.2801766771080665
>>> mat[0][1]
0.616846438123569
>>> 

Another option is to use CSV module in Python to read the matrix.

$\endgroup$
2
  • $\begingroup$ I'm not able to print mat[0][1]. It shows me this error. TypeError: 'map' object is not subscriptable $\endgroup$ Jul 18, 2016 at 3:59
  • $\begingroup$ @jazzytortoise It works for me. May be different Python version? I am using 2.7.6 on Linux. $\endgroup$
    – Nasser
    Jul 19, 2016 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.