def __init__(self, player_id, rating=None): self._player_id = player_id self._fetch_and_init() if rating is None: self._rating = Player() else: self._rating = Player(rating[0], rating[1])
def readElo(playerInfoInput, playerDict): fileToOpen = open("player.txt", "r") file = fileToOpen.read() file = file.splitlines() try: file.pop(0) except: pass #list containing all info playerInfo = list() for line in file: for person in line.split(): playerInfo.append(person) #create player objects #x is name, x+1 is rating, x+2 is rd, x+3 is vol for x in range(0, len(playerInfo), 4): playerDict[playerInfo[x]] = Player(playerInfo[x]) playerDict[playerInfo[x]].rating = float(playerInfo[x+1]) playerDict[playerInfo[x]].rd = float(playerInfo[x+2]) playerDict[playerInfo[x]].vol = float(playerInfo[x+3]) return playerDict
def getGlickoDataMatrix(years): glicko_df_reg = pd.read_csv('./data/RegularSeasonCompactResults.csv') glicko_df_tourny = pd.read_csv('./data/NCAATourneyCompactResults.csv') glicko_df = pd.concat([glicko_df_reg,glicko_df_tourny]) glicko_df.sort_values(by=['Season','DayNum']) glicko_years = list(range(1985, min(years))) glicko_df_sp = glicko_df.loc[glicko_df['Season'].isin(glicko_years)] team_ids = set(glicko_df.WTeamID).union(set(glicko_df.LTeamID)) glicko = dict(zip(list(team_ids), [Player() for _ in range(len(team_ids))])) preds = [] glicko_rounds(glicko, preds, glicko_df_sp) df_reg_season = pd.read_csv('data/RegularSeasonDetailedResults.csv') df_reg_season['tourny'] = 0 df_ncaa_tourny = pd.read_csv('data/NCAATourneyDetailedResults.csv') df_ncaa_tourny['tourny'] = 1 df = pd.concat([df_reg_season,df_ncaa_tourny]) df.sort_values(by=['Season','DayNum']) df_sp = df.loc[df['Season'].isin(years)] team_results = {} team_stats = {} for id in team_ids: team_results[id] = [] team_stats[id] = {'games': 0.0, 'points': 0.0, 'fgm': 0.0, 'fga': 0.0, 'fgm3': 0.0, 'fga3': 0.0, 'ftm': 0.0, 'fta': 0.0, 'or': 0.0, 'dr': 0.0, 'to': 0.0, 'ast': 0.0, 'stl': 0.0, 'blk': 0.0, 'pf': 0.0,'opp_points': 0.0, 'opp_fgm': 0.0, 'opp_fga': 0.0, 'opp_fgm3': 0.0, 'opp_fga3': 0.0, 'opp_ftm': 0.0, 'opp_fta': 0.0, 'opp_or': 0.0, 'opp_dr': 0.0, 'opp_to': 0.0, 'opp_ast': 0.0, 'opp_stl': 0.0, 'opp_blk': 0.0, 'opp_pf': 0.0, 'possesions': 0} data_matrix = pd.DataFrame() for row in df_sp.itertuples(): data_matrix = data_matrix.append(make_row(team_stats, team_results, glicko, min(row.WTeamID, row.LTeamID), max(row.WTeamID, row.LTeamID), row.Season, row.tourny, 1 if row.WTeamID > row.LTeamID else 0)) team_stats, team_results, glicko = update_stats(team_stats, team_results, glicko, row) return data_matrix, team_stats, team_results, glicko
def updatePlayerList(inputPlayerDict, inputDictionary): #read matches.txt file and updates dictionary for key, value in inputDictionary.items(): if value not in inputPlayerDict: userChoice = input("New player: " + value + ". Add as new player or existing?\nType in add to add as new player, edit to add to existing tag, or type exit to exit.\n") if userChoice.lower() == "add": inputPlayerDict[value] = Player(value) #if not userChoice.lower() == "edit": #break if userChoice.lower() == "edit": existingTag = input("Enter the correct tag: ") if existingTag in inputPlayerDict: inputPlayerDict[value] = inputPlayerDict[existingTag] if existingTag not in inputPlayerDict: newTag = input("Tag not found. Enter the new tag: ") inputPlayerDict[newTag] = Player(newTag) if userChoice.lower() == "exit": break return inputPlayerDict
class MafiaPlayer(object): """ >>> selensky = MafiaPlayer(777) >>> print selensky.name Роман SeLensky Селенский >>> selensky.player_id 777 >>> print selensky.toJson() {"id": 777, "name": "Роман SeLensky Селенский", "rating": [1200.0, 350.0]} """ def __init__(self, player_id, rating=None): self._player_id = player_id self._fetch_and_init() if rating is None: self._rating = Player() else: self._rating = Player(rating[0], rating[1]) def update_player(self, *args): self._rating.update_player(*args) def getRating(self): return self._rating.getRating() def getRd(self): return self._rating.getRd() def _fetch_and_init(self): response = requests.get(MAFSHOW_PLAYER_PAGE.format(self._player_id)) pq = pyquery.PyQuery(response.content) self._name = pq('.person > h1')[0].text_content().strip().encode('utf-8') @property def rating(self): return (self._rating.getRating(), self._rating.getRd()) @property def name(self): return self._name @property def player_id(self): return self._player_id def toJson(self): return '{{"id": {id}, "name": "{name}", "rating": {rating}}}'.format( id=self.player_id, name=self.name, rating=list(self.rating))
team_stats[row.Losing]['num_rushes_against'] += int(row.Winning_Rushes) team_stats[row.Losing]['rush_yards_against'] += int(row.Winning_Rush_Yards) team_stats[row.Losing]['num_plays_against'] += int(row.Winning_Plays) team_stats[row.Losing]['total_yards_against'] += int(row.Winning_Total_Yards) team_stats[row.Losing]['num_turnovers_against'] += int(row.Winning_TO) team_stats[row.Losing]['penalty_yards_against'] += int(row.Winning_Pen_Yards) team_stats[row.Losing]['time_of_possession_against'] += int(row.Winning_TOP) return team_stats, glicko df = pd.read_csv('data.csv') df = df[df['Year'] == 2017] team_ids = set(df['Winning']).union(df['Losing']) #print(team_ids) team_results = {} team_stats = {} glicko = dict(zip(list(team_ids), [Player() for _ in range(len(team_ids))])) #print(team_ids) for id in team_ids: team_results[id] = [] team_stats[id] = {'games':0,'points':0,'num_passes':0,'pass_yards':0,'num_rushes':0,'rush_yards':0,'num_plays':0,'total_yards':0,'num_turnovers':0,'penalty_yards':0,'time_of_possession':0,'points_against':0,'num_passes_against':0,'pass_yards_against':0,'num_rushes_against':0,'rush_yards_against':0,'num_plays_against':0,'total_yards_against':0,'num_turnovers_against':0,'penalty_yards_against':0,'time_of_possession_against':0} data_matrix = pd.DataFrame() results_matrix = pd.DataFrame() for row in df.itertuples(): id0 = row.Winning if row.Winning < row.Losing else row.Losing id1 = row.Winning if row.Winning > row.Losing else row.Losing data_matrix = data_matrix.append(make_row(team_stats,id0,id1,glicko,team_results)) results_matrix = results_matrix.append(pd.DataFrame({'id0':id0,'id1':id1,'points_0':(row.Winning_Points if row.Winning < row.Losing else row.Losing_Points), 'points_1':(row.Winning_Points if row.Winning > row.Losing else row.Losing_Points)}, index=[0])) team_stats, glicko = update_stats(team_stats, row, glicko) #print(update_stats(team_stats, row), 0 if row.Winning < row.Losing else 1) #print(team_stats['Michigan State']) #print(data_matrix.iloc[715])
def get_data_matrix(year): df = pd.read_csv('data.csv') df = df[df['Year'].isin([year - 1, year])] team_ids = set(df['Winning']).union(df['Losing']) team_results = {} team_stats = {} glicko = dict(zip(list(team_ids), [Player() for _ in range(len(team_ids))])) for id in team_ids: team_results[id] = [] team_stats[id] = { 'games': 0, 'points': 0, 'num_passes': 0, 'pass_yards': 0, 'num_rushes': 0, 'rush_yards': 0, 'num_plays': 0, 'total_yards': 0, 'num_turnovers': 0, 'penalty_yards': 0, 'time_of_possession': 0, 'points_against': 0, 'num_passes_against': 0, 'pass_yards_against': 0, 'num_rushes_against': 0, 'rush_yards_against': 0, 'num_plays_against': 0, 'total_yards_against': 0, 'num_turnovers_against': 0, 'penalty_yards_against': 0, 'time_of_possession_against': 0 } data_matrix = pd.DataFrame() results_matrix = pd.DataFrame() vegas_predictions = pd.DataFrame() for row in df.itertuples(): id0 = row.Winning if row.Winning < row.Losing else row.Losing id1 = row.Winning if row.Winning > row.Losing else row.Losing if row.Year == year: data_matrix = data_matrix.append( make_row(team_stats, id0, id1, glicko, team_results)) results_matrix = results_matrix.append( pd.DataFrame( { 'id0': id0, 'id1': id1, 'points_0': (row.Winning_Points if row.Winning < row.Losing else row.Losing_Points), 'points_1': (row.Winning_Points if row.Winning > row.Losing else row.Losing_Points) }, index=[0])) vegas_predictions = vegas_predictions.append( pd.DataFrame( { 'id0': id0, "id1": id1, 'points_0': int((row.Winning_Points if row.Winning < row.Losing else row.Losing_Points)), 'points_1': int((row.Winning_Points if row.Winning > row.Losing else row.Losing_Points)), "spread": (1 if row.WinningFU == "F" else -1) * float(row.Spread), "over_under": (row.Over_Under if isinstance( row.Over_Under, str) else "None") }, index=[0])) team_stats, glicko, team_results = update_stats( team_stats, row, glicko, team_results) data_matrix.to_csv(str(year) + "dataMatrix.csv") results_matrix.to_csv(str(year) + "resultsMatrix.csv") vegas_predictions.to_csv(str(year) + "vegasPredictions.csv") print(str(year) + ": Done") return data_matrix, results_matrix, vegas_predictions