DM554/DM545 – Linear and Integer Programming
Sparse MatricesIn several real life applications matrices have often very large dimensions and contain a large number of zero entries. Matrices with these characteristics are called sparse. The performance both in terms of memory space and efficiency of linear algebra operations with these matrices can be improved if zero entries are avoided. Sparse Matrix FormatMore in general sparse matrices can be stored by means of Compressed Sparse Row (CSR)
In [1]:
import scipy as sp
import scipy.sparse as sps
A=sp.array([[1.,0.,2.,0.],
[0.,0.,0.,0.],
[3.,0.,0.,0.],
[1.,0.,0.,4.]])
AS=sps.csr_matrix(A)
print AS.data
print AS.indptr
print AS.indices
print AS.nnz
Compressed Sparse Column (CSC)CSC works similarly but with
In [2]:
A=sp.array([[1.,0.,2.,0.],
[0.,0.,0.,0.],
[3.,0.,0.,0.],
[1.,0.,0.,4.]])
AS=sps.csc_matrix(A)
print AS.data
print AS.indptr
print AS.indices
print AS.nnz
Row-Based Linked List Format (LIL)
In [3]:
A=sp.array([[1.,0.,2.,0.],
[0.,0.,0.,0.],
[3.,0.,0.,0.],
[1.,0.,0.,4.]])
AS=sps.lil_matrix(A)
print "data:", AS.data
print "rows:", AS.rows
print "nnz:", AS.nnz
Altering and slicing are more efficiently executed on the LIL format:
In [4]:
AS[1,0]=17
BS=AS[1:3,0:2]
print BS.data, BS.rows
Generating sparse matrices
In [5]:
E=sps.eye(10,10,format='lil')
D=sps.spdiags(sp.ones((20,)),0,20,20,format='csr')
R=sps.rand(100,200,density=0.1,format='csc')
ConversionsElement-wise operations can be executed only in CSR or CSC format.
In [6]:
AS.toarray
AS.tocsr
AS.tocsc
AS.tolil
sps.issparse(AS)
AS.todense()
Out[6]:
Avoid using Plotting
In [7]:
%matplotlib inline
In [8]:
import matplotlib.pyplot as plt
plt.imshow(R.todense(), interpolation='nearest')
Out[8]:
In [9]:
from pylab import *
matshow(R.todense())
Out[9]:
Memory occupationIn numpy and scipy it is possible to read the number of bytes occupied by an array with th eattribute
In [13]:
sp.arange(100).nbytes
Out[13]:
In [15]:
R.data.nbytes
Out[15]:
In [18]:
R.data.nbytes + R.indptr.nbytes + R.indices.nbytes
Out[18]:
|