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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Matti Viljamaa
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
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: How does coo_matrix((data, (i, j)), [shape=(M, N)]) work?

Gregory Lee
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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user