GithubHelp home page GithubHelp logo

upeppe / fantabeto Goto Github PK

View Code? Open in Web Editor NEW
35.0 4.0 8.0 136.85 MB

Machine learning model for predicting Serie A players performance in a match, in terms of Fantacalcio (italian fantasy football) scores.

License: MIT License

Jupyter Notebook 100.00%
ai fantacalcio fantasy-football machine-learning neural-network python serie-a tensorflow tensorflow-probability

fantabeto's Introduction

fantabeto

Fantacalcio Bayesian Estimated Team's Outcome

Machine learning model for predicting Serie A players performance in a match, in terms of Fantacalcio (italian fantasy football) scores.

https://pub.towardsai.net/how-i-won-at-italian-fantasy-football-fantacalcio-using-machine-learning-ce8fc3fdcaef

The aim of this project is to predict Fantacalcio (Serie A fantasy football) player performances (vote and fantavote, respectively their match rating and that summed to the bonus/malus given by goals, assists and cards), using players and teams data from http://fantacalcio.it and http://fbref.com.

"outputs" folder contains predictions for the next Serie A matchdays, and an excel file for analysis. A list of players can be inserted to help selecting an optimal line-up.

The code has been adapted for Serie-A season 2023/2024, updating the statistics database and taking into account the stats from other leagues, for the players who are at their first season in Serie A (see rookies stats folder).

png

Two neural network models are trained for predicting vote and fantavote for outfield players and goalkeepers, for which the clean sheet probability is also an output. The outputs of these models are not raw predictions, but probability distributions, in the form of SinhArcsinh, which is a skewed distribution, meaning that the probability density is asymmetric. For predicting clean sheet probability, a Bernoulli distribution is instead used (a sample of which would be clean sheet = 1, with a given probability p, or clean sheet = 0 with probability 1-p)

See the following code and plot to show an example of vote and fantavote probability distributions. In this case, the player would be an attacking one, whose fantavote distribution is very skewed to the right (it is very more probable to score a goal and receive a 10 = 7+3 fantavote, than having an awful performance with a 4 fantavote!).

import numpy as np
import matplotlib.pyplot as plt

def sinh_archsinh_pdf(x, mu, sigma, eps, delta):
    mul = 2 / np.sinh( np.arcsinh(2) * delta)    
    z = (x - mu) / (sigma*mul)            
    S = np.sinh( -eps + (1/delta) * np.arcsinh(z))
    return np.exp(-0.5 * S * S) * np.sqrt(1 + S * S) / ( sigma * mul * delta ) / np.sqrt(1 + z * z) / np.sqrt(2 * np.pi)

x = np.arange(start = 0, stop = 30, step = 0.001)
pxv = sinh_archsinh_pdf(x, 6.06, 0.62, 0.33, 1.06)
pxf = sinh_archsinh_pdf(x, 5.6657, 1.224146, 0.795868, 1.9983)
plt.plot(x, pxv, label = 'vote', color = 'b')
plt.plot(x, pxf, label = 'fantavote', color = 'g')
plt.fill_between(x, pxv, color = 'lightblue')
plt.fill_between(x, pxf, color = 'lightgreen')
mv = np.average(x, weights = pxv)
mf = np.average(x, weights = pxf)
plt.vlines(x = mv, color = 'b', ymin = 0, ymax = 3, linestyle = 'dashed', label = 'mean vote = ' + '{:.2f}'.format(mv))
plt.vlines(x = mf, color = 'g', ymin = 0, ymax = 3, linestyle = 'dashed', label = 'mean fantavote = ' + '{:.2f}'.format(mf))
plt.legend()
plt.xlim([0, 20])
plt.ylim([0, 1])
plt.ylabel('Probability Density')
plt.xlabel('Vote')
plt.title('Beto (A) estimated performance')
plt.show()

png

