egde.txt
):
1 1 0.00000000000000000000
1 2 0.25790529076045041
1 3 0.77510411846367422
2 1 0.34610027855153203
2 2 0.00000000000000000000
2 3 0.43889275766016713
3 1 0.75335810231494713
3 2 0.22234924264075450
3 3 0.00000000000000000000
边的权重是浮动值,如图所示,分隔符是空白,我必须在文本文件中保持这种方式。我想将此边缘列表转换为
矩阵
1 2 3
1 0.000000 0.257905 0.775104
2 0.346100 0.000000 0.438893
3 0.753358 0.222349 0.000000
txttocsv2.py
)我认为这是可行的,但不幸的是没有:
import numpy as np
import scipy.sparse as sps
import csv
import pandas as pd
with open('connectivity.txt', 'r') as fil:
A = np.genfromtxt(fil)
i, j, weight = A[:,0], A[:,1], A[:,2]
dim = max(len(set(i)), len(set(j)))
B = sps.lil_matrix((dim, dim))
for i,j,w in zip(i,j,weight):
B[i,j] = w
for row in B: #I want to print the output as well to see if it works
print(row)
with open("connect.csv", "wb") as f:
for row in B:
writer = csv.writer(f)
writer.writerow(B)
错误是:
Traceback (most recent call last):
File "txttocsv2.py", line 16, in <module>
B[i,j] = w
File "/home/osboxes/pymote_env/local/lib/python2.7/site-packages/scipy/sparse/lil.py", line 379, in __setitem__
i, j, x)
File "scipy/sparse/_csparsetools.pyx", line 231, in scipy.sparse._csparsetools.lil_fancy_set (scipy/sparse/_csparsetools.c:5041)
File "scipy/sparse/_csparsetools.pyx", line 376, in scipy.sparse._csparsetools._lil_fancy_set_int32_float64 (scipy/sparse/_csparsetools.c:7021)
File "scipy/sparse/_csparsetools.pyx", line 87, in scipy.sparse._csparsetools.lil_insert (scipy/sparse/_csparsetools.c:3216)
IndexError: column index (3) out of bounds