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
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)] |
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
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) |
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
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()]) |
OlderNewer