There is code also for computing the expected points outcome of a particular line-up, by sampling multiple times from the players' points distribution and considering bonus for Defense Modifier ("Modificatore") and Clean Sheet. This leads to estimating the team's points probability distribution.

png

Credits:

http://Fantacalcio.it - The game! And of course, a lot of data, including votes, players list and probable line-ups.

http://FBRef.com - Plenty of stats for football players and teams.

https://github.com/amiles2233/ff_prob - Inspiration, for using Tensorflow Probability and Bayesian Neural Networks for this task.

https://github.com/parth1902/Scrape-FBref-data - FBref data scraping code.

#fantacalcio #fantasy-football #serie-a

#machine-learning #ai #neural-networks

#python #tensorflow #tensorflow-probability

fantabeto's People

Contributors

upeppe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fantabeto's Issues

IndexError running 1_scraping_fbref.ipynb

Hi, when I run the 3rd cell of "1_scraping_fbref.ipynb" I receive this error, can you help me?

IndexError Traceback (most recent call last)
Cell In[3], line 1
----> 1 df_outfield = get_outfield_data('https://fbref.com/en/comps/11/','/Serie-A-Stats')
3 df_outfield.to_csv('fbref_data/outfield_players.csv', index=False)
5 df_outfield

Cell In[2], line 152
151 def get_outfield_data(top, end):
--> 152 df1 = frame_for_category('stats',top,end,stats)
153 df2 = frame_for_category('shooting',top,end,shooting2)
154 df3 = frame_for_category('passing',top,end,passing2)

Cell In[2], line 136
134 def frame_for_category(category,top,end,features):
135 url = (top + category + end)
--> 136 player_table, team_table = get_tables(url,'for')
137 df_player = get_frame(features, player_table)
138 return df_player

Cell In[2], line 45
42 soup = BeautifulSoup(comm.sub("",res.text),'lxml')
43 all_tables = soup.findAll("tbody")
---> 45 team_table = all_tables[0]
46 team_vs_table = all_tables[1]
47 player_table = all_tables[2]

IndexError: list index out of range

Adding a section In README to illustrate project Installation and Startup

Hi @uPeppe ,

Hi UPeppe,

I have been reviewing your project, and I find it highly interesting. To assist users who may not be very familiar with TensorFlow, I think it would be beneficial to add a section to the README that outlines the installation and setup process of the project.

Would you be willing to include such a section?

Thank you!

GKs performance estimation?

Hi @uPeppe,

In the accompanying blog post you stated how the performance estimation model was good on movement players, but subpar on goalkeepers (GKs). Have you further experimented on the root causes of this phenomenon?

Some clarifications about the project

Hi @uPeppe ,

I've read your article on Medium and I have a few questions:

  • Did you use the project for both Fantacalcio Classic and Mantra?
  • For the Mantra version, how did you handle players with multiple roles and select the best formation among the various available modules for each matchday?
  • Did you apply the sin-arcsinh probability distribution to goalkeepers as well?
  • Which bonuses and penalties did you consider? (e.g., goalkeeper clean sheets, defense modifier)
  • For each matchday, did you consider validating the results by calculating the delta compared to the actual best formation?

Thanks!

Missing requirements.txt

Here is the translated text:

"The requirements.txt file is missing, so it is necessary to inspect each file to understand which libraries are required."

Selecting team at the start of the season and preparing for the January market auction

I've read carefully the code and your article on Medium: it's really a great work, no doubts.

When reading about the implementation of the model I haven't understood two concepts:

  1. How to select the team at the start of the season: I've read your reply to a comment on medium and you stated that you've made the initial team by applying the prediction using as opposing team one made through averaging all the stats of Serie A teams. I didn't find this reference in the github code.

  2. In the Medium article you say that by using the algorithm to predict selecting a statistically average Serie A team as an opponent, it was useful in preparing for the January market auction: where is this implemented in the code?

I'm trying to understand all the parts of the model better, I hope I haven't inconvenienced you.

Thanks for your time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.