PageRank algorithm (Python implementation)

Source: Internet
Author: User

Python implements the PageRank algorithm, purely using the Python native module, without using NumPy, scipy. The implementation of this program is relatively primitive, can be optimized more places.

#-*-coding:utf-8-*-import randomn = 8 #八个网页d = 0.85 #阻尼因子为0.85delt = 0.00001# Iteration control variable # two matrix multiply def matrix_multi (A, b): Resul t = [[0]*len (B[0]) for I in Range (Len (a))]for I in range (Len (a)): for J in Range (Len (b[0]): For K in range (len (B)): Result[i ][J] + = A[i][k]*b[k][j] return result# each element of matrix A is multiplied by ndef Matrix_multin (n,a): result = [[1]*len (A[0]) for I in Range (len (A))]f or I in range (len (A)): for J in Range (Len (a[0])): result[i][j] = N*a[i][j]return result# two matrices add def matrix_add (A, b): If Len (a[ 0])!=len (b[0]) and Len (a)!=len (B): Returnresult = [[0]*len (A[0]) for I in Range (Len (a))] for I in range (Len (a)): for J in Ra Nge (Len (a[0)): result[i][j] = A[i][j]+b[i][j]return result def pageRank (A): E = []for i in Range (N): e.append (1) norm = 100N ew_p = []for i in Range (N): New_p.append ([Random.random ()]) R = [[(1-d] *i*1/n] for i in e]while norm > delt:p = New_pne w_p = Matrix_add (R,matrix_multin (D,matrix_multi (a,p))) #P = (1-d) *e/n+d*m ' P pagerank algorithm core norm = 0# Solution Matrix First-order norm for I in range ( N): Norm + = ABS (new_p[i][0]-p[I][0]) print new_p# transfer probability matrix based on adjacency matrix and turn to Def Tran_and_convert (a): result = [[0]*len (A[0]) for I in Range (Len (a))]result_ convert = [[0]*len (A[0]) for I in Range (Len (a))]for I in range (Len (a)): for J in Range (Len (a[0]): result[i][j] = a[i][j]*1. 0/sum (A[i]) for I in range (len (result)): for J in Range (Len (result[0)): Result_convert[i][j]=result[j][i]return result_ Convertdef Main (): A = [[0,1,1,0,0,1,0,0],[0,0,0,1,1,0,0,0],[0,0,0,1,0,1,0,0],[0,0,0,0,0,1,0,0],[1,0,0,1,0,0,1,1], [0,0,0,1,0,0,0,0],[0,0,1,0,0,0,0,0],[0,0,0,1,0,0,1,0]]m = Tran_and_convert (A) PageRank (M) if __name__ = = ' __main__ ': Main ()


PageRank algorithm (Python implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.