This week, we will be using an open dataset from the popular site Kaggle. This European Soccer Database has more than 25,000 matches and more than 10,000 players for European professional soccer seasons from 2008 to 2016.
Although we won’t be getting into the details of it for our example, the dataset even has attributes on weekly game updates, team line up, and detailed match events. The goal of this notebook is to walk you through an end to end process of analyzing a dataset and introduce you to what we will be covering in this course. Our simple analytical process will include some steps for exploring and cleaning our dataset, some steps for predicting player performance using basic statistics, and some steps for grouping similar clusters using machine learning. Let's get started with our Python journey! |
To get started, we will need to:
We will start by importing the Python libraries we will be using in this analysis. These libraries include:
In [15]:
import sqlite3
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.preprocessing import scale
from customplot import *
In [16]:
# Create your connection.
cnx = sqlite3.connect('database.sqlite')
df = pd.read_sql_query("SELECT * FROM Player_Attributes", cnx)
We will start our data exploration by generating simple statistics of the data.
Let us look at what the data columns are using a pandas attribute called "columns".
In [17]:
df.columns
Out[17]:
Next will display simple statistics of our dataset. You need to run each cell to make sure you see the outputs.
In [18]:
df.describe().transpose()
Out[18]:
In [19]:
#is any row NULL ?
df.isnull().any().any(), df.shape
Out[19]:
Now let's try to find how many data points in each column are null.
In [20]:
df.isnull().sum(axis=0)
Out[20]:
In [21]:
# Fix it
# Take initial # of rows
rows = df.shape[0]
# Drop the NULL rows
df = df.dropna()
Now if we check the null values and number of rows, we will see that there are no null values and number of rows decreased accordingly.
In [22]:
#Check if all NULLS are gone ?
print(rows)
df.isnull().any().any(), df.shape
Out[22]:
To find exactly how many lines we removed, we need to subtract the current number of rows in our data frame from the original number of rows.
In [23]:
#How many rows with NULL values?
rows - df.shape[0]
Out[23]:
Our data table has many lines as you have seen. We can only look at few lines at once. Instead of looking at same top 10 lines every time, we shuffle - so we get to see different random sample on top. This way, we make sure the data is not in any particular order when we try sampling from it (like taking top or bottom few rows) by randomly shuffling the rows.
In [24]:
#Shuffle the rows of df so we get a distributed sample when we display top few rows
df = df.reindex(np.random.permutation(df.index))
In [25]:
df.head(5)
Out[25]:
Most of the time, we are only interested in plotting some columns. In that case, we can use the pandas column selection option as follows. Please ignore the first column in the output of the one line code below. It is the unique identifier that acts as an index for the data.
Note: From this point on, we will start referring to the columns as "features" in our description.
In [27]:
df[:10][['penalties', 'overall_rating']]
Out[27]:
We see that Pearson's Correlation Coefficient for these two columns is 0.39.
Pearson goes from -1 to +1. A value of 0 would have told there is no correlation, so we shouldn’t bother looking at that attribute. A value of 0.39 shows some correlation, although it could be stronger.
At least, we have these attributes which are slightly correlated. This gives us hope that we might be able to build a meaningful predictor using these ‘weakly’ correlated features.
Next, we will create a list of features that we would like to iterate the same operation on.
In [28]:
potentialFeatures = ['acceleration', 'curve', 'free_kick_accuracy', 'ball_control', 'shot_power', 'stamina']
The for loop below prints out the correlation coefficient of "overall_rating" of a player with each feature we added to the list as potential.
In [29]:
# check how the features are correlated with the overall ratings
for f in potentialFeatures:
related = df['overall_rating'].corr(df[f])
print("%s: %f" % (f,related))
In [30]:
cols = ['potential', 'crossing', 'finishing', 'heading_accuracy',
'short_passing', 'volleys', 'dribbling', 'curve', 'free_kick_accuracy',
'long_passing', 'ball_control', 'acceleration', 'sprint_speed',
'agility', 'reactions', 'balance', 'shot_power', 'jumping', 'stamina',
'strength', 'long_shots', 'aggression', 'interceptions', 'positioning',
'vision', 'penalties', 'marking', 'standing_tackle', 'sliding_tackle',
'gk_diving', 'gk_handling', 'gk_kicking', 'gk_positioning',
'gk_reflexes']
In [31]:
# create a list containing Pearson's correlation between 'overall_rating' with each column in cols
correlations = [ df['overall_rating'].corr(df[f]) for f in cols ]
In [32]:
len(cols), len(correlations)
Out[32]:
We make sure that the number of selected features and the correlations calculated are the same, e.g., both 34 in this case. Next couple of cells show some lines of code that use pandas plaotting functions to create a 2D graph of these correlation vealues and column names.
In [33]:
# create a function for plotting a dataframe with string columns and numeric values
def plot_dataframe(df, y_label):
color='coral'
fig = plt.gcf()
fig.set_size_inches(20, 12)
plt.ylabel(y_label)
ax = df.correlation.plot(linewidth=3.3, color=color)
ax.set_xticks(df.index)
ax.set_xticklabels(df.attributes, rotation=75); #Notice the ; (remove it and see what happens !)
plt.show()
In [34]:
# create a dataframe using cols and correlations
df2 = pd.DataFrame({'attributes': cols, 'correlation': correlations})
In [35]:
# let's plot above dataframe using the function we created
plot_dataframe(df2, 'Player\'s Overall Rating')
Now it is time for you to analyze what we plotted. Suppose you have to predict a player's overall rating. Which 5 player attributes would you ask for?
Hint: Which are the five features with highest correlation coefficients?
Until now, we used basic statistics and correlation coefficients to start forming an opinion, but can we do better? What if we took some features and start looking at each player using those features? Can we group similar players based on these features? Let's see how we can do this.
Note: Generally, someone with domain knowledge needs to define which features. We could have also selected some of the features with highest correlation with overall_rating. However, it does not guarantee best outcome always as we are not sure if the top five features are independent. For example, if 4 of the 5 features depend on the remaining 1 feature, taking all 5 does not give new information.
In [36]:
# Define the features you want to use for grouping players
select5features = ['gk_kicking', 'potential', 'marking', 'interceptions', 'standing_tackle']
select5features
Out[36]:
In [37]:
# Generate a new dataframe by selecting the features you just defined
df_select = df[select5features].copy(deep=True)
In [38]:
df_select.head()
Out[38]:
Now we will use a machine learning method called KMeans to cluster the values (i.e., player features on gk_kicking, potential, marking, interceptions, and standing_tackle). We will ask for four clusters. We will talk about KMeans clustering and other machine learning tools in Python in Week 7 so we won't discuss these methods here.
In [39]:
# Perform scaling on the dataframe containing the features
data = scale(df_select)
# Define number of clusters
noOfClusters = 4
# Train a model
model = KMeans(init='k-means++', n_clusters=noOfClusters, n_init=20).fit(data)
In [40]:
print(90*'_')
print("\nCount of players in each cluster")
print(90*'_')
pd.value_counts(model.labels_, sort=False)
Out[40]:
In [41]:
# Create a composite dataframe for plotting
# ... Use custom function declared in customplot.py (which we imported at the beginning of this notebook)
P = pd_centers(featuresUsed=select5features, centers=model.cluster_centers_)
P
Out[41]:
In [42]:
# For plotting the graph inside the notebook itself, we use the following command
%matplotlib inline
In [43]:
parallel_plot(P)