def team_similarity(roster1, roster2, players, freqfield='ownership'): '''Compare the rosters of two teams using statistics provided in the list `players`. Comparison is cosine similarity of TF-IDF vectors created from these rosters. Return value is in [0, 1]. A value of 1 means that the teams are identical. A value of 0 means that teams share no players in common.''' v1 = np.zeros(len(players)) v2 = np.zeros(len(players)) for i, player in enumerate(players): # Calculate inverse frequency of player selection for all players # selected in both teams. Same idea as TF-IDF in document similarity. in_team_one = optr.player_in_roster(player, roster1) is not None in_team_two = optr.player_in_roster(player, roster2) is not None if not in_team_one and not in_team_two: continue freq = float(getattr(player, freqfield)) ipf = np.log(1. / freq) if in_team_one: v1[i] = ipf if in_team_two: v2[i] = ipf # TODO - verify that there are 15 elements in both teams? # Calculate cos(θ) between vectors cos = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) return cos
def team_similarity(roster1, roster2, players, freqfield='ownership'): '''Compare the rosters of two teams using statistics provided in the list `players`. Comparison is cosine similarity of TF-IDF vectors created from these rosters. Return value is in [0, 1]. A value of 1 means that the teams are identical. A value of 0 means that teams share no players in common.''' v1 = np.zeros(len(players)) v2 = np.zeros(len(players)) for i, player in enumerate(players): # Calculate inverse frequency of player selection for all players # selected in both teams. Same idea as TF-IDF in document similarity. in_team_one = optr.player_in_roster(player, roster1) is not None in_team_two = optr.player_in_roster(player, roster2) is not None if not in_team_one and not in_team_two: continue freq = float(getattr(player, freqfield)) ipf = np.log(1./freq) if in_team_one: v1[i] = ipf if in_team_two: v2[i] = ipf # TODO - verify that there are 15 elements in both teams? # Calculate cos(θ) between vectors cos = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) return cos
def score_team(roster, players, field="total_points"): '''Calculate fantasy score of team''' score = 0. for i, player in enumerate(players): in_team = optr.player_in_roster(player, roster) is not None if in_team: score += getattr(player, field) return score