In this article, I’ll delve into the world of football in an attempt to predict teams‘ winning chances based on last seasons performance.
First I’ll retrieve last season’s standings from https://www.football-data.org/ API.
import requests
# Retrieving data from the API
standings_url = "https://api.football-data.org/v4/competitions/PL/standings?season=2024"
""
headers = {
"X-Auth-Token": "827366ecd4ba47f1a7b9760f0da663d1"
}
response = requests.get(standings_url, headers=headers)
standings_data = response.json()
Next is to calculate the estimated probability of winning this season using number of games won, and number of games played.
def win_probability(standings_data):
teams_win_probabilities = []
table = standings_data["standings"][0]["table"]
for row in table:
team_name = row["team"]["name"]
wins = row["won"]
played = row["playedGames"]
if played > 0:
win_rate = (wins / played) * 100
teams_win_probabilities.append({"team": team_name, "playedGames": played, "won": wins, "win_rate": round(win_rate, 2)})
else:
print(f"Skipping {row['team']['name']} because playedGames={played}")
print(f"Total teams processed: {len(teams_win_probabilities)}")
return teams_win_probabilities
To return output:
results = win_probability(standings_data)
for index, team in enumerate(results, start=1):
print(f"{index}. {team['team']}: nPlayed Games- {team['playedGames']}, Wins- {team['won']}, {team['win_rate']}% win rate")
Sample output of the first 5 teams on 2024/25 EPL table:
Total teams processed: 20
1. Liverpool FC:
Played Games- 38, Wins- 25, 65.79% win rate
2. Arsenal FC:
Played Games- 38, Wins- 20, 52.63% win rate
3. Manchester City FC:
Played Games- 38, Wins- 21, 55.26% win rate
4. Chelsea FC:
Played Games- 38, Wins- 20, 52.63% win rate
5. Newcastle United FC:
Played Games- 38, Wins- 20, 52.63% win rate
Final Thoughts
This may not give the exact results as to what team would win this season, however, it shows how likely that outcome is.
It is helpful for football pundits and analysts to gain insights into trends in the world of football.