sodcalculator

10 Tech How-To

ML Tire Degradation & Pit Stop Windows in F1

Guide 10-45985 · Updated · Step by step

This guide explains how to build a machine learning model that predicts tire degradation and recommends pit stop windows in Formula 1. You’ll learn the data pipeline, feature engineering, model selection, and how to turn predictions into actionable pit strategy. No prior ML experience is required, but familiarity with Python and basic statistics will help.

What You’ll Need

  • Python 3.8+ installed
  • Libraries: pandas, numpy, scikit-learn, matplotlib, seaborn (install via pip)
  • Access to F1 lap-by-lap data (e.g., from FastF1 API, Ergast API, or Kaggle datasets)
  • Basic understanding of regression and classification concepts
  • A code editor (VS Code, Jupyter Notebook, etc.)

Step-by-Step

  1. Collect and Load Data
    Use the FastF1 Python library to fetch session data. For example, load a race session and extract lap times, tire compounds, and track temperature. Ensure you have data for multiple races to train a robust model.
  2. Clean the Data
    Remove laps with missing values, pit laps (where lap time is artificially low), and outlier laps caused by safety cars or incidents. Filter to only laps where the driver is on a consistent stint (no pit stop in between).
  3. Feature Engineering
    Create features that influence degradation:

    • Lap number within the stint (stint_lap)
    • Tire compound (categorical: soft, medium, hard)
    • Track temperature (°C)
    • Fuel load (approximate by remaining laps or use a constant if not available)
    • Driver skill proxy (e.g., average lap time in clean air)
    • Track type (street vs. permanent circuit) – one-hot encoded

    The target variable is lap time (or degradation rate, i.e., lap time minus the driver’s best lap in the stint).

  4. Split Data into Train/Test Sets
    Split by race or session to avoid data leakage. For example, train on races from 2020-2021, test on 2022. This ensures the model generalizes to new conditions.
  5. Choose a Model
    Start with a Gradient Boosting Regressor (e.g., XGBoost or LightGBM) or a Random Forest. These handle non-linear relationships and interactions well. For simplicity, scikit-learn’s RandomForestRegressor works fine.
  6. Train the Model
    Fit the model on the training data. Use cross-validation to tune hyperparameters like number of trees, depth, and learning rate. Monitor mean absolute error (MAE) on the validation set.
  7. Evaluate on Test Data
    Predict lap times on the test set. Calculate MAE and R². A good model should have MAE under 0.5 seconds per lap. Plot predicted vs. actual lap times to spot systematic errors.
  8. Predict Degradation Curves
    For a given race, simulate a stint by feeding the model with increasing stint_lap values while holding other features constant. This gives a predicted lap time curve that rises as tires degrade.
  9. Determine Pit Stop Window
    Define a threshold lap time (e.g., 1.5 seconds slower than the driver’s best lap). The pit stop window is the range of laps where the predicted lap time crosses this threshold. Also consider the time lost in the pit lane (typically 20-25 seconds) – the window should be when the time gained on fresh tires exceeds the pit loss.
  10. Visualize and Communicate
    Plot the degradation curve with the threshold line and pit window shaded. Share this with the strategy team, noting confidence intervals if possible.

Common Mistakes

  • Data leakage: Including future information (like final race position) in training features.
  • Ignoring track temperature: Tire degradation is highly temperature-sensitive; omitting it leads to poor predictions.
  • Using raw lap times without context: Lap times vary due to traffic, so always use relative measures (e.g., delta to best lap).
  • Oversimplifying pit loss: The time lost in the pit lane isn’t just the stop time; include the in-lap and out-lap time loss.
  • Not validating on unseen races: A model that works on one circuit may fail on another; always test on a different race.

Tips

  • Start with a simple linear regression as a baseline before moving to complex models.
  • Use feature importance to understand which factors matter most – often track temperature and stint lap dominate.
  • Consider using a separate model per compound, as degradation rates differ significantly.
  • Incorporate driver-specific random effects if you have enough data per driver.
  • Use rolling averages to smooth noisy lap times.
  • Always save your model and pipeline so you can reuse it for new races.

Troubleshooting

  • Model predicts constant lap times (no degradation): Check if the target variable actually varies with stint lap. Add interaction features or try a more flexible model.
  • High error on out-of-sample races: This often indicates overfitting to specific circuits. Reduce model complexity, add more training data, or use cross-validation by race.
  • Missing data in FastF1: Some older races may lack tire data. Use the Ergast API as a fallback or interpolate missing values.
  • Pit window too wide: Tighten the threshold or use a probabilistic approach (e.g., predict the probability of being over the threshold).
  • Code crashes due to memory: Limit the number of laps per stint or downsample the data.

Frequently Asked Questions

  1. How much data do I need?
    At least 5-10 races with full lap data to get meaningful results. More is better, especially across different track types.
  2. Can I use deep learning?
    Yes, but it’s usually overkill. Gradient boosting often performs just as well with less data and tuning.
  3. How do I handle different tire compounds?
    Include the compound as a categorical feature. You can also train separate models per compound if data allows.
  4. What is the typical accuracy?
    With good data, you can achieve MAE of 0.3-0.5 seconds per lap. This is sufficient to identify pit windows within a few laps.
  5. Do I need to account for weather changes?
    Yes, rain or temperature swings can drastically affect degradation. Include weather data if available, or at least flag wet sessions.
  6. How do I handle a safety car?
    Safety car periods reset tire performance. Either exclude those laps or add a feature indicating a safety car period.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →