[SciPy-User] How does coo_matrix((data, (i, j)), [shape=(M, N)]) work?

Gregory Lee grlee77 at gmail.com
Tue Apr 17 18:20:54 EDT 2018


Hi Matti,

data, i and j are all 1d arrays of matching length.  The order of data
doesn't matter because the corresponding entries in i and j indicate the
row and column indices where the data is stored within the sparse MxN
matrix.

A minimal example that reversing the order of data, i and j gives the same
matrix:

from scipy.sparse import coo_matrix

coo_matrix(([2, 3], ([0, 1], [2, 1])), shape=(3, 3)).todense()
matrix([[0, 0, 2],
        [0, 3, 0],
        [0, 0, 0]])

coo_matrix(([3, 2], ([1, 0], [1, 2])), shape=(3, 3)).todense()
matrix([[0, 0, 2],
        [0, 3, 0],
        [0, 0, 0]])


On Tue, Apr 17, 2018 at 3:53 PM, Matti Viljamaa <mviljamaa at kapsi.fi> wrote:

> I’m confused about the following instantiation of coo_matrix:
>
> coo_matrix((data, (i, j)), [shape=(M, N)])
>
> data contains the entries of the matrix in any order. Why can they be in
> any order? Is data a vector or a matrix?
>
> What are i,j used for?
>
> BR, Matti
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at python.org
> https://mail.python.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20180417/65daf40e/attachment.html>


More information about the SciPy-User mailing list