hi everyone so i am internal rosh from
the school of fundamental sciences
massive university so today i am going
to talk about
football uh soccer data analysis uh
pedagogy introduction this is my twitter
handle
and this is the link to my website
thanks spike on a you for giving me this
opportunity to speak at this conference
so like i'm really glad that uh i'm
present here with you all so this is my
second year speaking at this renowned
conference and
welcome everyone so i'm going to
talk about how to get open access even
data from status bomb using the package
called stats bom pi in python
then how to draw a soccer pitch using
mpl soccer package
how to visualize a pass network for a
particular team in a particular match
and how to use uh network x module to
analyze the pass network to draw pass
maps along uh
their corresponding and their
corresponding heat maps
of a
of players from a particular team in a
particular match
and finally we will talk about how to
implement computational geometric
concepts like convex hulls voronoi
diagrams and delanite triangulations
using a python package called scipy dot
special like any other package we use
peep to install status bomb pipe
and then uh
the open data from status pike and stats
bomb can be accessed and like uh please
go through the terms and conditions uh
section
stated at their github documentation
we'll go step by step uh to understand
how to extract the relevant data
and uh using the stats bomb by package
so we also need to import the package
and next we get access to the
competitions data set what the
competition's data set looks uh like
comp data set looks like so we have a
competition id
and a season id so both these together
act as uh like
a unique id for extracting a particular
row we also have the country name here
actually it's the continent on uh from
where the
league belongs
uh so here it's europe uh the
competition name is uh champions league
uh so and the gender is male so
unfortunately the first 15 rows here
that i am showing is uh
of gender male but if you print the
whole uh
data set you will see there are
like rows where genders are females too
season name is uh 2018-19 or like 17-18
and so on you can extract the column
names of comp and this is what ah the
column names are compressionary season
id and so on ah now let us make sense of
a particular row from the comp data set
so for example we if we look into the
row where the competition at is 16 and
the season id is one we notice that the
country name is europe the competition
name is champions league and the season
name is 2017-18 and so so on so suppose
we are satisfied with the above
information and we want to analyze a
game from 1718's champions league season
so we use both this competition id and
season ids
and
this is what we write uh to extract that
particular row so math equals to sb dot
matches competition id equals to 16 and
season 8 equals to 1 so this gives
us the particular match from where we
will extract the even data
so this is uh the matte data set and we
see it also has a match id which is
again the unique id for this particular
data set mat but here we have only one
row
for this data set and the match date
when the match took place uh the kickoff
time
uh as we know it's an european champions
league uh game so it was on
2017-18 the home team was real madrid
and the other team was liverpool and the
home team score was three and their team
score was one now we use this match id
uh to extract the events and store it in
events data set
okay ah so the events data set uh
fetching as the events data for the
particular match and that's what it
looks like it has a
large number of rows
and columns so we see that we are able
to get access to all the events from uh
the real madrid versus the purple match
and like we can also print the column
names using events.columns uh now we
will see how we can draw a
football pitch using mpl soccer
and this package was developed by uh
and developed and maintained by animal
rogapal and andrew rollinson please go
and check them out
and like
like any other package we pp installed
it first
and then we uh
we also need to import the matplotlib
and the
matplotlib and the pitch classes uh so
this is what we do here import macbook
live.pipe.spl and uh from mpl
soccer.page import
page but here p is capacity that uh with
the function pitch with again p capital
uh we set the page pitch color to be
grass the line color to be white
stripe to be true and so on okay and
these are different parameters that you
can enter and if you draw it you will
end up with this page so this is the
default one uh the stats bomb uh type
page uh which is provide provided by the
stats boom by package where uh the x and
like x axis ranges from zero to 120 and
the y axis ranges from 80 to zero
and but there are different uh types of
features that you can draw with uh just
as one by package uh except the default
one like apart from the default one you
can also set the color to be black and
this is what you will end up with then
the default one stats bomb by their
stats bomb there are different uh
other pictures like opta track cap skill
corner y scout metric sports ufr custom
and so on
okay
like this can be set using pitch type
argument inside the pitch function
here i have set the pitch type to be ufo
and here we see that x ranges from 0 to
slightly above 100 and y ranges from 0
to 70. these are the most basic concepts
covering the topic of drawing and
visualizing a football page using mpl
soccer and now we will use uh
the concepts from complex network
analysis to draw a pass network uh for a
particular team from a particular match
and then we will analyze that network
using networkx package for that we need
to keep install network x we import
network access nx
we also keep install seaborn
and import it at sns if you go back and
look into the events data set you will
notice that there is a column named
tactics that provides us with the team
lineups formations player ids and
they're just a number from both the
teams okay so the corresponding uh
row values for a column type gives us an
idea about whether it was the starting
element for formation or was a tactical
shift so let us generate a completely
new data set only focusing on the
tactics and the type columns
and we'll filter the data in such a way
that the tactics column has no rows set
to name okay so that's what we do here
so this is what it looks like here we
see
the entry uh of the tactics column
uh
is a
dictionary but the key formation
uh gives the formation of the players
the key lineup gives the
player ids names and jersey numbers
for both the team let's focus only on
the tactics for the starting eleven
setup from both the teams and uh like uh
this is given so this i got this idea
from uh
like from the mpl soccer
package uh documentation uh website and
also from the uh youtube video by mac
johns and like uh you should go and
check both these resources out
okay so we will build the analysis uh
build and analyze the past network
generated from among the starting 11
players from either of the tips
okay if you look into the first two rows
of the type column intact we see that
they are set as starting 11
one for each team so let us separately
face the data for the teams so that's
what we do here so it's as i said it was
a the tactics column is made up of a
python dictionary object and for now we
are only interested in the key lineup
okay
so we just extract the information from
the lineup
key we then convert that into a data
frame using
from dict from dict function of the
pandas data frame
we end up with this okay so we say that
the
for the
column player
suppose the id is 5597 and his name was
keller nevers and his position is a
goalkeeper and
his id is one and the jersey number is
also given so we are basically
interested in the players names and
their corresponding jersey numbers so we
will use a simple for loop and store the
information in separate dictionaries for
both the teams okay so that's what we do
here okay so now we have collected the
names and the jeffy numbers of the
players the starting eleven players
uh from both the team from the events
data set will extract out the relevant
columns for our past network analysis
and these are the relevant columns
minute second team type location
person location pass outcome and player
so this is the first 10 rows of the
events pn data frame so minute second
team
type location password location pass
outcome player next step is to filter
out the data set by teams and store them
as new data sets okay one for liverpool
and one for real madrid
that's what we do here as we are only
interested in the past uh network
generation we will filter the data sets
by keeping only those rows where type is
said to be pass and will dis discard all
the other types again view the first 10
rows of the filter data sets so we see
that for real madrid uh here the team is
only real madrid and like it was the
type is only pass
location
here if you notice the location is a
[Music]
like it's a list of two numbers which
gives the x and y coordinates and same
for pass and location okay so later we
will uh like separate this location
column into location x and location y uh
for the
one column for x coordinates and the
other column for y coordinates and same
for pass and location let us now uh very
carefully observe the datasets so
suppose from eventspn uh real dataset we
are focusing on the second and the third
row command which makes the pass at
around zeroth minute and tenth second
second row and danny carvajal receives
the pass at around 0th minute and 11
seconds so in both the data sets we now
need to add two extra columns named as
pass maker and pass receiver but the
pass maker column would be similar to
player column and the pass receiver
column would be the player column whose
index would be shifted by one place in
the negative direction what i learned uh
from the
like mpl soccer's uh documentation and
the youtube video by mike johns so go
take again go check them out
so this can be achieved by the shift
function provided by pandas so we will
perform the operation on both the uh
both events p and real and even spin uh
leave
okay
so that's what we do here so this is the
modified data set um so
we see that uh we have added
the pass maker uh column and the past
receiver column uh now there might be
passes which are which were not
successful okay so one trick like one
thing to notice is that in stats bomb
data passes whose pass outcome are set
as name are actually the successful
passes okay so this is very important
thing to keep in mind we'll again filter
the data sets by successful process
uh so these are the modified data sets
again
so it seems we have been able to
logically clean and modify the data sets
now we are only focused on building the
past network among the players who are
in the starting level
okay
so for that we need the time when the
first substitution took place there were
three substitution for real madrid and
the first substitution took place at
36th minute at 17 second
okay and same for liverpool there were
two substitutions and the first
substitution took place at 29 2013
second
so that's what we do here with this
sample code we like try to find out the
minute and second
so we use the minute
value to
like discard all those uh rows which
appeared after that
minute okay
all those events that occurred after
that uh time now from the data sets we
split the location and the parcel
location into two columns as i said one
for uh x coordinates and another for y
coordinates so inspired by the way given
here uh if you look click in this link
you will see uh in the apple circle uh
documentation page we will take the
average locations of the starting eleven
players uh on the field for for a
unified construction of the past network
so for that we will use the aggregate
function
and what it does is that uh we'll count
the number of passes created by this
player
okay so
here f lock real gives the average
location of the player of a particular
pillar from a particular team in that
match as i said using the aggregate
function gives the mean of
the x and y coordinates of for a
particular player and for case scenario
we see that the mean is
eight four five in the x coordinate and
thirty one point eight three six in the
y coordinate and he completed uh eleven
passes once we sort out the starting
eleven players average locations in a
game we'll try to figure out the number
of times a particular pass maker pass
the ball to a particular pass receiver
because just to keep the direction of
pass in mind that is a pass from player
a to player b is not equal identical to
the pass from player b to player a so it
would give a directed ah network so
we'll use group by and the count
function to count the number of rows
where a unique player a pass the ball to
another unique player b
so here we use the group by function and
here we just reset the index accordingly
and we see that uh here the pass maker
is kasamiro and the first receiver is
carvajal and kasamiro passed only once
in the entire game to uh danny carvajal
he passed six times to tony cruz so we
see that six plus seven eight nine ten
eleven so yeah there there were eleven
passes if you remember for uh december
that will merge the data sets have
looked real and pass real and let us
identify the left and right data frames
for uh performing the merge okay here
avlog real is the left data frame and
the pass real is the right
so we just merge them and uh
this is what the new data set looks like
so pass maker pass receiver the number
of passes uh given by the pass maker to
pass receiver
pass makers average x location
x coordinate pass makers average y
coordinate
and the total number of
passes completed by the pass maker so we
see that it's 11 for all the
cases rows where uh the pass maker is
casimiro
and same for liverpool
uh so finally uh we will again perform a
merge on these updated data sets for
adding the average locations of the past
receivers and the number of times the
receiver receive the ball okay
so
that's what we do
and uh
like
we added this column number of passes
received by that particular uh
player so casimiro i think received 24
passes then lastly we will replace the
players names with their jersey numbers
okay
and then we just visualize the pass
pass network for both the teams so we
use the pitch function to draw the page
and then we use arrows to draw the edges
from between the nodes
and then pitch dot scatter uh
so ps dot arrows function uh does that
and ps dot scatter function draws the
nodes the thicker the edge uh the number
of more the number of
passes were played so that's what it
actually uh means
and yeah so this is the
first network for real madrid against
liverpool
and this is for liverpool okay
and now as we have drawn the visualize
the past networks we can
perform some complex network analysis by
analyzing our networks using some
metrics
and
as uh let us first develop the
isomorphic graph uh to the ones that we
just saw uh and by using the network
package network x package and the thing
is uh we don't need the average
locations here okay uh we are only
interested on the
nodes and the information about the
nodes and the edges but not about the
locations what we do is that
we create this data set pass real new
from password
new where
the pass maker we only take the columns
pass maker pass receiver and number of
passes use the nx dot digraph function
because it's a directed graph so digraph
function from network x
and then we add the edge
along with the weight yes so this is
what it looks like okay now we will use
network x to find out the node degrees
from uh
past network of real madrid so
by using the nx dot degree function on g
real
and we see that these are the node
degrees
so node degrees means the number of
passes
the player was actually
involved in
so for real madrid we see uh that jersey
number eight
was involved in the maximum number of
passes we can create the in degrees and
output out degrees to so that means the
number of in degrees means the number of
passes a player received and out degrees
means the number of passes the player
gave to some other players we can also
generate adjacency matrix
and like we see the diagonal had uh all
zeros that means
there were no self loops so a player
cannot pass to himself now we can work
on a metric that focuses on geodesic
distance between two players nodes uh
two player nodes in a graph one way to
implement this is to divide one by the
weight column and that will give a a
new graph this is the
graph that we end up with
and
we'll perform the same operations to
create a modify modified graph for
liverpool 2
and now using this modified graph we can
calculate the all pair shortest paths
between the nodes players for both the
teams let us compute first for real
matrix if you intend to find the
shortest path between two players like
what would have been the shortest
path for a pass from one player to
another so suppose we want to calculate
the shortest path from the goalkeeper
killer nervous to the forward christian
ronaldo we'll just type the following so
print uh this real one to seven and we
see that from keller never sergio ramos
and from sargeras to
whoever the player
was with jesse number 12 to cristina
ronaldo i think uh
okay it was marcelo and with him
ultimately passing to uh christian and
ronaldo would have been the
uh
like
would have been the uh shortest path and
this seems like a good post match
analysis tool
i got this idea from discussing with uh
sarah babu and he is from iist india
now we can calculate the eccentricity uh
which is based on the shortest distance
so eccentricity of a player note p tells
us how far the furthest player note from
a p is positioned in the past network so
this is for all the players for for for
real madrid
and this is the average uh
eccentricity and for liverpool also
we calculate the average assembly speed
we can also calculate the average
clustering coefficient of a player
calculated it's 0.182 and for liverpool
it's 0.276
so the average clustering coefficient
lies in the range 0 and 1 where a value
of 0 denotes the fact that none of the
nodes are connected to each other and
value of 1 denotes the fact that the
network is a click that is each node is
connected to all the other nodes of the
network also can compute the centrality
of each player uh and
for each node in either team's first
network i understand which player was
the most important so sent more the
centrality of a particular player more
important he was in that match
uh so for real madrid it was casimiro
and for liverpool it was milner right so
next we'll talk about uh pass map and
the corresponding heat pumps so we will
again uh
from the same material again work on the
fetching the pass data visualize the
pass map
and
as we saw in so this is the events data
set
and
so we'll
like take the
column stream type minute location
password location pass outcome and
player
okay so here we will
focus on a particular player and let's
choose tony cruz because he is one of
the
best midfield builders of this uh era
from events pass
we only take the player whose name is
tonic rose and
this is what we end up with the data set
like the type column in events past p1
has event types other than passes but
we'll only set to pass
and that's what we do here and this is
the data set where the type is only pass
and
like this is what the
uh pass map looks for uh for tony close
so if the pitch function and like for
successful process we set the color
green and for the ancestor process we
for the unsuccessful process we set the
color red
and
for the heat map we use the seaboard
schedule plot
yeah again i got this idea from uh
like uh mickey jones tutorial and like
if you
want you can also calculate the
percentage of successful and
unsuccessful process for two nicoles and
we see that he had around 91 percent
successful passes so these are if you
want you can draw the uh frequency
distribution so now we will focus on
applying some computational geometric
concepts on
uh the
event and tracking data
so for that we'll visualize the convex
cells from players even data
so we'll study how to develop a convex
all around these points uh and from
where a player had made a pass or had
taken a shot in a particular game so
mathematically if these points are
contained in a set x then the convex l
is the smallest convex set that contains
x uh so this figure has been adapted
from this wikipedia article uh if you go
and you will see
it has a lot of information about convex
cells so we'll use uh the sci-fi package
uh
go download it
uh so as we have been doing till now let
us pick the important columns from the
events data set
like team location type and player
here we only need the location of the
players so it seems like we only need
our four columns for now uh
and we are focusing on pass and short
events okay so we set the type to be
either pass or short so that's what we
do here then we'll next split the data
into two data sets one for real media
and one for liverpool we will now
extract the even data for tony cruz uh
from events all real
uh so we set the player to be tony cross
and this can be applied to all the other
players
and before computing and visualizing the
past
visualizing the con convex cell it is
good practice to discard the outliers
okay from the data sets
so we use the interquartile range
and we'll find the interquartile ranges
for the columns location x location i
from the event cell tony and then
compute the upper and lower bounds for
the data any points lying beyond these
bounds will be considered as
outliers and will be discarded okay
so
this is what we uh do here this is the
box plot for uh tony cruz's uh location
conditions
and we see these are the so these are
the whisker plots
and for location x these are the
outliers and for location why we don't
have
those outliers but yeah this
uh
we can discard the outliers uh
and next let us look into the eventual
tony data set
and first we'll collect all the points
from the two columns as a 2d matrix
this uh comes in eight while drawing the
convexal
so poinsol equals to even solve only
location x location y dot values give
allows us to do that now let us use the
convex hull function from scipy dot
special uh
package
and
we apply convex along this
location action location
y for events hall tony
and then we just collect the useful
information and we visualize the context
so this is the convexel of tonic uses
field coverage
now we can draw the convex for other
players too from either of the two and
uh next what we do is that we uh draw
the uh
we learn how to get the tracking data
for a particular match uh
uh like for each for
for some particular instances of a match
and then draw the corresponding
delineate triangulations and voronoi
diagrams
so we'll try to understand how to get
the tracking data from particular game
using stratform api so again it's given
in their uh
documentation please go and follow that
if you want
so we need to import useful classes from
the mpl soccer dot strasbourn module and
as you remember we use the match id18245
and
uh from mpl soccer.transform we import
read event event
the code is given here this is the
events and tracking look at the event
and tracking data sets so if we look
closely into the tracking data set we
understand that the column id represents
an uh unique id for a short freeze frame
that is a particular instance of a match
now we'll only extract the relevant
columns id player name the locations x
and y and the team
uh now let us try collecting the jersey
numbers uh we you will use a different
and easier approach from the one we have
done here the blog that i have written
so
uh yeah but
we can follow this to get the plane
information use the following command by
passing the match id so player info goes
to sb dot lineups match ad equals to the
one that we have been using
here is the data set we see that it has
just a number that country the player
belongs to player nickname player name
and player right now let us select a
particular id from taking data set
representing an instance of a particular
game
and we will filter tracking by a id
value um
which will give us the information of
the locations of the players on the page
at that moment
uh so these are all the unique ids for a
particular snap from a game
let us filter the data set now so we use
this short id to be this we just select
a particular value from the id column
uh and then tracking filter because the
tracking tracking id where the id is the
short id
and same for event and uh the data
filtered data set looks like this
okay
so we will now compute the dilute
triangulations from our team's player
locations to get an idea about the
possible links created among the
teammates
so delay right triangulations help us
understand the links
uh between teammates of a particular
team go look into the wikipedia article
from where it took this
diagram uh to understand more about the
illinois triangulations
so we will import that illinois uh
class from
sci-fi dot special
we visualize the triangulations and the
player positions of the instance on that
page
and this is the code to do that uh go
check it out and yeah so this is the
dilenoid triangulation for real madrids
so i've linked to the real madrid
players
and the red nodes indicate the locations
of liverpool's players and the white
notes indicate the real matrix and the
black lines indicate the direct links
between the players one particular team
at a particular moment forming that
illinois triangulations so the book
soccer matrix uh in his book soccer
matrix by dr sumter he mentions that
these lines have two useful indications
first they portray the availability of a
player
uh availability of passes among the
players from a particular team and
second they also indicate the no man's
lands for players from the opposition
team meaning that if an player if an
opposition player is on one of these
linking lines
then they are at the disadvantage
uh so
and we can also draw the voronoi
diagrams uh again look into the
wikipedia article to learn more about
boronics
we use uh cypher specials boronoid and
voronoi plot 2d classes to draw the
veronica diagrams
and this is the
voronoi diagram
and the voronoi diagrams give us the
zones of each and every player
on the pitch at a particular moment by
breaking the pitch into distinct regions
belonging to the players indicating the
field coverage of each player at that
moment
so yes this completes our section and i
think that's it for my presentation so
these are the references go check them
out
at the end thank you wear masks get
vaccinated and stay safe
thanks
you