Conrad Lotz
← All posts
Data ScienceMachine LearningSoftware Engineer4 min read

Simple prediction model in Python

Python football match prediction model using machine learning

Demystifying machine learning through hands-on collaboration with chatGPT: A journey from uncertainty to mastery, exploring RandomForest and LogisticRegression for practical football predictions.

I’ve been dabbling around in machine learning for a while by doing boot camps, and reading articles but truth be told, it never really sunk in how one can create a practical model you understand without just copy-paste and clicking the run button.

I did not do this by myself if I have to be completely honest. I have to thank my coding *buddy *chatGPT for patiently listening and working with me until I was able to successfully create the prediction model.

To be able to argue* *with chatGPT until you get to the right place and have the correct implementation is incredible and one achieves a higher learning experience because you and chatGPT have to figure it out together. chatGPT was not able to give me the answer straight away. It took some time and collaboration to come to the correct code.

The Model

I’ve been dabbling around in data science for a while by doing boot camps and reading articles. Truth be told, it never really sunk in how one can create a practical model you understand without just copy-paste and clicking the run button.

I did not do this by myself if I have to be completely honest. I have to thank my coding *buddy *chatGPT for patiently listening and working with me until I was able to successfully create the prediction model.

Out of curiosity, I’m employing two models to contrast outcomes for increased insight and thoroughness.

Some important components

**LogisticRegression **is a statistical method used for binary classification, a type of predictive analysis. It’s particularly well-suited for scenarios where the outcome is dichotomous, meaning there are only two possible classes.

RandomForestClassifier is an ensemble of decision trees, typically using bagging. It creates multiple decision trees and merges them together to get a more accurate and stable prediction.

Bagging, or Bootstrap Aggregating, is an ensemble learning technique used to improve the stability and accuracy of machine learning models by training multiple models on different bootstrap samples (subsets with replacement) of the dataset and then aggregating their predictions, typically through averaging or majority voting. This approach is particularly effective with high-variance models like decision trees, reducing overfitting and leading to more robust and reliable predictions.

I decided to use both these regressions for this example to investigate how it works and what the differences in accuracy and results are. Let’s dive in, shall we?

The Code

The packages we require for this are scikit-learn and Pandas

pip install scikit-learn, pandas

Alternatively, add these into a requirements.txt row separated and run the following command

pip install -r requirements.txt

I will load the fifa_football_results.csv, which includes 45,000 current football results, into a pandas DataFrame.

df = pd.read_csv("fifa_football_results.csv")

Create a target variable representing the match outcome for the home team

df['home_team_result'] = df.apply(lambda row: 'win' if row['home_score'] > row['away_score'] else ('lose' if row['home_score'] < row['away_score'] else 'draw'), axis=1)

Model accuracy comparison: LogisticRegression at 57% vs RandomForestClassifier at 52% on football predictions

LogisticRegression has a higher accuracy in predicting the winner with 57% compared to the 52% for RandomForestClassifier. The latter would be better for more complex predictions, and that is why the former has a higher accuracy—it is better to work with less complex datasets and predictions.

I’m going to use the following sets of 2 teams to run the model. I’m expecting the first

  • England vs Norway

  • Argentina vs Brazil

  • South Africa vs Wales

In the first matchup, I expect England to win. It is also worth noting that these teams have played against each other in the past. The second matchup I expect to be very close and I cannot predict who will be the winner. This should be the more fun exercise. Lastly, I chose two teams who have never played against each other.

…and the winner is

England vs Norway

Machine learning prediction result for England vs Norway — England wins

Winner: England

Argentina vs Brazil

Machine learning prediction result for Argentina vs Brazil — Argentina wins

Winner: Argentina

South Africa vs Wales

Machine learning prediction result for South Africa vs Wales — Wales wins

Winner: Wales

Conclusion

It was a fun first outing and a completely different experience from doing boot camps like kaggle.com and datacamp.com for example. With no knowledge of these models and machine learning functions I had to start from scratch and with some guidance from chatGPT, I was able to get to the correct starting point which always takes the longest.

With chatGPT solving all our problems, it is still worth understanding the basics of machine learning. That is what I am to do while. Organizations still need to crunch very large datasets for new analytics and insights.

Please find my code repository on Github

#data science#machine learning