JJson
JJson

Reputation: 233

Coo_matrix and other matrix examples

I understand this might be a very wide topic to ask but I would like to understand more about it. My question is about coo_matrix from sklearn.

I read from the documentation and understand that it represents coordinate matrix for sparse matrix. Is there any meaning to the word "coordinate" here?

For an example,

coo_matrix ((1, 2)) will get me

array([[0, 0]])

So what does the coordinate even mean in this example? Or do we have other example to differentiate it from other matrix? Such as csr_matrix, bsr_matrix or etc..

Again, If I asked the wrong question or this question could be found anywhere, please do let me know or provide me with the links, if possible.

**For the "let me know", just tell me that "You`re asking a too simple question" or "Your question could be found elsewhere, do some research" should be good and then I will delete this post as it is not relevant.

For the "share link", please do let me know where I could study and learn more about it. Thank you in advance. **

Hint: This post according to some experts is considered to be a duplicate post and I have gained some suggestions that I can leave it here if the answer posted by other might be relevant to future readers, thus, I leave it.

Upvotes: 0

Views: 1047

Answers (1)

Joe Patten
Joe Patten

Reputation: 1704

It looks like all you need for a coo matrix is a row list, a column list, and a value (data) list (each list being the same size). It's important that they are the same size as you will go through each element to get a triple (row index, col index, value). This tells the function to put a value at the (row index, col index) of the matrix you are making. I am pretty sure it's called the coordinate format because you pass in coordinates (with row and col) with associated values. Let's do a simple example.

row  = np.array([1,3])
col  = np.array([2,0])
data = np.array([5,11])
print(coo_matrix((data, (row, col)), shape=(4, 4)).toarray())

Looking at the first element of each list:

row[0] = 1 and col[0] = 2, thus we are going to look at the (1,2) element in the matrix (remember in python, indexing starts from 0). value[0] = 5, so the function puts 5 in as as the (1,2) element in the matrix.

Looking at the second element of each list:

row[1] = 3 and col[1] = 0, thus we are going to look at the (3,0) element in the matrix. value[1] = 11, so the function puts 11 in as as the (3,0) element in the matrix.

The rest of the elements (besides those indexed (1,2) and (3,0)) are then 0:

Output:

[[ 0  0  0  0]
 [ 0  0  5  0]
 [ 0  0  0  0]
 [11  0  0  0]]

Upvotes: 3

Related Questions