Skip to content

Instantly share code, notes, and snippets.

@mlopatka
Created October 19, 2016 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlopatka/a9e270187629016f6f1144d75d2dc9aa to your computer and use it in GitHub Desktop.
Save mlopatka/a9e270187629016f6f1144d75d2dc9aa to your computer and use it in GitHub Desktop.
CLLR metric for addons
import math
# math.log(number,base)
def cllr(r_lr, t):
# r = list of recomendeed addons
# t = list of gold standard addons from user
r, lr = zip(*r_lr) # unzip list of pairs
r = set(filter(bool, r)) # sometimes this needs cleaning due to text formatting issues
t = set(filter(bool, t))
r_lr = dict(r_lr)
# lr_vals)
n_ss = len(list(r.intersection(t)))
if n_ss == 0:
return 1.0 # highest possible cost, no correct class attributions
else:
n_ds = len(t) - n_ss
left_part = (1/n_ss) * sum([(1+(1/math.log(x,2))) for x in [r_lr[x] for x in r.intersection(t)]])
right_part = (1/n_ds) * sum([(1+(math.log(x,2))) for x in [r_lr[x] for x in r-t]])
cllr = 0.5 * (left_part + right_part)
if cllr > 1.0: # worse than 1.0 means performance is at chance
return 1.0
else:
return cllr
# test cases:
gold_standard_addons = [u'Session Manager', u'ZenMate Security & Privacy VPN', u'Ghostery', u'Hoxx VPN Proxy', u'DoNotTrackMe', u'Adblock Plus', u'Google Reverse Image Search']
recomended_addons_lr = [(u'Outlook Notifier', 5.2461101828490184), (u'Gmail\u2122 Notifier +', 5.2461101828490184), (u'Black Youtube Theme', 5.2461101828490184), (u'SQLite Manager', 5.2259056187480111), (u'DownThemAll!', 5.2259056187480111), (u'IE View', 5.1791873132821395), (u'Open in IE\u2122 (Internet Explorer)', 5.1791873132821395), (u'Quick Preference Button', 5.1132791146175824), ( u'Ghostery', 5.1132791146175824)]
print cllr(recomended_addons_lr, gold_standard_addons)
print cllr(recomended_addons_lr, gold_standard_addons[1::4])
print cllr([(1, 2.4),(2, 2.21),(3,1.19)], [1,2,3,4,5,6,7,8,9])
print cllr([(1, 1.123)],[1,2])
print cllr([(1, 4.564)],[1,2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment