networkx pagerank
本來覺得是不想寫這篇博客的,因?yàn)榫W(wǎng)上關(guān)于pagerank的介紹很多很多了,而且入門pagerank本來也不難,不過在networkx中實(shí)現(xiàn)的pagerank和網(wǎng)上大多數(shù)資料介紹的pagerank是不一樣的,這一點(diǎn)網(wǎng)上的說明卻比較少,因此本博客著重于講一下這一點(diǎn)。
1.pagerank介紹
首先給出我看的一些pagerank的連接。
2.networkx實(shí)現(xiàn)的pagerank的說明
首先,還是要貼一下源碼中的對各輸入?yún)?shù)的說明。看這個就很容易理解各個參數(shù)的意思了。基本上參數(shù)都可以很容易理解了,之前給的鏈接中的博客都有說明了,唯一可能有些費(fèi)解的只有dangling這個參數(shù),這個參數(shù)是為dangling node準(zhǔn)備的,什么是dangling node,也就是出度為0的結(jié)點(diǎn),在上述的博文中叫做終止點(diǎn),因?yàn)閐angling node會導(dǎo)致迭代過程中dangling node的pr值越來越大,所以需要對迭代公式做改進(jìn)的,在這里我們可以輸入一個字典,指定dangling node的權(quán)重,如果不指定的話,就是默認(rèn)dangling node和其他結(jié)點(diǎn)的權(quán)重一樣,也就是權(quán)重全部平均。其他應(yīng)該就沒問題了。
def pagerank(G, alpha=0.85, personalization=None,max_iter=100, tol=1.0e-6, nstart=None, weight='weight',dangling=None):"""Return the PageRank of the nodes in the graph.PageRank computes a ranking of the nodes in the graph G based onthe structure of the incoming links. It was originally designed asan algorithm to rank web pages.Parameters----------G : graphA NetworkX graph. Undirected graphs will be converted to a directedgraph with two directed edges for each undirected edge.alpha : float, optionalDamping parameter for PageRank, default=0.85.personalization: dict, optionalThe "personalization vector" consisting of a dictionary with akey for every graph node and nonzero personalization value for each node.By default, a uniform distribution is used.max_iter : integer, optionalMaximum number of iterations in power method eigenvalue solver.tol : float, optionalError tolerance used to check convergence in power method solver.nstart : dictionary, optionalStarting value of PageRank iteration for each node.weight : key, optionalEdge data key to use as weight. If None weights are set to 1.dangling: dict, optionalThe outedges to be assigned to any "dangling" nodes, i.e., nodes withoutany outedges. The dict key is the node the outedge points to and the dictvalue is the weight of that outedge. By default, dangling nodes are givenoutedges according to the personalization vector (uniform if notspecified). This must be selected to result in an irreducible transitionmatrix (see notes under google_matrix). It may be common to have thedangling dict to be the same as the personalization dict.Returns-------pagerank : dictionaryDictionary of nodes with PageRank as valueExamples-------->>> G = nx.DiGraph(nx.path_graph(4))>>> pr = nx.pagerank(G, alpha=0.9)Notes-----The eigenvector calculation is done by the power iteration methodand has no guarantee of convergence. The iteration will stopafter max_iter iterations or an error tolerance ofnumber_of_nodes(G)*tol has been reached.The PageRank algorithm was designed for directed graphs but thisalgorithm does not check if the input graph is directed and willexecute on undirected graphs by converting each edge in thedirected graph to two edges.See Also--------pagerank_numpy, pagerank_scipy, google_matrixReferences----------.. [1] A. Langville and C. Meyer,"A survey of eigenvector methods of web information retrieval."http://citeseer.ist.psu.edu/713792.html.. [2] Page, Lawrence; Brin, Sergey; Motwani, Rajeev and Winograd, Terry,The PageRank citation ranking: Bringing order to the Web. 1999http://dbpubs.stanford.edu:8090/pub/showDoc.Fulltext?lang=en&doc=1999-66&format=pdf"""接下來摘抄源碼解析博客中的對源碼中迭代公式的說明:
PR=alpha * (A * PR+dangling分配)+(1-alpha) * 平均分配
也就是三部分,A*PR其實(shí)是我們用圖矩陣分配的,dangling分配則是對dangling node的PR值進(jìn)行分配,(1-alpha)分配則是天下為公大家一人一份分配的
dangling node 也就是懸空結(jié)點(diǎn),它的出度為0,也就是無法從它到任何其他結(jié)點(diǎn),解決辦法是增加一定的隨機(jī)性,dangling分配其實(shí)就是加上一個隨機(jī)向量,也就是無法從這個結(jié)點(diǎn)去往任何其他結(jié)點(diǎn),但是可能會隨機(jī)重新去一個結(jié)點(diǎn),也可以這么理解,到了一個網(wǎng)站,這個網(wǎng)站不連接到任何網(wǎng)站,但是瀏覽者可能重新隨機(jī)打開一個頁面。
其實(shí)通俗的來說,我們可以將PageRank看成搶奪大賽,有三種搶奪機(jī)制。
1,A*PR這種是自由分配,大家都愿意參與競爭交流的分配
2,dangling是強(qiáng)制分配,有點(diǎn)類似打倒土豪分田地的感覺,你不參與自由市場,那好,我們就特地幫你強(qiáng)制分。
3,平均分配,其實(shí)就是有個機(jī)會大家實(shí)現(xiàn)共產(chǎn)主義了,不讓spider trap這種產(chǎn)生rank sink的節(jié)點(diǎn)撈太多油水,其實(shí)客觀上也是在幫dangling分配。
從圖和矩陣的角度來說,可以這樣理解,我們這個矩陣可以看出是個有向圖
矩陣要收斂–>矩陣有唯一解–>n階方陣對應(yīng)有向圖是強(qiáng)連通的–>兩個節(jié)點(diǎn)相互可達(dá),1能到2,2能到1
如果是個強(qiáng)連通圖,就是我們上面說的第1種情況,自由競爭,那么我們可以確定是收斂的
不然就會有spider trap造成rank sink問題
我們可以發(fā)現(xiàn)上述的迭代公式和之前所有的迭代公式都是不一樣的。
networkx中的迭代公式里多了一個dangling分配,其他都是一模一樣的。這個一開始我有一些難以理解,因?yàn)楦杏Xdangling分配似乎并不是必要的,如果為了解決dangling node的問題,最后加一個隨機(jī)分配就可以了,隨機(jī)分配就意味著一定幾率會隨機(jī)跳轉(zhuǎn)到一個全新的網(wǎng)頁。接下來,我們來看一下源碼中的這一部分的關(guān)鍵代碼,來更進(jìn)一步看看pr值究竟是咋樣更新迭代的。
從上面的源碼可以看到首先計(jì)算了一下danglesum,也就是把dangling node的pr值全部加起來求和,x[nbr] += alpha * xlast[n] * W[n][nbr][weight]這個也就是pr值的循環(huán)迭代了,x[n] += danglesum * dangling_weights[n] + (1.0 - alpha) * p[n]這個包含兩部分,(1.0 - alpha) * p[n]很容易理解肯定就是平均分配了,前面這個就是dangling分配了,也就是用danglesum乘上dangling node的權(quán)重,如果沒有給定的話,就是所有的node權(quán)重都一樣,即1/N。
3.驗(yàn)證說法的正確性及2個例子
這一部分就來驗(yàn)證下說法的正確性以及networkx這樣實(shí)現(xiàn)的好處,之后我會給出一個例子。
讓我們假設(shè)這樣一個圖結(jié)構(gòu):
一共就兩個node,A指向B,也就是說B是一個dangling node,在這個情況下,我們使用networkx中的pagerank進(jìn)行計(jì)算得到的pr值就如上圖所示,如何驗(yàn)證這兩個pr值計(jì)算的迭代公式確實(shí)是PR=alpha * (A * PR+dangling分配)+(1-alpha) * 平均分配呢?只需要將計(jì)算得到的pr值代入這個公式在進(jìn)行一次迭代,如果再一次迭代后得到的也是這個值的話說明確實(shí)是使用這個迭代公式的。
PR(A)=alpha?(A?PR+dangling分配)+(1?alpha)?平均分配=0.85?0.5?0.649123+0.15/2=0.350877PR(A)=alpha * (A * PR+dangling分配)+(1-alpha) * 平均分配=0.85 * 0.5 * 0.649123 + 0.15/2 = 0.350877PR(A)=alpha?(A?PR+dangling分配)+(1?alpha)?平均分配=0.85?0.5?0.649123+0.15/2=0.350877
PR(B)=alpha?(A?PR+dangling分配)+(1?alpha)?平均分配=0.350877+0.85?0.35087=0.649123PR(B)=alpha * (A * PR+dangling分配)+(1-alpha) * 平均分配=0.350877+0.85 * 0.35087=0.649123PR(B)=alpha?(A?PR+dangling分配)+(1?alpha)?平均分配=0.350877+0.85?0.35087=0.649123
確實(shí)和上述得到的一致,得證。
使用鏈接2中的博文中的迭代公式進(jìn)行計(jì)算
我們可以嘗試使用以前的計(jì)算方法去進(jìn)行計(jì)算,即使用如下的迭代公式:
初始時赴初值pr均是0.5,最終就會是下面這樣,我們可以發(fā)現(xiàn)是不歸一化的。
PR(A)=0.15/2=0.075PR(A)= 0.15/2 = 0.075PR(A)=0.15/2=0.075
PR(B)=0.15/2+0.85?0.075=0.13875PR(B)= 0.15/2+0.85*0.075 = 0.13875PR(B)=0.15/2+0.85?0.075=0.13875
進(jìn)行歸一化后,令人驚奇地發(fā)現(xiàn)結(jié)果和networkx輸出的結(jié)果是一致的。
那么這個是不是巧合呢?于是我又使用三個結(jié)點(diǎn)進(jìn)行實(shí)驗(yàn),如下圖所示:
并且也使用和上述一樣的方法進(jìn)行了實(shí)驗(yàn),發(fā)現(xiàn)使用源碼解析中的公式進(jìn)行計(jì)算就是得到上述的PR值,然后又使用鏈接2中的博文中的公式進(jìn)行計(jì)算,得到值后在進(jìn)行歸一化,依然得到了一樣的結(jié)論,也就是計(jì)算結(jié)果和networkx輸出的結(jié)果一致。
4.猜測與結(jié)論
于是總結(jié)上述的兩個實(shí)驗(yàn),我猜測源碼解析中的PR迭代公式和下圖中PR計(jì)算迭代公式效果是一樣的。
兩者唯一的區(qū)別是源碼解析中的公式最后迭代得到的直接就是歸一化后的PR值,而使用上面圖片中的公式迭代計(jì)算得到的是沒有歸一化的數(shù)值,但是結(jié)果是一模一樣的。
可惜的是我無法從理論推導(dǎo)中給出證明,要是有大佬可以指點(diǎn)一下,將不勝感激,即下面兩個公式為什么效果是一樣的
補(bǔ)充:上述例子的代碼的獲取連接
總結(jié)
以上是生活随笔為你收集整理的networkx pagerank的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【渝粤教育】电大中专新媒体营销实务_1作
- 下一篇: UART是什么