在不使用scipy的python中将COO转换为CSR格式

2024-06-09 14:59:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有类似的问题: Convert COO to CSR format in c++ 但是在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 

Tags: toinformatconvertindexvalue格式col
1条回答
网友
1楼 · 发布于 2024-06-09 14:59:09

我知道了如何在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]

相关问题 更多 >