snap-社交网络分析
Snap簡介
Stanford Network Analysis Platform (SNAP) is a general purpose network analysis and graph mining library. It is written in C++ and easily scales to massive networks with hundreds of millions of nodes, and billions of edges. It efficiently manipulates large graphs, calculates structural properties, generates regular and random graphs, and supports attributes on nodes and edges.
安裝方法
snap官網地址
1.從上面地址下載python版本壓縮包
2.解壓得到本地目錄下 C:\Users\Joker\snap
3.按進入cmd,cd進入該目錄,運行 python setup.py install。
linux系統下也是一樣的方法。
注意:snap只支持python2.7版本。windows系統也需要64位系統。
文檔和數據
snap相關文檔說明
文檔中包括了一些基本的數據類型。還包括了一些比較容易用到的功能的代碼說明:輸出網絡的基本信息,可視化網絡,度分布圖,各種網絡分析的功能通常一句代碼就解決了。
社交網絡數據
snap上收集了一些社交網絡數據,并且免費的分享給大家使用,免去了大家自己從網上爬數據的麻煩。業界良心啊。
連接預測
from __future__ import divisionimport snap import mathG=snap.LoadEdgeList(snap.PUNGraph,'predict_delete.txt',0,1) d_common={} d_jaccard={} d_adamic={}for N1 in G.Nodes():#print node_listfor N2 in G.Nodes():if((int(N1.GetId())<int(N2.GetId()))and not((N1.GetId()==N2.GetId())or (N1.IsNbrNId(N2.GetId())))):node_list_all=set([])value_adamic=0for Id in N1.GetOutEdges():#save the neighborsnode_list_all.add(Id)#sage the same neighbors of n1 and n2node_list_same=set([])for Id in N2.GetOutEdges():#save the all neighbors of n1 and n2if(Id in node_list_all):node_list_same.add(Id)node_list_n=set([])for N_n in G.GetNI(Id).GetOutEdges():#print N_nnode_list_n.add(N_n)if(len(node_list_n)>0):value_adamic=value_adamic+1/(math.log(len(node_list_n)))else:value_adamic=value_adamic+1node_list_all.add(Id)#if (len(node_list_same)>0):#print node_list_all,node_list_same key=str(N1.GetId())+'-'+str(N2.GetId())value_common=len(node_list_same)value_jaccard=len(node_list_same)/len(node_list_all)d_common[key]=int(value_common)d_jaccard[key]=int(value_jaccard)d_adamic[key]=int(value_adamic)s_common=sorted(d_common.items(),key=lambda item:item[1],reverse=True) s_jaccard=sorted(d_jaccard.items(),key=lambda item:item[1],reverse=True) s_adamic=sorted(d_adamic.items(),key=lambda item:item[1],reverse=True)sample=set([]) with open ('predict_d.txt','r') as f:for line in f.readlines():data=line.strip().split()ids=str(data[0])+'-'+str(data[1])sample.add(ids) count_common=0 count_jaccard=0 count_adamic=0 num=500 #common neighbors with open ('predict_common.txt','w') as f:for i in range(0,num):f.write(str(s_common[i][0])+'\n')if(str(s_common[i][0]) in sample):count_common=count_common+1precise=count_common/numf.write('precise='+str(precise)) #jaccard with open ('predict_jaccard.txt','w') as f:for i in range(0,num):f.write(str(s_jaccard[i][0])+'\n')if(str(s_jaccard[i][0]) in sample):count_jaccard=count_jaccard+1precise=count_jaccard/numf.write('precise='+str(precise)) #adamic with open ('predict_adamic.txt','w') as f:for i in range(0,num):f.write(str(s_adamic[i][0])+'\n')if(str(s_adamic[i][0]) in sample):count_adamic=count_adamic+1precise=count_adamic/numf.write('precise='+str(precise))通過三個方法計算每兩對節點的score值。排序,排序top-n的為新出現的連接。
最終比較三個預測方法,adamic方法的準確率最高。
common:兩個節點共同的鄰居的數量
jaccard:兩個節點共同鄰居的鄰居的數量/兩個節點總鄰居的數量
adamic:對兩個節點共同鄰居最加一個權重w,若某個共同鄰居的鄰居數非常高,那這個權重w就可以很小。
總結
以上是生活随笔為你收集整理的snap-社交网络分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux驱动开发】串口
- 下一篇: SnapGene 4.3.6 for w