Skip to content

Instantly share code, notes, and snippets.

@quinnzipse
Last active May 6, 2022 20:18
Show Gist options
  • Save quinnzipse/5e4eb160f13b9d810cd30968536f2cc4 to your computer and use it in GitHub Desktop.
Save quinnzipse/5e4eb160f13b9d810cd30968536f2cc4 to your computer and use it in GitHub Desktop.
MTH308 turning data into a matrix
class Game:
def __init__(self, t1, t2, dog, flags):
self.t1 = t1
self.t2 = t2
self.dog = dog
self.flags = flags
class TeamGame:
def __init__(self, name, is_home, score):
self.name = name
self.is_home = is_home
self.score = score
class Team:
def __init__(self, name):
self.name = name
self.wins = 0
self.losses = 0
self.total = 0
self.diff = 0
self.games_played = {}
def prep_data():
games = list()
with open("more_nhl_data", "r") as file:
for line in file:
flags = line[69:len(line)-1]
if flags.find("Sch") != -1:
break
dog = int(line[9:11])
t1_home = line[11] == '@'
t1_name = line[12:37].strip()
t1_score = int(line[37:39])
t1 = TeamGame(t1_name, t1_home, t1_score)
t2_home = line[40] == '@'
t2_name = line[41:66].strip()
t2_score = int(line[66:68])
t2 = TeamGame(t2_name, t2_home, t2_score)
games.append(Game(t1, t2, dog, flags))
return games
def generate_set_of_teams(games):
teams = { "SENTINAL" }
for game in games:
teams.add(game.t1.name)
teams.add(game.t2.name)
teams.remove("SENTINAL")
return teams
if __name__ == "__main__":
teams = {};
games = prep_data()
for team in generate_set_of_teams(games):
teams[team] = Team(team)
for team1 in teams:
t = teams[team1]
for team2 in teams:
t.games_played[team2] = 0
for game in games:
team1 = teams[game.t1.name]
team2 = teams[game.t2.name]
team1.games_played[team2.name] += 1
team2.games_played[team1.name] += 1
team1.diff += (game.t1.score - game.t2.score)
team2.diff += (game.t2.score - game.t1.score)
team1.total += 1
team2.total += 1
# team 1 always wins...
if game.t1.score > game.t2.score:
team1.wins += 1
team2.losses += 1
elif game.t2.score > game.t1.score:
team2.wins += 1
team1.losses += 1
for team in teams:
t = teams[team]
n = len(teams)
vector_b = [0] * n
vector_p2 = [0] * n
team_name_list = list(teams)
team_obj_list = list(teams.values())
for x in range(0,len(teams)):
key = team_name_list[x]
value = team_obj_list[x]
vector_b[x] = value.diff
vector_p2[x] = 1 + 1/2 * (value.wins - value.losses)
print("vector b => c(", end="")
for x in range(0,len(teams)):
print(vector_b[x], end=",")
print(")")
print("vector p2 => c(", end="")
for x in range(0,len(teams)):
print(vector_p2[x], end=",")
print(")")
n = len(teams)
m = n
matrix = [0] * n
for x in range (n):
matrix[x] = [0] * m
team_name_list = list(teams)
team_obj_list = list(teams.values())
for x in range(0,len(teams)):
key = team_name_list[x]
value = team_obj_list[x]
matrix[x][x] = value.total + 2
r = list(value.games_played.values())
for y in range(0, len(teams)):
matrix[x][y] -= r[y]
print("M + 2I = C => c(", end="")
for x in range(0,len(teams)):
for y in range(0, len(teams)):
print(matrix[x][y], end=",")
print(")")
print(len(teams))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment