Search code examples
pythonnetworkxadjacency-matrix

how can i add weight to networkx edges?


i have basic dataframe which looks like this

     A  B  C  D ... Z
foo1 1  0  1  0 ... 0
foo2 0  0  0  1 ... 0
foo3 0  1  0  0 ... 1
foo4 1  0  1  1 ... 0

(actual shape = 330, 1113) and i transformed this to adjacency matrix

   A  B  C  D ... Z
A
B
C
D
..
Z

(actual shape = 1113, 1113)

this matrix have only binary values, and i could get graph's several centralities(degree, closeness, betweenness) using networkx

and then, i gave some values to dataframe like

     A  B  C  D ... Z
foo1 3  0  3  0 ... 0
foo2 0  0  0  2 ... 0
foo3 0  5  0  0 ... 5
foo4 4  0  4  4 ... 0

(actual shape = 330, 1113 and values in a row are all same)

also i transformed this to adjacency matrix and calculate centralities but i have the same result with the binary values.

is this situation normal? i thought those centralities would be different because of the weight, but it isn't.

i want the column(e.g. A) with high value would be more central, but the both result are same.

why this happening? how can i solve it?


Solution

  • When you use nx.from_numpy_array, the values are from the fed adjacency array are set as edge weights, so that should do as you want. Using some example array:

    df.values
    array([[6, 0, 4, 0],
           [0, 0, 0, 1],
           [0, 2, 0, 0],
           [1, 0, 4, 8]])
    
    G = nx.from_numpy_array(df.values)
    
    G.edges(data=True)
    #EdgeDataView([(0, 0, {'weight': 1}), (0, 2, {'weight': 1})...
    

    As per the various centrality algorithms available in Centrality, for weights to be considered you have to modify the attribute names, so that they are taken into account. For instance, for the closeness_centrality, from the docs:

    If the ‘distance’ keyword is set to an edge attribute key then the shortest-path length will be computed using Dijkstra’s algorithm with that edge attribute as the edge weight.