Question: (F). Implement PR_Iterator Define a PR_Iterator class with a custom __next__() method. (This is my PR_DiGraph class: class PR_DiGraph: def __init__(self,data,iteration_limit): self.link_dict=data_to_dictionary(data) self.iteration_limit=iteration_limit def linked_by(self,x):
(F). Implement PR_Iterator
Define a PR_Iterator class with a custom __next__() method.
(This is my PR_DiGraph class:
class PR_DiGraph: def __init__(self,data,iteration_limit): self.link_dict=data_to_dictionary(data) self.iteration_limit=iteration_limit def linked_by(self,x): return(self.link_dict[x]) def __iter__(self): return(PR_Iterator(self))
)
The __init__ method of this class should create instance variables to store the PR_DiGraph object from which it was constructed; a counter i, starting at 0, to log the number of steps taken, and a current_state variable whose value is one of the keys of the link_dict of the Pr_DiGraph. You can choose its initial value arbitrarily; in my solution code I chose self.current_state = "hamilton".
We are going to use iteration to implement the PageRank algorithm. This means we are going to imagine a surfer who is following the links in our data set. Implement the following two methods:
- follow_link().
- Pick a random new character mentioned by the current character, or new airport served by the current airport. Let's call this next_state.
- If next_state != current_state, set current_state to next_state.
- Otherwise (if next_state == current_state), teleport (see below).
- You might run into KeyErrors, in which case you should again teleport (use a try-except block).
- teleport().
- Set the current state to a new state (key of the link dict) completely at random.
Hint: use random.choice from the random module to choose elements of lists.
Finally, implement __next__(). __next__() should do follow_link() with 85% probability, and do teleport() with 15% probability. You should also define a custom StopIteration condition to ensure that only as many steps are taken as the iteration_limit supplied to the PR_DiGraph initializer.
- To do something with 85% probability, use the following:
if random.random() < 0.85: # do the thing else: # do the other thing
Example Usage
After you define your class, run the following code and show that it works. Note: your precise sequence may be different from mine.
D = PR_DiGraph(data, iteration_limit = 5) for char in D: print(char) following link : current state = burr following link : current state = washington following link : current state = burr following link : current state = hamilton teleporting : current state = washington
I have added printed messages here for you to more clearly see what should be happening, but it is not necessary for you to do this. It is sufficient for your output to look like:
D = PR_DiGraph(data, iteration_limit = 5) for char in D: print(char) burr washington burr hamilton washington
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
