首页 > 解决方案 > 在没有scipy的python中将COO转换为CSR格式

问题描述

我有类似的问题: Convert COO to CSR format in c++ but in python。

我不想使用 SciPy。

首席运营官格式:

row_index col_index value
      1         1         1
      1         2        -1
      1         3        -3   
      2         1        -2
      2         2         5
      3         3         4
      3         4         6
      3         5         4
      4         1        -4
      4         3         2
      4         4         7     
      5         2         8
      5         5        -5 

期望的输出:

row_index col_index value
      0         1         1
      2         2        -1
      4         3        -3   
      7         1        -2
      8         2         5
                3         4
                4         6
                5         4
                1        -4
                3         2
                4         7     
                2         8
                5        -5 

标签: python

解决方案


我想出了如何在python中执行它:

nnz= len(value)
rows= max (row_index)+1
csr_row=[0]*(rows+1)

for i in range(nnz):
    csr_row[coo_row[i]+1]=csr_row[coo_row[i]+1]+1
for i in range(rows):
    csr_row[i+1]=csr_row[i+1]+csr_row[i]
    print("after: " , csr_row) # this helps the user follow along

输出:

after: [0, 2, 1, 3]
after: [0, 2, 3, 3]
after: [0, 2, 3, 6]

推荐阅读