Last active
August 29, 2015 14:09
-
-
Save wy36101299/9c0b89254f92cbffac39 to your computer and use it in GitHub Desktop.
pagerank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
with open('/Users/wy/Desktop/hw2dataset/graph_1.txt','r') as f: | |
tl=[] | |
f = f.readlines() | |
for s in f: | |
s=s.rstrip() | |
s=s.split(',') | |
tl.append(s[0]) | |
tl.append(s[1]) | |
setsum = len(set(tl)) | |
class ini(object): | |
def __init__(self,n): | |
self.d ={} | |
self.d['pr']=float(1)/float(n) | |
self.d['in']=[] | |
self.d['out']=[] | |
dd={} | |
for a in tl: | |
t =ini(setsum) | |
dd[a]= t | |
for s in f: | |
s=s.rstrip() | |
s=s.split(',') | |
dd[s[0]].d['out'].append(s[1]) | |
dd[s[1]].d['in'].append(s[0]) | |
def pr(setsum): | |
x=[] | |
for p,v in dd.items(): | |
pre = copy.deepcopy(v.d['in']) | |
score=0 | |
if len(pre)!=0: | |
for r in range( len(pre) ): | |
vv = dd[pre.pop()] | |
score += float(vv.d['pr'])/float(len(vv.d['out'])) | |
nowpr=(1-0.85)/float(setsum)+0.85*score | |
v.d['pr']=nowpr | |
x.append(nowpr) | |
return(x) | |
ri=[] | |
ro = pr(setsum) | |
while ri != ro: | |
ri=ro | |
ro = pr(setsum) | |
y=[] | |
for p,v in dd.items(): | |
print(p) | |
y.append(v.d['pr']) | |
print(v.d['pr']) | |
print(sum(y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment