numpy - Python: Edge List to an Adjacency Matrix using SciPy/Pandas shows IndexError: column index (3) out of bounds -


i have text file edge list (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   

the weights of edges floating values seen , separators white spaces must keep way in text file. want convert edge list matrix following , store in csv file:

    1         2         3 1   0.000000  0.257905  0.775104 2   0.346100  0.000000  0.438893 3   0.753358  0.222349  0.000000   

i have following code (txttocsv2.py) thought work, unfortunately not:

import numpy np import scipy.sparse sps import csv import pandas pd  open('connectivity.txt', 'r') fil:      = 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))     i,j,w in zip(i,j,weight):         b[i,j] = w      row in b: #i want print output see if works         print(row)      open("connect.csv", "wb") f:         row in b:             writer = csv.writer(f)             writer.writerow(b)   

the error is:

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   

could point out code failing , me out?
in advance :)
using ubuntu 14.04 32-bit vm , python 2.7

your code tries access location i,j in matrix b. problem i , j one-based , matrix zero-based. should switch b[i-1,j-1] = w. also, need change row writer.writerow(b) writer.writerow(row).

or john galt said, use pandas pivot:

import pandas pd

pd.read_csv('edge.txt', delimiter=' ', header=none).pivot(0,1,2).to_csv('connect.csv', header=false, index=false)


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -