Jun-30-2023, 01:55 PM
(This post was last modified: Jun-30-2023, 01:55 PM by deanhystad.)
I don't really understand your code all that well, but does is this close to correct?
from datetime import datetime from dataclasses import dataclass import pandas as pd @dataclass(order=True) class Match: """Better than dictionary or lists for organizing data.""" date: datetime player1: str player2: str score: float @dataclass(order=True) class Player: name: str rating: float = None def load_matches(filename: str) -> tuple[list[Match], dict[str, Player]]: """Return list of match results and player ratings extracted from file.""" # Collect match information in a list. df = pd.read_csv(filename)[["match_date", "player1", "player2", "actual_score"]] df["match_date"] = pd.to_datetime(df["match_date"]) # Convert timestamp to datetime df.sort_values(by=["match_date"]) matches = [Match(*row) for index, row in df.iterrows()] # Extract initial player ratings from file. Use first bp_score to # compute initial rating. df = pd.read_csv(filename)[["pb_date", "pb_player", "pb_score"]] df.dropna(subset=["pb_player"], inplace=True) # Drop matches without bp_player df["pb_date"] = pd.to_datetime(df["pb_date"]) # Convert timestamp to datetime df.sort_values(by=["pb_date"]) players = {} for index, (date, name, score) in df.iterrows(): if score >= 1700000: rating = 10 elif score >= 100000: rating = 5 else: rating = 1 if name not in players: players[name] = Player(name, rating) # Add other players from match data. for match in matches: if match.player1 not in players: players[match.player1] = Player(match.player1, 0) if match.player2 not in players: players[match.player2] = Player(match.player2, 0) return players, matches def update_rating(match: Match, players: dict[str, Player]) -> None: if match.score < 0.5: players[match.player1].rating -= 1 players[match.player2].rating += 1 elif match.score > 0.5: players[match.player1].rating += 1 players[match.player2].rating -= 1 # This does the first script. Somewhat. players, matches = load_matches("matches_full.csv") # This does the second script. Well, it starts with the initial rating and # adjusts based on match results. for match in matches: update_rating(match, players) for player in sorted(players.values(), key=lambda x: x.rating, reverse=True): print(player)I left out the Glicko stuff. I don't understand it. I would make an error if I tried to implement it, and the important thing to demonstrate is how to get the initial rating from your first script and use it in your second script.