Skip to content

Instantly share code, notes, and snippets.

@mneedham
Created July 18, 2018 10:10
Show Gist options
  • Save mneedham/fb334cdce09b1f16dbe56fd017fdcdcb to your computer and use it in GitHub Desktop.
Save mneedham/fb334cdce09b1f16dbe56fd017fdcdcb to your computer and use it in GitHub Desktop.
Personalized PageRank using networkx
# Dataset from https://blogs.oracle.com/bigdataspatialgraph/intuitive-explanation-of-personalized-page-rank-and-its-application-in-recommendation
import operator
import networkx as nx
G = nx.Graph()
G.add_nodes_from(["John", "Mary", "Jill", "Todd",
"iPhone5", "Kindle Fire", "Fitbit Flex Wireless", "Harry Potter", "Hobbit"])
G.add_edges_from([
("John", "iPhone5"),
("John", "Kindle Fire"),
("Mary", "iPhone5"),
("Mary", "Kindle Fire"),
("Mary", "Fitbit Flex Wireless"),
("Jill", "iPhone5"),
("Jill", "Kindle Fire"),
("Jill", "Fitbit Flex Wireless"),
("Todd", "Fitbit Flex Wireless"),
("Todd", "Harry Potter"),
("Todd", "Hobbit"),
])
pr = nx.pagerank(G)
pr = sorted(pr.items(), key=operator.itemgetter(1), reverse=True)
print("PageRank")
for item, score in pr:
print(item, score)
print("")
print("Personalised PageRank")
ppr = nx.pagerank(G, personalization={"John": 1})
ppr = sorted(ppr.items(), key=operator.itemgetter(1), reverse=True)
for item, score in ppr:
print(item, score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment