Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy/scipy build adjacency matrix from weighted edgelist

I'm reading a weighted egdelist / numpy array like:

0 1 1
0 2 1
1 2 1
1 0 1
2 1 4

where the columns are 'User1','User2','Weight'. I'd like to perform a DFS algorithm with scipy.sparse.csgraph.depth_first_tree, which requires a N x N matrix as input. How can I convert the previous list into a square matrix as:

0 1 1
1 0 1
0 4 0

within numpy or scipy?

Thanks for your help.

EDIT:

I've been working with a huge (150 million nodes) network, so I'm looking for a memory efficient way to do that.

like image 725
Fabio Lamanna Avatar asked Mar 19 '15 14:03

Fabio Lamanna


2 Answers

You could use a memory-efficient scipy.sparse matrix:

import numpy as np
import scipy.sparse as sparse

arr = np.array([[0, 1, 1],
                [0, 2, 1],
                [1, 2, 1],
                [1, 0, 1],
                [2, 1, 4]])
shape = tuple(arr.max(axis=0)[:2]+1)
coo = sparse.coo_matrix((arr[:, 2], (arr[:, 0], arr[:, 1])), shape=shape,
                        dtype=arr.dtype)

print(repr(coo))
# <3x3 sparse matrix of type '<type 'numpy.int64'>'
#   with 5 stored elements in COOrdinate format>

To convert the sparse matrix to a dense numpy array, you could use todense:

print(coo.todense())
# [[0 1 1]
#  [1 0 1]
#  [0 4 0]]
like image 105
unutbu Avatar answered Sep 20 '22 15:09

unutbu


Try something like the following:

import numpy as np
import scipy.sparse as sps

A = np.array([[0, 1, 1],[0, 2, 1],[1, 2, 1],[1, 0, 1],[2, 1, 4]])
i, j, weight = A[:,0], A[:,1], A[:,2]
# find the dimension of the square matrix
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

print B.todense()
>>>
[[ 0.  1.  1.]
 [ 1.  0.  1.]
 [ 0.  4.  0.]]
like image 21
igavriil Avatar answered Sep 21 '22 15:09

igavriil