Question: Hi I am trying to convert a sparse matrix COO format row list to CSR format row ptr in an efficient way because the matrix

Hi

I am trying to convert a sparse matrix COO format row list to CSR format row ptr in an efficient way because the matrix has a millions row.

For example:

coo_row_list=[0,0,0,1,1,1,2,2,2,2,3,3,3,3,4,4,4,5,5,5,6,6]

Output:

csr_pt=[0,3,6,10,14,17,20,22]

which 0 is the index of first "0", 3 is the first index of "1", 6 is the 2nd index of "2", etc

I wrote the below code, it works on the example, however, when I run it on the actual matrix (a million number), it got stuck and fail. Do you have an more efficient way?

A_coo_row=[0,0,0,1,1,1,2,2,2,2,3,3,3,3,4,4,4,5,5,5,6,6]

A_set=set(A_coo_row)

for i in A_set:

d[i]=A_coo_row.count(i)

csr_ptrs=[0]

index=0

for key in d.keys():

index+=d[key]

csr_ptrs.append(index)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!