Advertisement
Guest User

Untitled

a guest
May 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import scipy.sparse as ssp
  2. import numpy as np
  3. from sklearn.preprocessing import normalize
  4. from numba.typed import Dict
  5. from numba import jit, types, njit, prange
  6.  
  7.  
  8. @njit(parallel=True, nogil=True)
  9. def reverse_push_bipartite(A_indptr, A_indices, A_data, nL, nR, r_max, alpha):
  10. '''
  11. Compute significant PPRs based on 2-hop random walks with restart on the
  12. right side.
  13.  
  14. A_indptr: CSC indptr
  15. A_indices: CSC indices
  16. A_data: CSC data
  17. nL: number of nodes on left side
  18. nR: number of nodes on right side
  19. r_max: residual, a small positive real number
  20. alpha: restart probability
  21.  
  22. Return:
  23. List[Dict[int, float]]
  24. For nL <= v < nL + nR, if PPR(u, v) > r_max, then it would show up as
  25.  
  26. PPR(u, v) = result[v - nL][u]
  27.  
  28. with an error of o(r_max)
  29. '''
  30. result = [Dict.empty(key_type=types.int64, value_type=types.float64)
  31. for _ in range(nL, nL + nR)]
  32.  
  33. for t in prange(nL, nL + nR):
  34. r = Dict.empty(key_type=types.int64, value_type=types.float64)
  35. p = Dict.empty(key_type=types.int64, value_type=types.float64)
  36. r[t] = 1.
  37. while True:
  38. for v, r_v in r.items():
  39. if r_v > r_max:
  40. break
  41. else:
  42. break
  43. A_v_indices = A_indices[A_indptr[v]:A_indptr[v+1]]
  44. A_v_data = A_data[A_indptr[v]:A_indptr[v+1]]
  45. a = alpha if v >= nL else 0
  46. for i, d in zip(A_v_indices, A_v_data):
  47. r[i] = r.get(i, 0) + (1 - a) * d * r_v
  48. if a > 0:
  49. p[v] = p.get(v, 0) + a * r_v
  50. del r[v]
  51.  
  52. result[t - nL] = p
  53. print(t)
  54.  
  55. return result
  56.  
  57. if __name__ == '__main__':
  58. # 0, 1, 2, 3 are on the left
  59. # 4, 5, 6, 7 are on the right
  60. # We want to compute all PPR(u, v) > 0.01 with restart probability 0.2,
  61. # based on 2-hop random walks on the right side.
  62. A = ssp.coo_matrix(
  63. (np.ones(9),
  64. # source nodes destination nodes
  65. ([0, 0, 1, 2, 3, 4, 5, 6, 7], [5, 6, 6, 7, 6, 0, 1, 2, 3])))
  66. A = normalize(A, norm='l1', axis=1).tocsc()
  67. p = reverse_push_bipartite(
  68. A.indptr, A.indices, A.data, 4, 4, 0.01, 0.2)
  69. print('Converting to dict')
  70. p = [dict(d) for d in p]
  71. for i, d in enumerate(p):
  72. for u, v in d.items():
  73. print('PPR(%d, %d) = %f' % (u, 4 + i, v))
  74. # Result:
  75. # PPR(4, 4) = 0.200000
  76. # PPR(5, 5) = 0.200000
  77. # PPR(4, 5) = 0.080000
  78. # PPR(6, 6) = 0.551456
  79. # PPR(4, 6) = 0.390993
  80. # PPR(5, 6) = 0.439320
  81. # PPR(7, 6) = 0.439320
  82. # PPR(7, 7) = 0.551456
  83. # PPR(6, 7) = 0.439320
  84. # PPR(4, 7) = 0.310993
  85. # PPR(5, 7) = 0.351456
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement