Skip to content

Instantly share code, notes, and snippets.

@lmeulen
lmeulen / ergast_drivers_most_wins.py
Created August 20, 2022 11:04
ergast_drivers_most_wins
winners = stats.get_winners()
wins = winners[['driverId', 'driver', 'race']].groupby(['driverId', 'driver']). \
count().reset_index()
stats.horizontal_barplot(df=wins, rowcount=10, sort_value='race',
sort_ascending=False, invert_yaxis=True,
xcolumn='driver', ycolumn='race',
title='Drivers with most wins',
xlabel='wins', ylabel='')
@lmeulen
lmeulen / F1Stats_Additions.py
Last active August 20, 2022 11:06
F1Stats_Additions
class F1Stats:
...
def get_winners(self):
winners = self.get_race_results()
return winners[winners.position == 1].copy()
def get_quali_results(self):
qualiresults = \
@lmeulen
lmeulen / plot_ergast_lines.py
Created August 19, 2022 15:42
plot_ergast_lines
def horizontal_driver_lines_plot(df, xcolumn, ycolumn, invert_yaxis=False,
title = '', xlabel='', ylabel='',
ymin=None, ymax=None):
fig, ax = plt.subplots(figsize=(15,10))
drivers = df['code'].unique()
for driver in drivers:
df[df.code == driver].plot(xcolumn, ycolumn, ax=ax, label=driver,
color=stats.get_driver_color(driver))
ax.get_legend().remove()
if invert_yaxis: ax.invert_yaxis()
@lmeulen
lmeulen / ergast_cwdc_standings.py
Created August 19, 2022 15:11
ergast_cwdc_standings
class F1Stats
...
def get_wdc_standing(self, year=None):
standings = \
self.dfs["driver_standings"]. \
merge(self.dfs["races"][['raceId', 'year', 'round', 'name']].rename(columns={'name':'race'}),
on='raceId') .\
merge(self.dfs["drivers"][['driverId', 'name', 'code']])
@lmeulen
lmeulen / ergast_hor_barplot.py
Created August 19, 2022 14:41
ergast_hor_barplot
def horizontal_barplot(df, rowcount, sort_value, sort_ascending,
xcolumn, ycolumn, title='',
xlabel='', ylabel='', invert_yaxis=True):
fig, ax = plt.subplots(figsize=(15,8))
plot_data = df.sort_values(sort_value, ascending=sort_ascending)[:rowcount]
rects = ax.barh(plot_data[xcolumn], plot_data[ycolumn])
for rect in rects:
yloc = rect.get_y() + rect.get_height() / 2 # Center the text vertically in the bar
@lmeulen
lmeulen / ergast_winners.py
Last active August 19, 2022 06:54
ergast_winners
class F1Stats
...
def get_race_results(self):
raceresults = pd.merge(self.dfs['results'][['resultId', 'raceId', 'driverId', 'constructorId',
'grid','position', 'positionText', 'points']],
self.dfs['races'][['raceId', 'year', 'round', 'name', 'date']]. \
rename(columns={'name': 'race'}))
raceresults = pd.merge(raceresults,
@lmeulen
lmeulen / ergast_cleanup.py
Created August 19, 2022 05:44
ergast_cleanup
def cleanup_data(self):
# Races
self.__column_strip_string("races", ["name"])
self.__column_to_datetime("races", "date", "time")
self.__column_to_datetime("races", "fp1_date", "fp1_time", "fp1")
self.__column_to_datetime("races", "fp2_date", "fp2_time", "fp2")
self.__column_to_datetime("races", "fp3_date", "fp3_time", "fp3")
self.__column_to_datetime("races", "quali_date", "quali_time", "quali")
self.__column_to_datetime("races", "sprint_date", "sprint_time", "sprint")
self.__drop_columns("races", ["url"])
@lmeulen
lmeulen / cleanup_methods.py
Last active August 19, 2022 05:37
cleanup_methods
def __column_strip_string(self, dfname, columnnames):
for c in columnnames:
self.dfs[dfname][c] = self.dfs[dfname][c].str.strip()
def __column_to_int(self, dfname, columnnames, default=0):
for c in columnnames:
self.dfs[dfname][c] = self.dfs[dfname][c].replace('',default).astype(int)
def __column_to_float(self, dfname, columnnames, default=0):
for c in columnnames:
@lmeulen
lmeulen / download_ergast_data.py
Created August 18, 2022 19:46
download_ergast_data
import requests
import os
import pickle
import pandas as pd
from zipfile import ZipFile
class F1Stats:
zipfile=None
cachefile=None
@lmeulen
lmeulen / predict_E10_MLP.py
Created August 13, 2022 12:30
predict_E10_MLP
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Split the dataset in train and test data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Scale inputs
sc_X = StandardScaler()
X_trainscaled=sc_X.fit_transform(X_train)