Geek Culture
Published in

Geek Culture

Linearly Optimising Teams for PL Fantasy League

Predict your own optimised FPL Teams with player data from PL using Linear Programming on Google Colab (IPython)

Participating in Football from Home | Photo by JESHOOTS.COM on Unsplash

What are Fantasy Leagues?

Fantasy Leagues are a great way for non-professionals to showcase their talents of analyzing the sport but can also become really addictive, and can become dangerous in the leagues where real money is involved (especially in the case of unofficial Fantasy Leagues).

Fantasy Premier League

Once a user registers themselves for this league, they are provided with £100 million (virtual money) which they need to spend on selecting 15 players from the 20 teams who play in the Premier League for that respective season which goes on for a whole year divided in a total of 38 gameweeks.

  1. Playing a match
  2. Scoring a goal
  3. Assisting a team mate who goes on and scores a goal
  4. Bonus Points if the player plays exceptionally well
  5. Keeping a clean sheet (not for strikers)
  6. Saving a penalty
  1. Receiving a yellow or red card
  2. Conceding a goal (goalkeepers and defenders only)
  3. Missing a penalty kick
  4. Scoring an own goal

What is Linear Programming?

One of the best things about Linear Programming is that as soon as you’ve written down a linear objective and constraints, you’re finished. All you need to do afterwards is to plug it into a solver and benefit from the results.

What does FPL have to do with Linear Programming?

Rules in FPL

  1. You are constrained to a budget of £100.0 million to choose players (where better players cost more money);
  2. You can pick a total of 15 players, which should have 2 goalkeepers, 5 defenders, 5 midfielders and 3 forwards (the category of player is decided by FPL);
  3. Out of these 15, you have to select 11 players to play in a certain gameweek. This 11 should contain 1 goalkeeper, at least 3 defenders, at least 2 midfielders and at least 1 forward. Only these 11 players will earn points;
  4. Only a maximum of 3 players can be selected in the squad from a single team;

Methodology

  1. Select 15 players (Constraint 1)
  2. Select 2 goalkeepers (Constraint 2)
  3. Select 5 defenders (Constraint 3)
  4. Select 5 midfielders (Constraint 4)
  5. Select 3 forwards/strikers (Constraint 5)
  6. Use less than £100.0 million (Constraint 6)
  7. Not select more than 3 players from a single team (Constraint 7)

I decided to use Google Colab as it is efficient and fast. I also used PuLPprogramming library of Python as it allows us to write down an objective function and constraints in a very intuitive way and instantly solve them.

Importing Data

!pip install pydrivefrom pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
downloaded = drive.CreateFile({'id':"1f4ZbuRae1uQ6kkI2XMuon37ATWAKrsK8"})   
# replace the id with id of file you want to access
downloaded.GetContentFile('data.json')
# replace the file name with your file

Extracting Players’ data

Players data extracted from the JSON file | Image by Author

Exploring the extracted data

Correlation of total points with different columns, mean of total points in relation to the position, median of total points in relation to the position and the number of players in a position | Image by Author
  1. Clean sheets: As clean sheets give players points, so obviously players with higher clean sheets have more points.
  2. Goals conceded: Although this shouldn’t be the case but the goals conceded also increases with the amount of matches a player plays, which further leads to increase in points.
  3. Bonus Points (bps): This means a player played well so obviously directly correlates to total points.
  4. Minutes: As from the previous one, the more minutes a player plays, higher the number of points.

Exploring the data visually (Kernel Density Function)

Kernel Density Estimations in relation to players’ positions | Image by Author

Imputations

Kernel Density Estimations in relation to players’ positions after accreditation | Image by Author

Modeling the Linear Problem

Optimisation Function | Image by Author
  1. Goalkeeper Constraint — 1 goalkeeper
  2. Defender Constraint — 4 defenders
  3. Midfielder Constraint — 4 midfielders
  4. Forward Constraint — 2 forwards

Results

Team created for FPL following the rules and constraints | Image by Author

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store