<jupyter_start><jupyter_text>"NFL Score Prediction"> "Predict how many points each team will score in a game"- toc: false- branch: master- badges: true- comments: true- categories: [fastpages, jupyter]- image: images/some_folder/your_image.png- hide: false- search_exclude: true- metadata_key1: metadata_value1- metadata_key2: metadata_value2 This is a work in progress, something fun to do for a sport I'm interested in. Go Browns!<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
df = pd.read_csv("C:/NFL Prediction/Data Raw/season18.csv",index_col=0)
df.head()<jupyter_output><empty_output><jupyter_text>Work out who won each game and create a record for each game and team combination<jupyter_code>df['homewin'] = np.where(df['homescore']> df['awayscore'],1,0)
df['awaywin'] = np.where(df['homescore']< df['awayscore'],1,0)
df.head()
home = df.drop(['awaywin'],axis=1)
home = home.rename(columns={'home':'team','homescore':'PF','awayscore':'PA','homewin':'win','away':'opponent'})
home.head()
home['hometeam'] = 1 
home.tail()
away = df.drop(['homewin'],axis=1)
away = away[['GameID','date','away','home','awayscore','homescore','awaywin']]
away = away.rename(columns={'away':'team','awayscore':'PF','homescore':'PA','awaywin':'win','home':'opponent'})
away['hometeam'] = 0
away.head()
scores = pd.concat([home,away])
scores.shape
scores = scores.sort_values(['team','date'])
scores = scores.reset_index(drop=True)
scores.head()<jupyter_output><empty_output><jupyter_text>Create moving average score variables<jupyter_code>ma1 = scores.groupby('team')['PF'].shift().rolling(1).mean().reset_index()
ma2 = scores.groupby('team')['PF'].shift().rolling(2).mean().reset_index()
ma3 = scores.groupby('team')['PF'].shift().rolling(3).mean().reset_index()
scores['score1MA'] = ma1['PF']
scores['score2MA'] = ma2['PF']
scores['score3MA'] = ma3['PF']
scores
scores_2 = pd.merge(scores,scores,how='left', left_on=['GameID','opponent'], right_on = ['GameID','team'], suffixes= ("","_opponent"))
scores_2.head()
scores_2 = scores_2.drop(['date_opponent','team_opponent','opponent_opponent','PF_opponent','PA_opponent','win_opponent','hometeam_opponent'],axis=1)<jupyter_output><empty_output><jupyter_text>Create win record coming into the game<jupyter_code>scores_2['cum_wins'] = scores.groupby('team')['win'].transform(lambda x: x.cumsum().shift())
scores_2['cum_PF'] = scores.groupby('team')['PF'].transform(lambda x: x.cumsum().shift())
scores_2['cum_PA'] = scores.groupby('team')['PA'].transform(lambda x: x.cumsum().shift())
scores_2['cum_games'] = scores.groupby('team').cumcount()
scores_2['cum_wins'] = scores_2['cum_wins'].fillna(0)
scores_2['win_pct'] = scores_2['cum_wins']/scores_2['cum_games']
scores_2
win_percentage = scores_2.filter(['GameID','date','team','win_pct','cum_wins','cum_PF','cum_PA'])
scores_3 = pd.merge(scores_2,win_percentage,how='left',left_on=['date','opponent'],right_on=['date','team'],suffixes=('','_opponent'))
scores_3 = scores_3.drop(['GameID_opponent','team_opponent'],axis=1)
scores_3['win_pct_var'] = scores_3['win_pct'] - scores_3['win_pct_opponent']
scores_3['cum_wins_var'] = scores_3['cum_wins'] - scores_3['cum_wins_opponent']
scores_3['cum_PF_cum_PA_opp'] = scores_3['cum_PF'] - scores_3['cum_PA_opponent']
scores_3['cum_PA_cum_PF_opp'] = scores_3['cum_PA'] - scores_3['cum_PF_opponent']
scores_3.head()<jupyter_output><empty_output><jupyter_text>Import player stats for each game<jupyter_code>player = pd.read_csv("C:/NFL Prediction/Data Raw/ps18.csv",index_col=0)
player.head()
player.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 13649 entries, 1 to 13649
Data columns (total 57 columns):
Season           13649 non-null int64
game.id          13649 non-null int64
date             13649 non-null object
Team             13624 non-null object
playerID         13649 non-null object
name             13649 non-null object
pass.att         13649 non-null int64
pass.comp        13649 non-null int64
passyds          13649 non-null int64
pass.tds         13649 non-null int64
pass.ints        13649 non-null int64
pass.twopta      13649 non-null int64
pass.twoptm      13649 non-null int64
rush.att         13649 non-null int64
rushyds          13649 non-null int64
rushtds          13649 non-null int64
rushlng          13649 non-null int64
rushlngtd        13649 non-null int64
rush.twopta      13649 non-null int64
rush.twoptm      13649 non-null int64
recept           13649 non-null int64
recyds           13649 non-null int64
rec.tds          13649 non-null int64
reclng       [...]<jupyter_text>Aggregate the columns to team and date level<jupyter_code>team = player.groupby(['Team','date']).sum().reset_index()
team.head()<jupyter_output><empty_output><jupyter_text>Run Model<jupyter_code>scores_3.fillna(0, inplace=True)

X=scores_3[['score1MA','hometeam','cum_wins_var','cum_PF','cum_PA','cum_PF_cum_PA_opp','cum_PA_cum_PF_opp']]  # Features
y=scores_3['win']  # Labels

# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) # 70% training and 30% test
clf=RandomForestClassifier(n_estimators=100,min_samples_leaf=20)
clf.fit(X_train,y_train)
clf.score(X_test,y_test)
clf.score(X_train,y_train)
feature_importances = pd.DataFrame(clf.feature_importances_,
                                   index = X_train.columns,
                                    columns=['importance']).sort_values('importance',ascending=False)
feature_importances
team.corr()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1586546927275929, 'fraction_numerical': 0.04384574749075541, 'mean_word_length': 5.918392204628502, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '210091', 'license': 'Apache-2.0', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2018, 'n_words': 358, 'path': '_notebooks/2020-07-07-NFL Score Prediction.ipynb', 'repo_name': 'willbreeden/ds-blog', 'size': '5679'}