This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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='') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class F1Stats: | |
... | |
def get_winners(self): | |
winners = self.get_race_results() | |
return winners[winners.position == 1].copy() | |
def get_quali_results(self): | |
qualiresults = \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
import pickle | |
import pandas as pd | |
from zipfile import ZipFile | |
class F1Stats: | |
zipfile=None | |
cachefile=None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |