Skip to content

How to model Soccer: Python Tutorial

The Task

This notebook will outline how to train a classification model to predict the outcome of a soccer match using a dataset provided by

  1. Reading data from file and get a raw dataset
  2. Data cleaning and feature engineering
  3. Training a model
  4. The tutorial covers the thought process of manipulating the dataset (why and how), some simple data cleaning, feature engineering and training a classification model.

The tutorial DOES NOT delve deep into the fundamentals of machine learning, advanced feature engineering or model tuning.

There are some helpful hints along the way though.

# import required libraries

import numpy as np
import pandas as pd
import os

import warnings

Read data from file and get a raw dataset

Change the data types - date column.

We need the date column in good order for our tutorial. Here's the data set we're using for this tutorial.

In general, it's a good idea to evaluate data types of all columns that we work with to ensure they are correct.

df = pd.read_csv('soccerData.csv')
df['date']= pd.to_datetime(df['date'])

Get data columns and create raw dataset

For this tutorial, let's take only a few stats columns to work with.

Typically we would explore all features and then decide which data to discard.

  1. Goal counts
  2. Half Time Goal Counts
  3. Corners
  4. Total shots
  5. Shots on target
  6. Fouls
  7. Yellow Cards
  8. Red Cards
raw_match_stats = df[[

Clean data

As a cleaning step, we order our data by date and drop rows with NA values.

raw_match_stats = raw_match_stats.sort_values(by=['date'], ascending=False)

raw_match_stats = raw_match_stats.dropna(inplace=True)

Raw dataset

This raw dataset is structured so that each match has an individual row and stats for both teams are on that row with columns titles "home" and "away".

Our goal is to build a machine learning (ML) model that can predict the result of a soccer match. Given that we have some match stats, we will aim to use that information to predict a WIN, LOSS or DRAW.

date match_id home_team_name away_team_name home_team_goal_count away_team_goal_count home_team_half_time_goal_count away_team_half_time_goal_count home_team_shots away_team_shots home_team_shots_on_target away_team_shots_on_target home_team_fouls away_team_fouls home_team_corner_count away_team_corner_count home_team_yellow away_team_yellow home_team_red away_team_red
6/11/2023 222305 Tottenham Chelsea 1 4 1 1 8 17 5 8 12 21 1 6 1 5 2 0
5/11/2023 222304 Luton Liverpool 1 1 0 0 8 24 5 6 7 13 4 7 1 1 0 0
5/11/2023 222303 Nottm Forest Aston Villa 2 0 1 0 5 13 3 3 6 9 0 10 1 1 0 0
4/11/2023 222299 Everton Brighton 1 1 1 0 10 7 4 2 15 5 3 3 4 2 0 0
4/11/2023 222296 Fulham Man Utd 0 1 0 0 18 12 3 5 9 15 9 4 5 2 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
19/08/2000 213457 Chelsea West Ham 4 2 1 0 17 12 10 5 19 14 7 7 1 2 0 0
19/08/2000 213462 Liverpool Bradford 1 0 0 0 16 3 10 2 8 8 6 1 1 1 0 0
19/08/2000 213463 Sunderland Arsenal 1 0 0 0 8 14 2 7 10 21 2 9 3 1 0 1
19/08/2000 213464 Tottenham Ipswich 3 1 2 1 20 15 6 5 14 13 3 4 0 0 0 0
19/08/2000 213456 Charlton Man City 4 0 2 0 17 8 14 4 13 12 6 6 1 2 0 0

Data cleaning and feature engineering

Target variable - Match Result

Our machine learning model aims to predict the result of a match. This "result" is called the "target variable". Our dataset has no columns showing the match result. We will create two columns for the results for each team. One of these would become the target variable for our ML model.

# create results columns for both home and away teams (3 - win, 1 = Draw, 0 = Loss).

raw_match_stats.loc[raw_match_stats['home_team_goal_count'] == raw_match_stats['away_team_goal_count'], 'home_team_result'] = 1
raw_match_stats.loc[raw_match_stats['home_team_goal_count'] > raw_match_stats['away_team_goal_count'], 'home_team_result'] = 3
raw_match_stats.loc[raw_match_stats['home_team_goal_count'] < raw_match_stats['away_team_goal_count'], 'home_team_result'] = 0

raw_match_stats.loc[raw_match_stats['home_team_goal_count'] == raw_match_stats['away_team_goal_count'], 'away_team_result'] = 1
raw_match_stats.loc[raw_match_stats['home_team_goal_count'] > raw_match_stats['away_team_goal_count'], 'away_team_result'] = 0
raw_match_stats.loc[raw_match_stats['home_team_goal_count'] < raw_match_stats['away_team_goal_count'], 'away_team_result'] = 3

Average pre-match stats - Ten match average

Great! Now we have a dataset with many rows of data, with each row representing match stats and the match result (this would become our target variable).

But our goal is to build an ML model that predicts the match result prior to the start of a match. Are the stats from that match what we need to build this ML model? No! When predicting a match outcome BEFORE the start of the match, we are forced to rely on match stats available to us from previous matches.

Therefore, we need a dataset with the match result (target variable) and stats for each team heading into that match. For this tutorial, we will look at the average stats for each team in the ten matches preceding each match.

Lets look at how we can get the average stats for the previous 10 matches for each team at each match.

  1. Split the raw_match_stats to two datasets (home_team_stats and away_team_stats).
  2. Stack these two datasets so that each row is the stats for a team for one match (team_stats_per_match).
  3. At each row of this dataset, get the team name, find the stats for that team during the last 10 matches, and average these stats (avg_stats_per_team).
  4. Add these stats to the team_stats_per_match dataset.

Why did we chose ten matches? Why not 15? Should we average over a time period (matches in the last year perhaps?) rather than a number? What's the least number of matches available for each competing team in the dataset? These are all interesting questions that may improve our model.

# Split the raw_match_stats to two datasets (home_team_stats and away_team_stats)

home_team_stats = raw_match_stats[[

home_team_stats = home_team_stats.rename(columns={'home_team_name':'name',

away_team_stats = raw_match_stats[[

away_team_stats = away_team_stats.rename(columns={'away_team_name':'name',

# add an additional column to denote whether the team is playing at home or away - this will help us later

# stack these two datasets so that each row is the stats for a team for one match (team_stats_per_match)
team_stats_per_match = pd.concat([home_team_stats,away_team_stats])
# At each row of this dataset, get the team name, find the stats for that team during the last 10 matches, and average these stats (avg_stats_per_team). 

avg_stat_columns = ['goals_per_match','corners_per_match','shots_per_match','shotsOnTarget_per_match','fouls_per_match', 'possession_per_match']
stats_list = []
for index, row in team_stats_per_match.iterrows():
    team_stats_last_five_matches = team_stats_per_match.loc[(team_stats_per_match['name']==row['name']) & (team_stats_per_match['date_GMT']<row['date_GMT'])].sort_values(by=['date_GMT'], ascending=False)

avg_stats_per_team = pd.DataFrame(stats_list, columns=avg_stat_columns)
# At each row of this dataset, get the team name, find the stats for that team during the last 5 matches, and average these stats (avg_stats_per_team). 

avg_lastTen_stat_columns = [

lastTen_stats_list = []
for index, row in team_stats_per_match.iterrows():
    team_stats_last_ten_matches = team_stats_per_match.loc[(team_stats_per_match['name']==row['name']) & (team_stats_per_match['date']<row['date'])].sort_values(by=['date'], ascending=False)

avg_lastTen_stats_per_team = pd.DataFrame(lastTen_stats_list, columns=avg_lastTen_stat_columns)

Average Pre-Match Stats Last 5 Home and Last 5 Away Matches

Often teams can play better at home than away so it's important to consider whether they are playing at home or away. For example in the AFL, the Brisbane Lions are known to be almost unbeatable at the Gabba (there's a reason it's called the Gabbatoir). As such we need to add some additional features.


lastFive_Home_stats_list = []
team_stats_L5_home_matches = team_stats_per_match[team_stats_per_match['home_or_away'] == 'Home']
for index, row in team_stats_L5_home_matches.iterrows():
    team_stats_last_five_home_matches = team_stats_L5_home_matches.loc[(team_stats_L5_home_matches['name']==row['name']) & (team_stats_L5_home_matches['date']<row['date'])].sort_values(by=['date'], ascending=False)

avg_lastFiveHome_stats_per_team = pd.DataFrame(lastFive_Home_stats_list, columns=avg_lastFiveHome_stat_columns)
team_stats_L5_home_matches = pd.concat([team_stats_L5_home_matches.reset_index(drop=True), avg_lastFiveHome_stats_per_team], axis=1, ignore_index=False)

lastFive_away_stats_list = []
team_stats_L5_away_matches = team_stats_per_match[team_stats_per_match['home_or_away'] == 'Away']
for index, row in team_stats_L5_away_matches.iterrows():
    team_stats_last_five_away_matches = team_stats_L5_away_matches.loc[(team_stats_L5_away_matches['name']==row['name']) & (team_stats_L5_away_matches['date']<row['date'])].sort_values(by=['date'], ascending=False)

avg_lastFiveAway_stats_per_team = pd.DataFrame(lastFive_away_stats_list, columns=avg_lastFiveAway_stat_columns)
team_stats_L5_away_matches = pd.concat([team_stats_L5_away_matches.reset_index(drop=True), avg_lastFiveAway_stats_per_team], axis=1, ignore_index=False)
team_stats_L5_home_matches.columns = team_stats_L5_home_matches.columns[:2].tolist() + ['team_1_'+str(col) for col in team_stats_L5_home_matches.columns[2:]]
team_stats_L5_away_matches.columns = team_stats_L5_away_matches.columns[:2].tolist() + ['team_2_'+str(col) for col in team_stats_L5_away_matches.columns[2:]]
home_and_away_stats = pd.merge(team_stats_L5_home_matches,team_stats_L5_away_matches,how='left',on=['date','match_id'])

Reshape average pre-match stats

Now that we have the average stats for each team going into every match, we can create a dataset similar to the raw_match_stats, where each row represents both teams from one match.

  1. Re-segment the home and away teams (name Team 1 and Team 2 rather than home and away).
  2. Combine at each match to get a dataset with a row representing each match.
team_stats_per_match = pd.concat([team_stats_per_match.reset_index(drop=True), avg_lastTen_stats_per_team], axis=1, ignore_index=False)
# Re-segment the home and away teams.
home_team_stats = team_stats_per_match.iloc[:int(team_stats_per_match.shape[0]/2),:]
away_team_stats = team_stats_per_match.iloc[int(team_stats_per_match.shape[0]/2):,:]

home_team_stats.columns = home_team_stats.columns[:2].tolist() + ['team_1_'+str(col) for col in home_team_stats.columns[2:]]
away_team_stats.columns = away_team_stats.columns[:2].tolist() + ['team_2_'+str(col) for col in away_team_stats.columns[2:]]

# Combine at each match to get a dataset with a row representing each match. 
# drop the NA rows (earliest match for each team, i.e no previous stats)
away_team_stats = away_team_stats.iloc[:, 2:]
match_stats = pd.concat([home_team_stats, away_team_stats.reset_index(drop=True)], axis=1, ignore_index=False)
match_stats = match_stats.dropna().reset_index(drop=True)

Train ML model

In our ML model, we will use the raw Team 1 and Team 2 average stats as features.

Some questions we could ask ourselves about this dataset are: (and there is no easy answer without some experimentation)

Would we be better off using the differential between the teams as features?

Can we generate any other useful features from the dataset provided?

Do we need to weigh the home and away teams because home teams win more often?

In this tutorial we will:

  1. Train a model using 68 feature columns
  2. Use a datetime split in training/test data (a random 80/20 split could also be used)
  3. Use accuracy to evaluate our models

It's probably worth evaluating multiple models (several models explained in this tutorial), perhaps use k-fold cross validation, and use metrics other than accuracy to evaluate a model (check the commented out code).

# import required libraries

from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_fscore_support as score, confusion_matrix, roc_auc_score, classification_report, log_loss

from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression

Get data from our dataset

  1. Team_1_result column - target variable
  2. The raw stats of the two teams (68 columns) - features

Do we need to scale or normalize the feature columns in order for it to make mathematical sense to a ML model? This depends on the type of model we are training, but it's definitely worth investigating in order to achieve a high performing model.

We should also investigate the dataset to check if it's balanced on all classes or if it's skewed towards a particular class (i.e are there an equal number of wins, losses and draws?). If not, would this affect model performance?


# Define features
features = ['team_1_average_goalsScored_last_ten',

Split test and training data

We train a model on the training data, and then use test data to evaluate the performance of that model.

train_data = match_stats[match_stats['date'] < '2018-07-01']
test_data = match_stats[match_stats['date'] >= '2018-07-01']

X_train = train_data[features]
X_test = test_data[features]
Y_train = train_data['team_1_result']
Y_test = test_data['team_1_result']

Name and define classifiers

names = ["Nearest Neighbors", "Logistic Regression","Linear SVM", "RBF SVM", "Gaussian Process",
         "Decision Tree", "Random Forest", "Neural Net", "AdaBoost",
         "Naive Bayes", "QDA"]

classifiers = [
    SVC(kernel="linear", C=0.025, probability=True),
    SVC(gamma=2, C=1, probability=True),
    GaussianProcessClassifier(1.0 * RBF(1.0)),
    RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
    MLPClassifier(alpha=1, max_iter=1000),

Iterate through all classifiers and get their accuracy score

We can use the best performing model to make our predictions.

There are several other metrics in the code that have been commented out which might provide helpful insights on model performance.

for name, clf in zip(names, classifiers):
    # Fit the classifier on the training data and make predictions, Y_train)
    test_data[name + '_team_1_result'] = clf.predict(X_test)
    accuracy = clf.score(X_test, Y_test)

    # prediction_proba = clf.predict_proba(X_test)
    # logloss = log_loss(y_test,prediction_proba)
    # precision, recall, fscore, support = score(y_test, prediction)
    # conf_martrix = confusion_matrix(y_test, prediction)
    # clas_report = classification_report(y_test, prediction)

    print(name, accuracy)

# Export the predictions to a CSV file
test_data.to_csv('predictions.csv', index=False)
Nearest Neighbors 0.43313373253493015
Logistic Regression 0.5359281437125748
Linear SVM 0.532435129740519
RBF SVM 0.4431137724550898
Gaussian Process 0.4286427145708583
Decision Tree 0.499001996007984
Random Forest 0.49001996007984033
Neural Net 0.5069860279441117
AdaBoost 0.499500998003992
Naive Bayes 0.5149700598802395
QDA 0.4865269461077844


Note that whilst models and automated strategies are fun and rewarding to create, we can't promise that your model or betting strategy will be profitable, and we make no representations in relation to the code shared or information on this page. If you're using this code or implementing your own strategies, you do so entirely at your own risk and you are responsible for any winnings/losses incurred. Under no circumstances will Betfair be liable for any loss or damage you suffer.