Skip to content

Instantly share code, notes, and snippets.

@brennonbrimhall
Created December 27, 2018 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brennonbrimhall/6ef7eaf42e0d34dd355f2ca71388e93d to your computer and use it in GitHub Desktop.
Save brennonbrimhall/6ef7eaf42e0d34dd355f2ca71388e93d to your computer and use it in GitHub Desktop.
Predicting Robot Awards
from sklearn.linear_model import LogisticRegression
train = [x for x in output if x[2] <= 2018]
test = [x for x in output if x[2] == 2018]
x_train = [x[0] for x in train]
x_test = [x[0] for x in test]
models = []
for i in range(n):
logreg = LogisticRegression(solver = 'liblinear')
y_train = [y[1][i] for y in train]
y_test = [y[1][i] for y in test]
logreg.fit(x_train, y_train)
models.append(logreg)
print((awards_to_predict[i][1], models[i].score(x_test, y_test)))
import json
import numpy as np
"""
List of all awards in the TBA database.
"""
CHAIRMANS = 0
WINNER = 1
FINALIST = 2
WOODIE_FLOWERS = 3
DEANS_LIST = 4
VOLUNTEER = 5
FOUNDERS = 6
BART_KAMEN_MEMORIAL = 7
MAKE_IT_LOUD = 8
ENGINEERING_INSPIRATION = 9
ROOKIE_ALL_STAR = 10
GRACIOUS_PROFESSIONALISM = 11
COOPERTITION = 12
JUDGES = 13
HIGHEST_ROOKIE_SEED = 14
ROOKIE_INSPIRATION = 15
INDUSTRIAL_DEESIGN = 16
QUALITY = 17
SAFETY = 18
SPORTSMANSHIP = 19
CREATIVITY = 20
ENGINEERING_EXCELLENCE = 21
ENTREPRENEURSHIP = 22
EXCELLENCE_IN_DESIGN = 23
EXCELLENCE_IN_DESIGN_CAD = 24
EXCELLENCE_IN_DESIGN_ANIMATION = 25
DRIVING_TOMORROWS_TECHNOLOGY = 26
IMAGERY = 27
MEDIA_AND_TECHNOLOGY = 28
INNOVATION_IN_CONTROL = 29
SPIRIT = 30
WEBSITE = 31
VISUALIZATION = 32
AUTODESK_INVENTOR = 33
FUTURE_INNOVATOR = 34
RECOGNITION_OF_EXTRAORDINARY_SERVICE = 35
OUTSTANDING_CART = 36
WSU_AIM_HIGHER = 37
LEADERSHIP_IN_CONTROL = 38
NUM_1_SEED = 39
INCREDIBLE_PLAY = 40
PEOPLES_CHOICE_ANIMATION = 41
VISUALIZATION_RISING_STAR = 42
BEST_OFFENSIVE_ROUND = 43
BEST_PLAY_OF_THE_DAY = 44
FEATHERWEIGHT_IN_THE_FINALS = 45
MOST_PHOTOGENIC = 46
OUTSTANDING_DEFENSE = 47
POWER_TO_SIMPLIFY = 48
AGAINST_ALL_ODDS = 49
RISING_STAR = 50
CHAIRMANS_HONORABLE_MENTION = 51
CONTENT_COMMUNICATION_HONORABLE_MENTION = 52
TECHNICAL_EXECUTION_HONORABLE_MENTION = 53
REALIZATION = 54
REALIZATION_HONORABLE_MENTION = 55
DESIGN_YOUR_FUTURE = 56
DESIGN_YOUR_FUTURE_HONORABLE_MENTION = 57
SPECIAL_RECOGNITION_CHARACTER_ANIMATION = 58
HIGH_SCORE = 59
TEACHER_PIONEER = 60
BEST_CRAFTSMANSHIP = 61
BEST_DEFENSIVE_MATCH = 62
PLAY_OF_THE_DAY = 63
PROGRAMMING = 64
PROFESSIONALISM = 65
GOLDEN_CORNDOG = 66
MOST_IMPROVED_TEAM = 67
WILDCARD = 68
CHAIRMANS_FINALIST = 69
OTHER = 70
AUTONOMOUS = 71
awards_to_predict = [
(WINNER, "Winner"),
(FINALIST, "Finalist"),
(CHAIRMANS, "Charimans"),
(ENGINEERING_INSPIRATION, "EI"),
(CREATIVITY, "Creativity"),
(ENGINEERING_EXCELLENCE, "Engineering Excellence"),
(ENTREPRENEURSHIP, "Entrepreneurship"),
(DEANS_LIST, "Deans List"),
(GRACIOUS_PROFESSIONALISM, "Gracious Professionalism"),
(IMAGERY, "Imagery"),
(INDUSTRIAL_DEESIGN, "Industrial Design"),
(SAFETY, "Safety"),
(INNOVATION_IN_CONTROL, "Innovation in Control"),
(QUALITY, "Quality"),
(SPIRIT, "Spirit"),
(VOLUNTEER, "Volunteer"),
(WOODIE_FLOWERS, "Woodie Flowers"),
(JUDGES, "Judges Award")
]
awards_to_indicies = {v[0]: i for i, v in enumerate(awards_to_predict)}
indicies_to_awards = {i: v[0] for i, v in enumerate(awards_to_predict)}
k = .8
n = len(awards_to_predict)
output = []
json_file = open('data.json')
with json_file as f:
data = json.load(f)
for team in data:
year = 2010
history_vector = np.zeros(n)
awards_vector = np.zeros(n)
if 'events' in team:
for event in sorted(team['events']):
for award in team['events'][event]:
if award['year'] >= 2010 and award['award_type'] in [x[0] for x in awards_to_predict]:
if award['year'] == year:
awards_vector[awards_to_indicies[award['award_type']]] = 1
elif award['year'] > year:
output.append((history_vector * (1 - k), awards_vector, year, team['team_number']))
history_vector = history_vector + awards_vector * (k ** (award['year'] - year))
year = award['year']
awards_vector = np.zeros(n)
awards_vector[awards_to_indicies[award['award_type']]] += 1
output.append((history_vector * (1 - k), awards_vector, year, team['team_number']))
import requests
import json
with open('API_KEY', 'r') as key_file:
API_KEY = key_file.read()
API_BASE = 'http://www.thebluealliance.com/api/v3/'
team_index = 0
teams = []
more_teams_to_get = True
# get the full list of teams in pages, looping until we start getting none back
while more_teams_to_get:
print("grabbing list of teams from page {}".format(team_index))
response = requests.get(API_BASE + 'teams/' + str(team_index), headers={'X-TBA-Auth-Key':API_KEY})
current_team_set = response.json()
print("found {} teams".format(len(current_team_set)))
teams += current_team_set
team_index += 1
if len(current_team_set) == 0:
more_teams_to_get = False
print("found {} teams in total".format(len(teams)))
# only save keys we care about, prune out everything else
good_team_keys = ['key', 'name', 'nickname', 'team_number']
good_award_keys = ['name', 'award_type', 'year']
# grab all the awards a team has won and sort them by event
print("populating awards for each team")
for i in range(len(teams)):
# prune team keys
team = {key: teams[i][key] for key in good_team_keys}
team['events'] = {}
response = requests.get(API_BASE + 'team/' + team['key'] + '/awards', headers={'X-TBA-Auth-Key':API_KEY})
awards = response.json()
for award in awards:
# prune award keys
new_award = {key: award[key] for key in good_award_keys}
# group awards by event and add to the team object
team['events'].setdefault(award['event_key'],[]).append(new_award)
teams[i] = team
# dump everything in a JSON file
print("writing data to file")
with open('data.json', 'w') as output_file:
json.dump(teams, output_file)
print("done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment