from __future__ import division
import numpy as np
import matplotlib.pyplot as pt
import scipy.sparse as sps
data = [5, 6, 7]
rows = [1, 1, 2]
columns = [2, 4, 6]
A = sps.coo_matrix(
(data, (rows, columns)),
shape=(10, 10), dtype=np.float64)
A
A.todense()
A.nnz
pt.spy(A)
For a COO matrix, the juicy attributes are data
, row
, and col
.
Coordinate format is not the only format. For Compressed Sparse Row, look in data
, indptr
, and indices
.
fill_percent = 5
size = 1000
nentries = size**2 * fill_percent // 100
data = np.random.randn(nentries)
rows = (np.random.rand(nentries)*size).astype(np.int32)
columns = (np.random.rand(nentries)*size).astype(np.int32)
B_coo = sps.coo_matrix(
(data, (rows, columns)),
shape=(size, size), dtype=np.float64)
B_csr = sps.csr_matrix(B_coo)
B_dense = B_coo.todense()
vec = np.random.randn(size)
from time import time
start = time()
for i in xrange(200):
B_dense.dot(vec)
print "time: %g" % (time() - start)