Skip to content

Instantly share code, notes, and snippets.

View maxlikely's full-sized avatar

Max Likely maxlikely

View GitHub Profile
@maxlikely
maxlikely / mean_avg_prec.py
Created October 23, 2012 23:38
Mean average precision
def node_mean_avg_prec(G, node, predictions):
'''
Calculates mean average precision for a user.
'''
gold = set(G[node])
indicator = [1 if p in gold else 0 for p in predictions]
prec = np.cumsum(indicator)
prec = [indicator[i] * p / float(i+1) for i, p in enumerate(prec)]
@maxlikely
maxlikely / is_permutation.py
Created October 30, 2012 19:13
Determines if two strings are permutations
def is_permutation(first_string, second_string):
'''
checks if two strings are permutations of eachother
'''
from collections import Counter
return Counter(first_string) == Counter(second_string)
@maxlikely
maxlikely / dict_eqaulity.py
Created October 30, 2012 19:25
Compare to dictionaries for equality
def dicts_equal(first_dict, second_dict):
'''
check if two dicts are equal
'''
# same length, same keys, same values
return len(first) == len(second) and \
all([k in first for k in second]) and \
all([v == second[k] for k,v in first.items()])