DEV Community

kawasaki031677
kawasaki031677

Posted on

Experiencing Machine Learning with Premier League Data

Introduction: Taking on Machine Learning!

I've been studying machine learning recently. To gain practical experience, I decided to work with Premier League data and apply machine learning techniques to it.

Using Premier League Match Data

For this project, I used match result data from the 2024-2025 season. The data was downloaded from this site.

As the main features, I selected the following:

  • Shots (HS, AS)
  • Shots on Target (HST, AST)
  • Fouls (HF, AF)
  • Cards (HY, AY)

The target variable is the match result (win, draw, or loss).

Building a Machine Learning Model

Here's the actual code I used:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
import joblib

# 1. Load data
df = pd.read_csv('epl2024.csv')

# 2. Extract only necessary columns
features = ['HS', 'AS', 'HST', 'AST', 'HF', 'AF', 'HC', 'AC', 'HY', 'AY']  # Shots, fouls, cards, etc.
target = 'FTR'  # Match result (H/D/A)

# 3. Create feature and target data
X = df[features]
y = df[target]

# 4. Label encoding (convert target variable to numbers)
le = LabelEncoder()
y_encoded = le.fit_transform(y)  # H->0, D->1, A->2, etc.

# 5. Split into training and test data
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)

# 6. Create and train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# 7. Prediction and evaluation
y_pred = model.predict(X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=le.classes_))

# 8. Save the trained model and label encoder
joblib.dump(model, 'epl_rf_model.pkl')
joblib.dump(le, 'label_encoder.pkl')
Enter fullscreen mode Exit fullscreen mode

Looking at Prediction Results and Evaluation

When I ran the code, I got the following output:

Accuracy: 0.5921052631578947

Classification Report:
               precision    recall  f1-score   support

           A       0.62      0.75      0.68        28
           D       0.15      0.12      0.14        16
           H       0.76      0.69      0.72        32

    accuracy                           0.59        76
   macro avg       0.51      0.52      0.51        76
weighted avg       0.58      0.59      0.58        76
Enter fullscreen mode Exit fullscreen mode

The accuracy (correct prediction rate) was 59%. Looking at the details more closely, I can see that away wins (A) and home wins (H) have both high precision and recall, so the model predicts them fairly accurately. On the other hand, draws (D) are difficult for the model to predict.

Making Actual Predictions

Now that I've created the model, let me use it to make predictions on new data:

import pandas as pd
import numpy as np
import joblib

model = joblib.load('epl_rf_model.pkl')
le = joblib.load('label_encoder.pkl')

# New match data (as a DataFrame with feature names)
new_match = pd.DataFrame(
    [[15, 8, 1, 3, 10, 12, 5, 4, 2, 1]],
    columns=['HS', 'AS', 'HST', 'AST', 'HF', 'AF', 'HC', 'AC', 'HY', 'AY']
)

predicted_num = model.predict(new_match)
predicted_label = le.inverse_transform(predicted_num)

print(f"Prediction result: {predicted_label[0]}")
Enter fullscreen mode Exit fullscreen mode

What Was the Prediction Result for the New Match?

The output was as follows:

Prediction result: A
Enter fullscreen mode Exit fullscreen mode

In this example, the home team had only 1 shot on target while the away team had 3 shots on target, so the model predicted an away team victory.

Summary

Even though I had almost no knowledge of machine learning, working hands-on with actual data deepened my understanding. Next, I'd like to try more advanced analysis, such as predicting next season's standings (hoping my favorite Manchester United will finish in first place...!)

Top comments (0)