Exploring ‘Game Of Thrones’ Battles and Predicting the fate of the characters using Machine Learning Techniques .
By: Omkar Sunthankar
Tags: Game of Thrones, Machine Learning, Random Forest Classifiers, Exploratory Data Analysis, Seaborn

In this post we will do some Exploratory Data Analysis ( EDA ) related to different battles fought in the mystical Westros and we will go one bit further to predict fate of characters using the different features available . Lets get started “Valar Morghulis” .

Data

The datasets are available on Kaggle the two datasets we are concerned with are

battles.csv character_prediction.csv

1. Exploratory Data Analysis ( EDA )

EDA is a statistical technique used to understand the inherent characteristics of the data . It mainly uses Visual Methods to achieve the task . We will be using Python Libraries viz Pandas , Matplotlib , Seaborn in this post .

Lets start with importing the libraries

The next step involves reading files

Let us look at the data , We will do that using .head() function . By default it will display 5 rows .

It is important to understand your data . We can do that by following functions info() provides information regarding the data type and number of null values present

.describe() provides information useful information regarding count , min ,max ,std deviation ,mean etc .

Visualisation

Let us start exploring data using visualization techniques . The library used here is Seaborn which is a extension of MatplotLib .

Few Questions we can look answers for

a. Which kings has fought the most battles ?

b. Which kings have won most battles , what is their win percentage ?

We can see that Joffrey Baratheon/ Tommen Baratheon has fought most numbers of battles , where as Stannis Baratheon has been involved in least battles . Every King has good win percentage except stannis who has won only 40 % of his battles .

c. Who is good at defending their territories , and their percentage ?

Seems that Mance rayder has the best track record of defending his territory .

d. How much does size of army matter to final outcome ? How much does a certain terrain contribute to victory or loss ?

There is no visual evidence of correlation between size of the army and victory in a battle.

e. Which types of battlefield have favoured the attackers ?

Looks like Ambush has helped everyone , win percentage for ambush is 100 .

f. Which commander is good at marshalling his troops and has a good track record ?

g. Let’s now look at commanders who have done better defending job .

Looks like Ser Gregor Clegane and Tywin Lannister have impeccable record of defending their territories

h. Which area in westrosi history has witnessed most battles ?

Number of battles vs Regions

2. Machine Learning Model

Let’s now move to interesting part of Machine learning i.e building a Machine learning model .

We will join dataset_death and dataset_character and remove unwanted columns

Lets now fill missing values and transform some categorical values to numerical values

Let’s now look features are contributing most to the target variable i.e “actual”. For this we would use Extremely Randomized Trees Classifier which is a variation Random Forest Algorithm which comes bundled in Sklearn Ensemble Package .

The bar chart shows that Popularity is very important and heavily influences the Survival , followed by house , Allegiance , culture . Lets drop less influential features .

Let’s make sure that our dataset is balanced , an imbalanced dataset might lead poor recall values .

Looks like the dataset is imbalanced . To make sure we get good results , we need to balance the dataset . As our death count less 50% of that of living count . Which could lead to bias and impact our recall values .there are many dataset balancing techniques , in this problem we will use Synthetic minority oversampling technique aka SMOTE .

The dataset X_sm , y_sm looks quite balanced .

Important Note : It is very important to split the data into training and testing before applying oversampling algorithm . If you use the entire dataset , it could lead to leakage of synthetic data to testing data , giving incorrect testing accuracy .

Let’s use this new dataset and try to fit it to a machine learning model . For this task we will use Random Forest Classifier.

Let’s look at the roc curve of our model .

ROC curve

Conclusion

We performed some Exploratory Data Analysis on the game of thrones dataset We created a RandomForest Classifier to classify a character to be dead or alive .

Links

Dataset: https://www.kaggle.com/mylesoneill/game-of-thrones

References

If you liked the blog please show support by clapping . If you have any doubts please feel free to comment below .