# [SciPy-User] Dotting two sparse rows

Warren Weckesser warren.weckesser@enthought....
Thu Nov 10 07:01:28 CST 2011

```On Thu, Nov 10, 2011 at 5:36 AM, Matt Henderson <matthen@gmail.com> wrote:

> Hi there,
>
> I need to dot two csr matrices with shapes (1, 25675), (1, 25675)
> together, giving a single number.  These rows are extremely sparse- with
> around 20 non-zero entries each.  What's the fastest way to do this? I know
> the following works:
>
> np.dot(a, b.T).todense()[0,0]
>
>
Looks like a.multiply(b).sum() is faster:

In [248]: a
Out[248]:
<1x30000 sparse matrix of type '<type 'numpy.float64'>'
with 81 stored elements in Compressed Sparse Row format>

In [249]: b
Out[249]:
<1x30000 sparse matrix of type '<type 'numpy.float64'>'
with 77 stored elements in Compressed Sparse Row format>

In [250]: %timeit np.dot(a, b.T).todense()[0,0]
1000 loops, best of 3: 470 us per loop

In [251]: %timeit a.dot(b.T).todense()[0,0]
1000 loops, best of 3: 408 us per loop

In [252]: %timeit a.multiply(b).sum()
10000 loops, best of 3: 182 us per loop

Warren

Cheers,
> Matt
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20111110/dfceb242/attachment.html
```