top of page
  • Facebook
  • Twitter
  • Spotify
  • Apple Music

Touchdown or Fumble? Using AI to Predict NFL Play Yards (Spoiler: It's Mostly Fumbles)

Writer: Chinmay Bharathulwar
Chinmay Bharathulwar
Sep 20, 2023
3 min read

Introduction

Welcome to the first day of our exploration into the world of Artificial Intelligence (AI) in the NFL! Over the next week, we'll dive into how AI and Machine Learning (ML) can be utilized to analyze and predict various aspects of the game. Today, we'll start with a fundamental problem: predicting the yards gained on a play based on several key features.

We'll use a dataset from the 2022 NFL season and apply machine learning models to predict the number of yards gained on a play. Let's get started with some data preparation and initial modeling.

Data Preparation

First, let's load the play-by-play data for the 2022 NFL season and inspect its structure. a




Selecting Relevant Features

For our prediction task, we'll focus on the following features:

  • Offense Team

  • Play Type

  • Formation

  • Quarter

  • Minute

  • Down

  • Yard To Go

  • Yard Line

  • Yards (our target variable)

We'll also include additional features to enhance our model's predictive power.

Encoding Categorical Variables

Machine learning models require numerical input, so we need to encode our categorical features (Formation, PlayType, OffenseTeam, etc.).


Train-Test Split

In machine learning, we need to evaluate the model's performance on data it hasn't seen before to ensure it generalizes well. To do this, we split our dataset into two parts: training and testing sets. The training set is used to train the model, while the testing set is used to evaluate its performance.

Math Behind the Split

The function train_test_split randomly divides the data. The parameter test_size=0.2 indicates that 20% of the data is used for testing, and 80% for training. The random_state=42 ensures reproducibility by setting a seed for the random number generator.


Training a Random Forest Model

Random Forest Regressor

A Random Forest is an ensemble learning method that combines multiple decision trees to produce a more robust and accurate model. Here's how it works:

  1. Bootstrap Sampling: Random subsets of the training data are created with replacement.

  2. Decision Trees: Each subset is used to train a decision tree. Trees are grown to their full depth without pruning.

  3. Aggregation: The final prediction is obtained by averaging the predictions of all individual trees (for regression tasks).

Math Behind Random Forest

  • Decision Tree: A decision tree splits the data into subsets based on feature values. The goal is to maximize information gain (or minimize impurity) at each split.

  • Random Forest: By averaging the predictions of multiple decision trees, the model reduces variance and improves generalization. Each tree is trained on a different subset, and the features are randomly selected at each split, introducing randomness and reducing overfitting.

Results

Mean Squared Error (MSE)

MSE measures the average of the squares of the errors—that is, the average squared difference between the predicted values and the actual value.


where yi are the actual values, y^i (hat)​ are the predicted values, and n is the number of observations.


R² is the proportion of the variance in the dependent variable that is predictable from the independent variables. It ranges from 0 to 1.

Here is the code for this

Interpretation of Results

  • Mean Squared Error (MSE): 59.06

  • This indicates that, on average, the squared difference between the predicted and actual yards is 59.06. Lower values indicate better model performance.

  • R-squared (R²): 0.12

  • The model explains approximately 12% of the variance in the yards gained. This suggests that the model captures some of the underlying patterns but has significant room for improvement.


Conclusion

In this first installment, we've established a foundational machine learning model to predict the number of yards gained on a play using a Random Forest Regressor. Despite the performing like Dak during playoffs, this baseline model sets the stage for more advanced modeling techniques and feature engineering.


Stay tuned for Day 2, where we'll explore advanced models and techniques to enhance our predictions. We'll dive into feature engineering, hyperparameter tuning, and the use of more sophisticated algorithms to improve our model's accuracy.

Comments


bottom of page