ramez
ramez

Reputation: 431

transform an adjacency list into a sparse adjacency matrix using python

When using scipy, I was able to transform my data in the following format:

(row, col)  (weight)
(0, 0)      5
(0, 47)     5
(0, 144)    5
(0, 253)    4
(0, 513)    5
...
(6039, 3107)    5
(6039, 3115)    3
(6039, 3130)    4
(6039, 3132)    2

How can I transform this into an array or sparse matrix with zeros for missing weight values as such? (based on the data above, column 1 to 46 should be filled with zeros, and so on...)

0 1 2 3  ... 47 48 49 50
1 [0 0 0 0 ... 5 0 0 0 0
2  2 0 1 0 ... 4 0 5 0 0
3  3 1 0 5 ... 1 0 0 4 2
4  0 0 0 4 ... 5 0 1 3 0
5  5 1 5 4 ... 0 0 3 0 1]

I know it is better in terms of memory to keep the data in the format above, but I need it as a matrix for experimentation.

Upvotes: 5

Views: 3184

Answers (1)

hilberts_drinking_problem
hilberts_drinking_problem

Reputation: 11602

scipy.sparse does it for you.

import numpy as np
from scipy.sparse import dok_matrix

your_data = [((2, 7), 1)]

XDIM, YDIM = 10, 10 # Replace with your values

dct = {}
for (row, col), weight in your_data:
    dct[(row, col)] = weight

smat = dok_matrix((XDIM, YDIM))
smat.update(dct)

dense = smat.toarray()

print dense

'''
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
'''

Upvotes: 7

Related Questions