In [90]: from scipy import sparse
Making the inputs:
In [91]: Y = np.array([0,3,2,1,5,5,3])
In [92]: n=len(Y)
In [93]: data = [1]*n
Making the sparse matrix:
In [94]: M = sparse.coo_matrix((data, (Y, range(n))), shape=(10,n))
Looking at it in various ways:
In [95]: M
Out[95]:
<10x7 sparse matrix of type '<class 'numpy.int32'>'
with 7 stored elements in COOrdinate format>
In [96]: print(M)
(0, 0) 1
(3, 1) 1
(2, 2) 1
(1, 3) 1
(5, 4) 1
(5, 5) 1
(3, 6) 1
In [97]: M.toarray()
Out[97]:
array([[1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
The matrix has 3 key attributes, which should match closely to the inputs:
In [98]: M.data
Out[98]: array([1, 1, 1, 1, 1, 1, 1])
In [99]: M.row
Out[99]: array([0, 3, 2, 1, 5, 5, 3])
In [100]: M.col
Out[100]: array([0, 1, 2, 3, 4, 5, 6], dtype=int32)
Given inputs like this there isn't much to creating the matrix - take inputs as is, or tweaking them a bit to make the first kind of array.
The work is in the methods, such as the toarray, which creates a dense array. How that sparse matrix maps on to that array should be obvious, though the code details are buried. Judging from some errors I've seen, I think it actually does: M.tocsr().toarray(). The csr format implements most of the sparse math, as well as indexing. The coo format is a basic one, most useful for initially creating the matrix.