chl
chl

Reputation: 385

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?

Upvotes: 1

Views: 2482

Answers (2)

fishbacp
fishbacp

Reputation: 1263

Here's how I compute betweenness for a weighted graph, where G has been created using the directions from @yatu and networkx has been imported as nx:

BC = nx.betweenness_centrality(G, normalized=False, weight='weight')

And here's a function to compute and compare BC values using a simple bar graph. The argument "labels" is a dictionary of labels, where values are strings, e.g. something of the form

labels={0:'A',1:'B',2:'C',3:'D'}

def network_betweenness_bar(G, labels):
    # Compute Betweenness Centrality Values and plot as bar graph
    BC = nx.betweenness_centrality(G, normalized=False, weight='weight')
    BC = list(BC.values())
    BC = [round(elem, 2) for elem in BC]
    x = np.arange(len(labels))
    axis_labels = list(labels.values());
    plt.bar(x, BC)
    plt.xticks(x, axis_labels)
    plt.title('Betweenness Centrality', fontsize=18)
    return BC

Upvotes: 1

yatu
yatu

Reputation: 88305

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.

Upvotes: 2

Related Questions