[SciPy-User] Why doesn't dot product transpose?

RJ Nowling rnowling at gmail.com
Sat Apr 23 13:19:18 EDT 2011


Thank you for the response.

As you pointed out, the simple answer is to use 1D arrays.  We are
using sparse matrices and want to perform dot products on the column
vectors of those matrices.  When I use csc_matrix.getcol() or
matrix[:,1] syntax, a sparse 2D array is returned with shape (n, 1).
It seems that my approach is getting the proper data structures for
the dot product, as you pointed out.

So, what's the correct way of getting a sparse shape (n,) column
vector from a matrix?  (E.g., we don't want to perform unnecessary
operations by converting to dense representations.)

Thank you,
RJ

> I think the documentation is pretty clear:
>
> For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays
> to inner product of vectors (without complex conjugation). For N dimensions
> it is a sum product over the last axis of a and the second-to-last of b:
> dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
>
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html
>
> The example makes it clear that if a.shape == b.shape == (n,1) then dot(a,b)
> doesn't work.  However, if a.shape == b.shape == (n,), I believe it does
> "the right thing" :-)  Should you be using 1-d arrays instead of 2-d arrays?
>
> Cheers,
>
> Jason

On Fri, Apr 22, 2011 at 6:09 PM, RJ Nowling <rnowling at gmail.com> wrote:
> Hi all,
>
> This is my first time posting to the scipy user mailing list.  I found
> it rather frustrating to get the dot product working for (sparse)
> matrices.  In particular, when trying to use dot product with two
> matrices of nx1, it doesn't perform the transpose of the first matrix.
>  Thus, the dot product is really just an overload of the
> multiplication:
>
>>>> from numpy import ones
>>>> from scipy.sparse import csc_matrix
>>>> m = csc_matrix(ones((5, 4)))
>>>> m
> <5x4 sparse matrix of type '<type 'numpy.float64'>'
>        with 20 stored elements in Compressed Sparse Column format>
>>>> m.getcol(1)
> <5x1 sparse matrix of type '<type 'numpy.float64'>'
>        with 5 stored elements in Compressed Sparse Column format>
>
>
> Attempt to perform dot product:
>>>> m.getcol(1).dot(m.getcol(1))
> ...
> ValueError: dimension mismatch
>
> Works when transposing the first matrix:
>>>> m.getcol(1).transpose().dot(m.getcol(1))
> <1x1 sparse matrix of type '<type 'numpy.float64'>'
>        with 1 stored elements in Compressed Sparse Row format>
>
> Dot product same as matrix multiplication?  Shouldn't it transpose the
> first matrix for you?
>>>> m.getcol(1).transpose() * m.getcol(1)
> <1x1 sparse matrix of type '<type 'numpy.float64'>'
>        with 1 stored elements in Compressed Sparse Row format>
>
> I understand that this is the same convention followed in Numpy, but
> it makes no sense to me.  I thought that u dot v = uT * v ?  Why even
> use the dot product if I still have to do the transpose?
>
> Thank you,
> RJ
>
> --
> em rnowling at gmail.com
> c 954.496.2314
>



-- 
em rnowling at gmail.com
c 954.496.2314



More information about the SciPy-User mailing list