Home > Mobile >  Find Eigenvector centrality in python
Find Eigenvector centrality in python

Time:09-17

I have a dataframe, which is a weighted edge list:

  from A to B weight 
   1     2     1
   3     5     1
   1     4     1
   4     1     3
   1     3     2
   6     2     1

I am trying to calculate Eigenvector centrality from this weighted edge list.

Any solution, link I can refer to, or just any comments will be helpful. Thanks!

CodePudding user response:

Using the eigenvector_centrality function of networkx (https://networkx.org/):

# Create example DataFrame
df = pd.DataFrame({'source': [1, 3, 1, 4, 1, 6],
                   'target': [2, 5, 4, 1, 3, 2],
                   'weight': [1, 1, 1, 3, 2, 1]})
df
   source  target  weight
0       1       2       1
1       3       5       1
2       1       4       1
3       4       1       3
4       1       3       2
5       6       2       1

# Build graph with networkx
import networkx as nx
G = nx.from_pandas_edgelist(df, edge_attr='weight')

# Compute eigenvector centrality of each node, accounting for edge weights
nx.eigenvector_centrality(G, weight='weight')

{1: 0.6974250778676078,
 2: 0.19770984859637408,
 3: 0.3954196949440728,
 5: 0.10429672377035848,
 4: 0.5518650949515594,
 6: 0.05214836300951692}
  • Related