python - 在没有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中执行它:
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]
推荐阅读
- ibm-midrange - 即使签名 ID 从程序对象到服务程序不同,程序也不会引发签名冲突错误
- sql - Cognos Analytics:关于 CASE WHEN 表达式的 QE-DEF-0459
- neo4j - 在一台机器上运行两个 Neo4j 实例作为 Windows 服务
- php - laravel - 未找到自定义类
- tcl - 如何存储按钮按下的值?
- php - 在方法中使准备好的语句静态
- java - 使用bufferimage java swing绘制多个形状
- haskell - 仍然混淆带有多个箭头的类型签名如何工作
- python - 如何在熊猫中对行进行分组?
- ios - 在 swift 中使用 Multipeer Connectivity 发送 PDF