using eigenvector centrality for weighted graph in python

1.2k Views Asked by At

I want to use network centrality to a similarity matrix. It means that I have a similarity matrix as below:

similarity matrix:[[1,0.1,1,0.4],
                   [0.13,1,0.9,0.6],
                   [0.6,0.1,1,0.11],
                   [0.5,0.23,0.43,1]]

Then I created a weighted graph for this matrix. But I don't know how can I create eigenvector centrality for this graph. I used from below code to do this:

centrality = nx.eigenvector_centrality_numpy(G, weight='weight')

But can anybody help me to know what means weight in this code? what should I replace instead of it? my graph is weighted by itself. How can I show it in this code?

1

There are 1 best solutions below

0
On

So if you created the graph in the following way:

G = nx.from_numpy_array(sim_matrix)

then the graph is weighted by default with your weights. Each edge has an attribute 'weight'. You can test it by running the following code:

G.edges(data=True)

EdgeDataView([(0, 0, {'weight': 1.0}), (0, 1, {'weight': 0.13}), (0, 2, {'weight': 0.6}), (0, 3, {'weight': 0.5}), (1, 1, {'weight': 1.0}), (1, 2, {'weight': 0.1}), (1, 3, {'weight': 0.23}), (2, 2, {'weight': 1.0}), (2, 3, {'weight': 0.43}), (3, 3, {'weight': 1.0})])

You can define different attributes to each edge/node see networkx documentation

Therefore when you use eigenvector_centrality_numpy you can define the attribute for the weight. In your case it 'weight' which also a default in documentation.