Predictive Analysis of an IPL Match | by Geet Pithadia | Towards Data Science

Since the dawn of the IPL in 2008, it has attracted viewers all around the globe. High level of uncertainty and last moment nail biters has urged fans to watch the matches. Within a short period, IPL has become the highest revenue generating league of cricket. Data Analytics has been a part of sports entertainment for a long time. In a cricket match, we might have seen the scoreline showing the probability of the team winning based on the current match situation. This, my friend, is Data Analytics in action!
Being a cricket fan, visualizing the statistics of cricket is mesmerizing. While I was jumping blogs on Medium and kernels (Let’s say code playbook) on Kaggle, I was fascinated by the analysis done. Hence, I decided to get my first hands-on experience by building a classifier to predict the winning team.
In Machine Learning, the problems are categorized into 2 groups mainly: Regression Problem and Classification problem. The Regression problem deals with the kind of problems having continuous values as output while in the Classification problem the outputs are categorical values. Since the output of winner prediction is a categorical value, the problem which we are trying to solve is a Classification problem.
So where to start, and what to do?
Understand the dataset.Clean the data.Analyze the candidate columns to be Features.Process the features as required by the model/algorithm.Train the model/algorithm on training data.Test the model/algorithm on testing data.Tune the model/algorithm for higher accuracy.
Understand the dataset.
Clean the data.
Analyze the candidate columns to be Features.
Process the features as required by the model/algorithm.
Train the model/algorithm on training data.
Test the model/algorithm on testing data.
Tune the model/algorithm for higher accuracy.
Let’s get started!
Well, for smooth learning I recommend having a glance at Python libraries that I used like Pandas, Numpy, and Scikit-learn.
While dealing with data, Kaggle: Your Home for Data Science is the to-go platform. I used the dataset https://www.kaggle.com/nowke9/ipldata. The dataset has 2 files: matches.csv having every match detail from 2008 to 2019 and deliveries.csv having ball by ball detail for every match.
I am using .read_csv() to read the CSV file. The path in the .read_csv() function can be relative or absolute. Here, I am using Google Colaboratory as my playground and hence the file is stored there.
Nothing is perfect. Life is messy. Relationships are complex. Outcomes are uncertain. People are irrational. — Hugh Mackay
As rightly said by Hugh Mackay “Nothing is perfect” in the above quote and in this case, it relates to our data. The data that we have, contains null values in several columns.
column null_valuescity      7 winner    4 player_of_match 4 umpire1   2 umpire2   2 umpire3  637 dtype: int64
There are several ways to handle the null values and among them, I will be using Imputation on column city. Imputation is a way to fill the missing values statistically. For more reference, you might find https://www.kaggle.com/alexisbcook/missing-values helpful. Since filling the values in the column winner is illogical, we can drop those records.
Combination of .select() and .where() from numpy replaces the values in the columns city based on the column venue if the values in city are null.
While playing around with the data, I found an interesting redundancy. Team Rising Pune SuperGiants were duplicated in columns team_1, team_2, winner, and toss_winner.
Replacing these values with one value is the obvious thing to do next. Python libraries make data manipulation an easy task to perform. Pandas allow us to load the data into DataFrame and perform manipulations on it easily and efficiently.
Phew!! The data is clean now. We can finally start on analyzing the features (columns).
Note: The columns taken into consideration are: team_1, team_2, toss_winner, toss_decision, venue, city and winner.
For the columns to be able to assist the model in the prediction, the values should make some sense to the computers. Since they (still) don’t have the ability to understand and draw inference from the text, we need to encode the strings to numeric categorical values. While we may choose to do the process manually, the Scikit-learn library gives us an option to use LabelEncoder.
Before we hop on to building models, an important observation has to be acknowledged. Columns like toss_winner, toss_decision, and winner might make sense to us, but what about the machines?
Let me elaborate. The values in toss_winner and winner include team names, but what is the relation of these variables with team_1 or team_2? The only thing common between them is that they would share the same value, but that is not enough to be logical. Also, toss_decision might be bat or field, but what team are they referring to? To tackle this problem, we will add new columns team1_win, team1_toss_win, and team1_bat for columns winner, toss_winner, and toss_decision such that they reflect the relationship with column team_1.
The value in the column team1_win will be 1 if team1 wins else 0, for column team1_toss_decision it will be 1 if team1 wins the toss and finally team1_bat will be 1 if team1 is batting first.
We manually selected a bunch of features based on the domain knowledge we had. But we have no statistical proof of the selected features being important for our dataset. Scikit-learn provides an excellent module named feature_selection which gives us a couple of ways to do the feature selection. First, we will check the columns if any of them represent the same values as other columns. For this, we need to create a correlation matrix to find out the relationships between the column. If the absolute value of the correlation between the columns is high enough, we can say that they represent similar values.
Correlation matrix for our data would look something like this:
Here, we see that team1_bat represents the same information as team1_toss_win. Strange, right? It’s just how the dataset was built, if team1 wins the toss then they will always bat and if team2 wins the toss then they will always field. So we removed the column team1_bat from our list of features.
For a Classification problem, multiple algorithms can train the classifier according to the data we have and using the pattern, predict the outcomes of certain input conditions. We will try DecisionTreeClassifier, RandomForestClassifier, LogisticRegression, and SVM and choose the algorithm best suited for our data distribution.
Once we build the model, we need to validate that model using values that are never exposed to the model. Hence we split our data using train_test_split, a class provided by Scikit-learn into 2 parts having a distribution of 80–20. The model is trained on 80% of data and validated against the other 20% of the data.
We can now train various models and compare the performances.
This resulted in:
It is evident from the results that SVM gives us a higher accuracy of 66.23% than other algorithms for this data distribution.
Even though the accuracy is not high enough to be useful, it gives a basic idea about the strategies and methodologies used in designing a solution to the Machine Learning problem.
There are many other factors affecting the outcome of a match like weather, the form of a player, home ground advantage, etc which are not included here. Try adding these features and playing around with it. A further modification to the code can be done by using the concept of K-Fold cross-validation that is an alternative to train_test_split. Check out an amazing article in the link for K-Fold.
Since you have made it till here, we did a decent job in predicting the probability of an IPL team to win by converting it to a Binary classification problem and using Python libraries while learning about them. If you have any questions, suggestions or feedback, feel free to reach out to me on Email or LinkedIn. You can find this code on my GitHub and you are more than welcome to contribute.
Happy Predicting!
Thanks to Dhrumil Patel, Syed Zubair, Sahil Sangani, and Yash Shah for the help!