Implemented Pagerank
This commit is contained in:
parent
96258ae19b
commit
33ba27e2d0
17
caliGraph.py
17
caliGraph.py
@ -407,6 +407,8 @@ def scoreUnread(G, globMu, globStd):
|
|||||||
if len(feedbacks):
|
if len(feedbacks):
|
||||||
node['mean'], node['std'] = norm.fit(feedbacks)
|
node['mean'], node['std'] = norm.fit(feedbacks)
|
||||||
node['se'] = globStd / math.sqrt(len(feedbacks))
|
node['se'] = globStd / math.sqrt(len(feedbacks))
|
||||||
|
feedbacks.append(node['pagerank_score'])
|
||||||
|
ws.append(['pagerank'])
|
||||||
feedbacks.append(node['std'])
|
feedbacks.append(node['std'])
|
||||||
ws.append(['sigma'])
|
ws.append(['sigma'])
|
||||||
feedbacks.append(node['se'])
|
feedbacks.append(node['se'])
|
||||||
@ -477,6 +479,15 @@ def getKeywords(txt,rake):
|
|||||||
return k
|
return k
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
def runPagerank(G):
|
||||||
|
try:
|
||||||
|
scores = nx.pagerank(G=G)
|
||||||
|
except nx.exception.PowerIterationFailedConvergence:
|
||||||
|
print('[!] Could not calculate pagerank-scores: power iteration of the eigenvector calculation did not converge')
|
||||||
|
scores = {}
|
||||||
|
for n in list(G.nodes):
|
||||||
|
G.nodes[n]['pagerank_score'] = scores[n] if n in scores else 0
|
||||||
|
|
||||||
def buildBookGraph(books, darkMode=False, extractKeywords=True, mergeTags=True):
|
def buildBookGraph(books, darkMode=False, extractKeywords=True, mergeTags=True):
|
||||||
G = nx.Graph()
|
G = nx.Graph()
|
||||||
if extractKeywords:
|
if extractKeywords:
|
||||||
@ -622,6 +633,7 @@ def buildFullGraph(darkMode=False):
|
|||||||
graphAddTopLists(G, books, darkMode=darkMode)
|
graphAddTopLists(G, books, darkMode=darkMode)
|
||||||
graphAddSeries(G, books, darkMode=darkMode)
|
graphAddSeries(G, books, darkMode=darkMode)
|
||||||
graphAddTags(G, books, darkMode=darkMode)
|
graphAddTags(G, books, darkMode=darkMode)
|
||||||
|
runPagerank(G)
|
||||||
return G, books
|
return G, books
|
||||||
|
|
||||||
|
|
||||||
@ -898,6 +910,7 @@ def evaluateFitness(books, debugPrint=False):
|
|||||||
graphAddTopLists(G, books)
|
graphAddTopLists(G, books)
|
||||||
graphAddSeries(G, books)
|
graphAddSeries(G, books)
|
||||||
graphAddTags(G, books)
|
graphAddTags(G, books)
|
||||||
|
runPagerank(G)
|
||||||
|
|
||||||
ratedBooks = [n for n in list(G.nodes) if 'rating' in G.nodes[n] and G.nodes[n]['rating'] != None]
|
ratedBooks = [n for n in list(G.nodes) if 'rating' in G.nodes[n] and G.nodes[n]['rating'] != None]
|
||||||
boundsLoss = 0
|
boundsLoss = 0
|
||||||
@ -932,8 +945,8 @@ def evaluateFitness(books, debugPrint=False):
|
|||||||
for g in gradient:
|
for g in gradient:
|
||||||
gradient[g] /= len(errSq)
|
gradient[g] /= len(errSq)
|
||||||
if debugPrint:
|
if debugPrint:
|
||||||
print(sum(errSq)/len(errSq), 0.005*regressionLoss, 0.2*boundsLoss/len(ratedBooks), 1.0*sum(linSepLoss)/len(linSepLoss))
|
print(sum(errSq)/len(errSq), 0.003*regressionLoss, 0.2*boundsLoss/len(ratedBooks), 1.0*sum(linSepLoss)/len(linSepLoss))
|
||||||
fit = sum(errSq)/len(errSq) + 0.005*regressionLoss + 0.2*boundsLoss/len(ratedBooks) - 1.0*sum(linSepLoss)/len(linSepLoss)
|
fit = sum(errSq)/len(errSq) + 0.003*regressionLoss + 0.2*boundsLoss/len(ratedBooks) - 1.0*sum(linSepLoss)/len(linSepLoss)
|
||||||
return fit, gradient
|
return fit, gradient
|
||||||
|
|
||||||
def train(initGamma, full=True):
|
def train(initGamma, full=True):
|
||||||
|
Loading…
Reference in New Issue
Block a user