Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View BlueSCar's full-sized avatar

Bill Radjewski BlueSCar

View GitHub Profile
class PermutationImportance():
def __init__(self, learn:Learner, df=None, bs=None):
"Initialize with a test dataframe, a learner, and a metric"
self.learn = learn
self.df = df
bs = bs if bs is not None else learn.dls.bs
if self.df is not None:
self.dl = learn.dls.test_dl(self.df, bs=bs)
else:
self.dl = learn.dls[1]
@BlueSCar
BlueSCar / logos_with_matplotlib.py
Created January 23, 2021 20:21
Plotting team logos in Python with matplotlib
import cfbd
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import pandas as pd
# Grab some stats from the CFBD API
stats = cfbd.StatsApi().get_advanced_team_season_stats(year=2020)
df = pd.DataFrame.from_records([dict(team=s.team, o_line_yards=s.offense.line_yards, d_line_yards=s.defense.line_yards) for s in stats])
# Need to grab team ids
import cfbd
import pandas as pd
games = cfbd.GamesApi().get_team_game_stats(year=2020, week=1)
stats = []
for g in games:
for t in g.teams:
for s in t.stats:
stats.append(dict(game_id=g.id, team=t.school, conference=t.conference, category=s.category, stat=s.stat))
import numpy as np
import pandas as pd
import requests
pd.set_option('display.float_format', lambda x: '%.8f' % x)
season = 2
day = 3
response = requests.get('https://collegefootballrisk.com/api/territories', params={'season': season, 'day': day})
@BlueSCar
BlueSCar / game_players.py
Last active January 8, 2020 00:17
Python - Normalizing /game/players endpoint
import pandas as pd
import requests
response = requests.get(
"https://api.collegefootballdata.com/games/players",
params={"year": 2019, "week": "1"}
)
records = pd.io.json.json_normalize(
response.json(),
@BlueSCar
BlueSCar / playTypes.json
Created August 20, 2019 15:42
Regexes for parsing player names out of play types
[{
typeId: 3,
regex: /(?:Fumble by [^,]+, )?(?:([A-Z][^,\s]+(?: [A-Z][^,\s]+)+)|(?:N\/A))(?:(?: pass incomplete(?: to )*)([A-Z][^,\s]+(?: [A-Z'][^,\s]*)+)*)*(?:(?:, broken up by )([A-Z][^,\s]+(?: [A-Z][^,\s]+)+))*/g,
types: [{
id: 1,
isOffense: true,
yardageStat: false
}, {
id: 2,
isOffense: true,