The best Formula 1 drivers of 2022: a k-means clustering exercise
How to group drivers with k-means, pick the number of clusters with the elbow method and interpret the result with a radar chart and a random forest.
Using data from the first four races of the 2022 Formula 1 season, the author grouped drivers with k-means to build a Formula 1 Fantasy team. The exercise walks through cleaning the dataset, choosing the number of clusters, interpreting what sets each group apart and turning the result into a decision.
TABLA DE CONTENIDOS
Written in June 2022, this article uses data from that year’s Formula 1 season to show, step by step, how to group items with k-means, how to choose the number of groups and how to interpret what sets each group apart in order to make a decision.
Ever thought about placing bets with cluster analysis?
This analysis started with the idea of building the best possible team in Formula 1 Fantasy and winning a bet with my friends. My plan was to split the drivers into clusters (groups), interpret those groups to understand which criteria the algorithm used to divide them and, with a little luck, find out which drivers were the best of the season.
The data: the first four races of 2022
The analysis began on May 4, 2022, when four Formula 1 races had been held: Bahrain, Saudi Arabia, Australia and Italy. For each driver, the dataset included name, nationality, age, team, driver points, team points, and qualifying and race results for each of those four races.
Ten teams with two drivers each were racing in 2022, but Sebastian Vettel tested positive for COVID-19 before the first two races, so he missed the Bahrain and Saudi Arabian Grands Prix; Nico Hülkenberg replaced him.


Data cleaning and feature engineering
The original dataset listed every race of the 2022 championship, including those that had not been held yet. So I removed those races and did some more data cleaning. This is what the original dataset looked like:

After dropping some columns with empty values, converting object columns to integer or float, replacing the ‘Team’ column with championships won divided by championships contested, and adding columns such as ‘total_DNS’ (did not start) and ‘frequency’ (races finished in the points divided by races entered), the dataset looked like this:

K-means and the elbow method
With clean data, we were ready to build the clusters. To decide how many groups to split the drivers into, we used the elbow method, which helps data scientists choose the ideal number of groups. The method tries different numbers of clusters and computes an error metric for each. The results are plotted with the number of clusters on the X axis and distortion on the Y axis. One look at the chart shows why it is called the elbow method: the elbow is the inflection point of the curve. In this exercise the error metric is distortion, the sum of squared distances from each driver to the center of their cluster. Other metrics can be used as well, such as the average silhouette coefficient, which was also calculated but is not shown in the chart.

Applying k-means with k=2, the algorithm assigns each driver to cluster 0 or cluster 1. At this point we only know how many drivers are in each cluster, not how they differ, that is, what defines each cluster.
Interpretability: what sets each cluster apart
To answer that, I built a radar chart, which shows the differences between the clusters across the most important columns at the same time.

The chart shows that the best drivers ended up in cluster 1: those with the most points, both individual and team, those who finish in the points most often and those on teams with more championships. The only area where cluster 0 “wins” is ‘total_DNS’, the number of races a driver did not start, where lower is better.
Machine learning has great potential, but algorithms usually don’t explain why they do what they do (here, why they put a driver in one group or the other), which is a barrier to adoption. Fortunately, there is an entire field devoted to making machine learning models and their decisions interpretable. A common approach is to measure the importance (the classification power) of each feature. With that in mind, and to find out which “rules” the algorithm used to separate the clusters, I trained a RandomForestClassifier with the assigned cluster as the target; this classifier builds many decision trees. Once interpreted, the first result was simple and easy to see:

This means that if team points are above 61.5, the driver goes to cluster 1; otherwise, to cluster 0. To make the exercise harder, we removed the ‘Team Points’ column, and the result changed and became less obvious:

This is the new way the clusters split without ‘Team Points’, and separating them by eye is harder. Cluster 1 holds the drivers with more than 26.0 ‘Driver Points’ and a Bahrain grid position of 11 or better. Cluster 0 holds everyone with 26.0 ‘Driver Points’ or fewer, plus those with more than 26.0 but a Bahrain grid position worse than 11.
The result: my fantasy team


Seeing which cluster each driver landed in, I built my Formula 1 Fantasy team. I couldn’t pick every driver from cluster 1, because I had a budget and those drivers are the most expensive, but I did pick three drivers from cluster 1 and two from cluster 0.

The image shows the team I picked for the bet: the three cluster 1 drivers are Leclerc, Sainz and Pérez, and the two from cluster 0 are Alonso and Bottas.
What happened after changing the team
Thanks to the drivers I picked after the analysis, my results improved and I moved closer to first place; finishing first would win me the prize. The image below shows how my results improved sharply after the Miami Grand Prix: I came second in Spain and first in Monaco. The improvement came from the team change after the analysis.

The bet with my friends ran until November 20, 2022, the date of the season’s last race in Abu Dhabi, which would decide the winner.
What the analysis delivered
Although I didn’t reach first place in the overall standings in the two races held after using the analysis to change my team, I believe the research helped a lot to improve the team and, with it, my results each race weekend.
Juan Martín Stirling
Data & Analytics. Business Data Science student