Implementing The K-Means Algorithm to Predict Passenger Survival Status
Please see my Github Repo for code detail.
To have a better understanding of the incident of Titanic's sinking, K-means algorithm is implemented using Scikit-learn on the Titanic dataset to try to predict if a passenger survived or not.
The sinking of the RMS Titanic is a well-known shipwreck that occurred during its maiden voyage on April 15, 1912. Despite being deemed "unsinkable," the ship hit an iceberg and sank, leading to the tragic loss of 1502 out of 2224 passengers and crew members. Due to the shortage of lifeboats, survival largely depended on luck, although certain groups of people appeared to have had a better chance of surviving than others:
- PassengerId: Unique Id of passenger
- Survived: If the passenger survived (0-No, 1-Yes)
- Pclass: Passenger Class (1=1st, 2=2nd, 3=3rd)
- Name: Name of the passenger
- Sex: Male/Female
- SibSp: Number of siblings/spouses aboard
- Parch: Number of parents/children aboard
- Ticket: Ticket Number
- Fare: Passenger Fare
- Cabin: Passenger Fare
- Embarked: Port of Embarkation (C=Cherbourg, Q=Queenstown, S=Southampton)
- Load data from provided on Kaggle
- Explore data set
- Data cleaning
- Impute missing data
- Encode categorical feature to numerical feature
- Scaling feature
- Determine number of clusters
- Model fitting
4. Data Exploration
- Data preview:
- Description of numerical fields:
- Missing values by field:
5. Data Cleaning
- Empty values are imputed with the field's average value
- Certain fields ("Name", "Ticket", "Cabin", "Embarked" columns) are dropped since they are irrelevant to the model
- Categorical field encoded to be numeric ("Sex" column)
- To consider all features equally, the values for all features must be transformed into the same scale of measurement.
- To attempt to choose the right number of clusters, the SSE is calculated to plot the elbow plot.
- No obvious "bends" are visible in the elbow plot:
- A cluster number of 3 is chosen arbitrarily to continue building the model.
- Model evaluation is carried out by calculating the percentage of correctly predicted survival outcome of passengers.
- Two attempts to improve the model are made:
- MinMaxScaler: to scale each feature to a given range in the training and testing data sets.
- Utilizing additional parameters in KMeans: to explore additional functionality of the KMeans module for better model performance.