I am trying to implement a PageRank algorithm in python. I have created a function called randomwalk
that takes as input the set edges
, a teleport probability a
and a positive integer iters
and performs the random walk. Starting from any page, the function randomly follows links from one page to the next, teleporting to a completely random page with probability a at each iteration. It stores all visited states.
Now I want to create a histogram of the frequency by which each page was visited. This histogram is what the randomwalk function should return.
My code is below:
import matplotlib.pyplot as plt
edges =[[0,1], [1,1], [2,0], [2,2], [2,3], [3,3], [3,4], [4,6], [5,5], [6,6], [6,3]]
def randomWalk(edges, a ,iters):
pages = list({edge[0] for edge in edges})
dict_edges = {}
for edge_from, edge_to in edges:
if edge_from not in dict_edges:
dict_edges[edge_from] = [edge_to]
else:
dict_edges[edge_from].append(edge_to)
current_page = random.choice(pages)
visit_counts_dictionary = {page:0 for page in pages}
visit_counts_dictionary[current_page] +=1
for _ in range(iters):
if random.uniform(0,1) < a:
current_page = random.choice(pages)
visit_counts_dictionary[current_page] += 1
else:
current_page = random.choice(dict_edges[current_page])
visit_counts_dictionary[current_page] += 1
plt.hist(visit_counts_dictionary)
plt.show()
print(randomWalk(edges, 0, 10))
An empty histogram appears, with the following error:
ValueError: x must be 1D or 2D
Aucun commentaire:
Enregistrer un commentaire