Skip to content

Port to Python3 and add PageRank #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions analyze.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function

import bs4, itertools, matplotlib, numpy, os, random, re, requests, sys, time
from matplotlib import pyplot

Expand All @@ -19,6 +21,17 @@ def get_n_results_dumb(q):
m = re.search(r'([0-9,]+)', s)
return int(m.groups()[0].replace(',', ''))

def page_rank(m, beta=0.85, niter=20):
N = m.shape[0]
x = numpy.ones(N, dtype=numpy.float32) / N
for i in range(niter):
x_next = m.dot(x) * beta
x_next += (1 - beta) / N # ***
xdiff = numpy.linalg.norm(x - x_next, ord=1)
x = x_next
print(("iter #%d: %f" % (i + 1, xdiff)))
return x

if True:
tag = 'prog_lang'
items = ['java', 'c', 'c++', 'c#', 'python', 'visual basic', 'node', 'perl', 'php', 'ruby', 'go', 'swift', 'dart', 'objective c', 'cobol', 'fortran', 'lua', 'scala', 'lisp', 'haskell', 'rust', 'erlang', 'clojure', 'matlab', 'pascal', 'r', 'elixir', 'kotlin'] #, 'prolog', 'typescript']
Expand Down Expand Up @@ -47,7 +60,7 @@ def get_n_results_dumb(q):

m = numpy.zeros((len(items), len(items)))
random.shuffle(qs)
print 100. * len(set(cache).intersection([q for _, _, q in qs])) / len(qs)
print(100. * len(set(cache).intersection([q for _, _, q in qs])) / len(qs))

for i, j, q in qs:
if q in cache:
Expand Down Expand Up @@ -92,23 +105,28 @@ def plot_mat(m, items, cm, fn, text=False, dir_text=None):
pyplot.savefig(fn, dpi=300)

# Plot lexicographical
ps = sorted(range(len(items)), key=lambda i: items[i])
ps = sorted(list(range(len(items))), key=lambda i: items[i])
plot_mat(m[ps,:][:,ps], sorted(items), pyplot.cm.OrRd, '%s_matrix.png' % tag, text=True)

m += numpy.eye(len(items)) # hack to fix zero entries
for item, pop in zip(items, m.sum(axis=0) + m.sum(axis=1)):
print('%20s %6d' % (item, pop))
print(('%20s %6d' % (item, pop)))
m /= m.sum(axis=0)[numpy.newaxis,:]
u = numpy.ones(len(items))

for i in xrange(100):
for i in range(100):
u = numpy.dot(m, u)
u /= u.sum()

# Create a new matrix where rows/columns are ordered by u
ps = sorted(range(len(items)), key=lambda i: u[i])
ps = sorted(list(range(len(items))), key=lambda i: u[i])
for p in reversed(ps):
print('| %5.2f%% | %20s |' % (u[p]*100, items[p]))
print(('| %5.2f%% | %20s |' % (u[p]*100, items[p])))

m_new = m[ps,:][:,ps]

prs = page_rank(m_new)
for pr, lang in sorted(zip(prs, [items[p] for p in ps]), reverse=True):
print("%12s\t%.3f" % (lang, pr))

plot_mat(m_new, [items[p] for p in ps], pyplot.cm.BuGn, '%s_matrix_eig.png' % tag, dir_text='future popularity')