SHARE
TWEET

Untitled

a guest Sep 17th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def compute_random_walk(A, walk_length):
  2.     '''Compute random walk based on transition probabilities
  3.     Params:
  4.         A: Adjacency matrix (numpy array)
  5.         walk_length: The length of the walk (int)
  6.     Returns:
  7.         A list of indices passed by the walk
  8.     '''
  9.     passed_node_indices = []
  10.  
  11.     # compute the transition probability matrix
  12.     r_sum = np.sum(A, axis=1)
  13.     P = A/r_sum.reshape(-1,1)
  14.  
  15.     # now extract the nodes based on the walk
  16.     node_indices = np.arange(A.shape[0])
  17.     current_pos = np.random.choice(node_indices, size=1)[0]
  18.     passed_node_indices.append(current_pos)
  19.     for i in range(walk_length):
  20.         pdist = P[current_pos].tolist()
  21.         current_pos = np.random.choice(node_indices, size=1, p=pdist)[0]
  22.         passed_node_indices.append(current_pos)
  23.     assert len(passed_node_indices) == walk_length+1
  24.     return passed_node_indices
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top