Created
July 24, 2016 15:07
-
-
Save denis-bz/fd5dbe2b11e5ba650781c8e446eb9730 to your computer and use it in GitHub Desktop.
Which {sparse or numpy array} * {sparse or numpyarray} work ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Which {sparse or numpy array} * {sparse or numpy array} work ? | |
Which combinations are valid, what's the result type ? | |
Here try-it-and-see on N^2 combinations | |
with `safe_sparse_dot` from scikit-learn, not "*" . | |
See also: | |
http://scipy-lectures.github.com/advanced/scipy_sparse/ | |
https://scipy.github.io/old-wiki/pages/SciPyPackages/Sparse.html | |
http://stackoverflow.com/questions/tagged/scipy+sparse-matrix (lots) | |
""" | |
# Keywords: scipy sparse dot-product basics tutorial | |
from __future__ import division | |
import sys | |
import numpy as np | |
from scipy import sparse | |
np.set_printoptions( threshold=20, edgeitems=5, linewidth=140, | |
formatter = dict( float = lambda x: "%.2g" % x )) # float arrays %.2g | |
#............................................................................... | |
def safe_sparse_dot( a, b ): | |
""" -> a * b or np.dot(a, b) """ | |
# from sklearn | |
if sparse.issparse(a) or sparse.issparse(b): | |
try: | |
return a * b | |
except ValueError: # dimension mismatch: print shapes | |
print "Error: %s %s * %s %s" % ( | |
type(a).__name__, a.shape, | |
type(b).__name__, b.shape ) | |
raise | |
else: | |
return np.dot(a, b) | |
# def asum( ndarray or sparse ): summary, shape type min av max [density] | |
#............................................................................... | |
n = 10 | |
# to change these params in sh or ipython, run this.py a=1 b=None c=\"str\" ... | |
for arg in sys.argv[1:]: | |
exec( arg ) | |
n = (n // 5) * 5 | |
numpyvec = np.tile( [0,0,0,0,1.], n // 5 ) | |
numpyarray = np.ones(( n, n )) | |
rowvec = sparse.csr_matrix( numpyvec ) # (1, n) | |
colvec = rowvec.T # (n, 1) csc | |
Alil = sparse.lil_matrix((n,n)) | |
Alil.setdiag( np.ones(n) ) | |
Acsr = Alil.tocsr() | |
Acoo = Acsr.tocoo() | |
D = sparse.eye( n ) # scipy.sparse.dia.dia_matrix | |
#............................................................................... | |
print "\n-- safe_sparse_dot( various scipy sparse, numpy array ) --" | |
names = "Acsr Acoo D rowvec colvec numpyvec numpyarray ".split() | |
for Anm in names: | |
A = eval(Anm) | |
print "%s: %s %s " % ( | |
Anm, A.shape, type(A).__name__ ) | |
for Anm in names: | |
A = eval(Anm) | |
for Bnm in names: | |
B = eval(Bnm) | |
print "%s * %s = " % (Anm, Bnm) , | |
try: | |
AB = safe_sparse_dot( A, B ) | |
# AB = A * B | |
print AB.shape, type(AB).__name__ | |
except StandardError, errmsg: | |
# print "Error", errmsg | |
pass | |
print "" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment