You have an original sparse matrix X:
>>print type(X)
>>print X.todense()
<class 'scipy.sparse.csr.csr_matrix'>
[[1,4,3]
[3,4,1]
[2,1,1]
[3,6,3]]
You have a second sparse matrix Z, which is derived from some rows of X (say the values are doubled so we can see the difference between the two matrices). In pseudo-code:
>>Z = X[[0,2,3]]
>>print Z.todense()
[[1,4,3]
[2,1,1]
[3,6,3]]
>>Z = Z*2
>>print Z.todense()
[[2, 8, 6]
[4, 2, 2]
[6, 12,6]]
What’s the best way of retrieving the rows in Z using the ORIGINAL indices from X. So for instance, in pseudo-code:
>>print Z[[0,3]]
[[2,8,6] #0 from Z, and what would be row **0** from X)
[6,12,6]] #2 from Z, but what would be row **3** from X)
That is, how can you retrieve rows from Z, using indices that refer to the original rows position in the original matrix X? To do this, you can’t modify X in anyway (you can’t add an index column to the matrix X), but there are no other limits.
If you have the original indices in an array
i, and the values iniare in increasing order (as in your example), you can use numpy.searchsorted(i, [0, 3]) to find the indices in Z that correspond to indices [0, 3] in the original X. Here’s a demonstration in an IPython session:In [39]: X = csr_matrix([[1,4,3],[3,4,1],[2,1,1],[3,6,3]]) In [40]: X.todense() Out[40]: matrix([[1, 4, 3], [3, 4, 1], [2, 1, 1], [3, 6, 3]]) In [41]: i = array([0, 2, 3]) In [42]: Z = 2 * X[i] In [43]: Z.todense() Out[43]: matrix([[ 2, 8, 6], [ 4, 2, 2], [ 6, 12, 6]]) In [44]: Zsub = Z[searchsorted(i, [0, 3])] In [45]: Zsub.todense() Out[45]: matrix([[ 2, 8, 6], [ 6, 12, 6]])