I can’t wrap my head around csr_matrix
examples in scipy documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
Can someone explain how this example work?
JavaScript
x
8
1
>>> row = np.array([0, 0, 1, 2, 2, 2])
2
>>> col = np.array([0, 2, 2, 0, 1, 2])
3
>>> data = np.array([1, 2, 3, 4, 5, 6])
4
>>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
5
array([[1, 0, 2],
6
[0, 0, 3],
7
[4, 5, 6]])
8
I believe this is following this format.
csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
where data, row_ind and col_ind satisfy the relationship a[row_ind[k], col_ind[k]] = data[k].
What is a
here?
Advertisement
Answer
This is a sparse matrix. So, it stores the explicit indices and values at those indices. So for example, since row=0 and col=0 corresponds to 1 (the first entries of all three arrays in your example). Hence, the [0,0] entry of the matrix is 1. And so on.