1

I have a question about how PageRank can show the impact of "weight". I want to calculate the PageRank of trade countries using the trade value as a weight, my code is shown below. But what I find is that the results are same as the results that are not weighted. I don't know why.

Could someone help me to understand how to show the "weight" in PageRank calculating?

import networkx as nx
import os
import pandas as pd

data=pd.read_excel('f-e-2016-intermediate-use.xlsx')
G=nx.DiGraph()
teams=data.groupby(['reportercode','partnercode'])
team_names=[name for name,group in teams]
G.add_edges_from(team_names)

a_node=data.groupby(['reportercode'])
source_nodes=[name for name,group in a_node]
b_node=data.groupby(['partnercode'])
target_nodes=[name for name,group in b_node]
nodes=set(source_nodes+target_nodes)
G.add_nodes_from(nodes)

page_rank=nx.pagerank(G,weight='tradevalueus')
Skye
  • 11
  • 2

1 Answers1

0

I've just came across this after looking for the answer myself. What worked for me is simply adding the weight=True command alongside the Pagerank parameters, e.g. for building a page rank score for all nodes in a network:

pagerank_dict = dict(nx.pagerank(G, weight=True)

The only problem might be that you're using a different method than me to read in your edgelist. I suggest using the nx.read_weighted_edgelist feature to load in the node and edge data for your graph. Your excel file should contain three columns with adjacent values for source node, target node, and edge weight (Don't include headers, and save in .csv format). You can then use the following command to load in your data so its guaranteed to work correctly with pagerank:

G = nx.read_weighted_edgelist('f-e-2016-intermediate-use.csv', delimiter=',', create_using = nx.DiGraph(), nodetype=str)
pagerank_dict = dict(nx.pagerank(G, weight=True)
Laurie
  • 1,189
  • 1
  • 12
  • 28