Hi,
I'm now testing dot product and using the following:
import numpy as np, scipy.sparse as sp
A = np.matrix(np.zeros((5,10))) B = np.zeros((10,1)) print (A*B).shape print np.dot(A,B).shape
A = sp.csr_matrix(np.zeros((5,10))) B = sp.csr_matrix((10,1)) print (A*B).shape print np.dot(A,B).shape
A = sp.csr_matrix(np.zeros((5,10))) B = np.zeros((10,1)) print (A*B).shape print np.dot(A,B).shape
I got:
(5, 1) (5, 1) (5, 1) (5, 1) (5, 1) (10, 1)
Obviously, the last computation is not a dot product, but I got no warning at all. Is that the expected behavior ?
By the way, I wrote a speed benchmark for dot product using the different flavors of sparse matrices and I wonder if it should go somewhere in documentation (in anycase, if anyone interested, I can post the benchmark program and result).
Nicolas
I'd be interested to see the benchmark ;)
On Thu, May 28, 2009 at 4:14 PM, Nicolas Rougier Nicolas.Rougier@loria.fr wrote:
Hi,
I'm now testing dot product and using the following:
import numpy as np, scipy.sparse as sp
A = np.matrix(np.zeros((5,10))) B = np.zeros((10,1)) print (A*B).shape print np.dot(A,B).shape
A = sp.csr_matrix(np.zeros((5,10))) B = sp.csr_matrix((10,1)) print (A*B).shape print np.dot(A,B).shape
A = sp.csr_matrix(np.zeros((5,10))) B = np.zeros((10,1)) print (A*B).shape print np.dot(A,B).shape
I got:
(5, 1) (5, 1) (5, 1) (5, 1) (5, 1) (10, 1)
Obviously, the last computation is not a dot product, but I got no warning at all. Is that the expected behavior ?
By the way, I wrote a speed benchmark for dot product using the different flavors of sparse matrices and I wonder if it should go somewhere in documentation (in anycase, if anyone interested, I can post the benchmark program and result).
Nicolas
Numpydiscussion mailing list Numpydiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Hi,
I tried to post results but the file is too big, anyway, here is the benchmark program if you want to run it:
Nicolas

import time import numpy from scipy import sparse
def benchmark(xtype = 'numpy.array', xdensity = 0.1, ytype = 'numpy.array', ydensity = 1.0, n = 1000):
x = numpy.zeros((n,n), dtype = numpy.float64) xi = int(n*n*xdensity) x.reshape(n*n)[0:xi] = numpy.random.random((xi,)) y = numpy.zeros((n,1), dtype = numpy.float64) yi = int(n*ydensity) y.reshape(n)[0:yi] = numpy.random.random((yi,)) x = eval('%s(x)' % xtype) y = eval('%s(y)' % ytype) t0 = time.clock() if xtype == 'numpy.array' and ytype == 'numpy.array': for i in range(1000): z = numpy.dot(x,y) else: for i in range(1000): z = x*y tf = time.clock()  t0 text = '' text += (xtype + ' '*20)[0:20] text += (ytype + ' '*20)[0:20] text += '%4dx%4d %4dx%4d %.2f %.2f %.2f' % (n,n,n,1,xdensity, ydensity, tf) return text
xtypes = ['numpy.array', 'numpy.matrix', 'sparse.lil_matrix', 'sparse.csr_matrix', 'sparse.csc_matrix'] ytypes = ['numpy.array', 'numpy.matrix', 'sparse.lil_matrix', 'sparse.csr_matrix', 'sparse.csc_matrix'] xdensities = [0.01, 0.10, 0.25, 0.50, 1.00] ydensities = [1.00]
print '=================== =================== =========== =========== =========== =========== =======' print 'X type Y type X size Y size X density Y density Time ' print '      ' n = 100 for xdensity in xdensities: for ydensity in ydensities: for xtype in xtypes: for ytype in ytypes: print benchmark(xtype, xdensity, ytype, ydensity, n) print '      ' n = 1000 for xdensity in xdensities: for ydensity in ydensities: for xtype in xtypes: for ytype in ytypes: print benchmark(xtype, xdensity, ytype, ydensity, n) print '      ' print '=================== =================== =========== =========== =========== =========== ======='
On Thu, May 28, 2009 at 10:14 AM, Nicolas Rougier Nicolas.Rougier@loria.fr wrote:
Obviously, the last computation is not a dot product, but I got no warning at all. Is that the expected behavior ?
Sparse matrices make no attempt to work with numpy functions like dot(), so I'm not sure what is happening there.
By the way, I wrote a speed benchmark for dot product using the different flavors of sparse matrices and I wonder if it should go somewhere in documentation (in anycase, if anyone interested, I can post the benchmark program and result).
Sparse dot products will ultimately map to sparse matrix multiplication, so I'd imagine your best bet is to use A.T * B (for column matrices A and B in csc_matrix format).
participants (3)

Nathan Bell

Nicolas Rougier

Sebastian Walter