Implementing The K-Means Algorithm to Predict Passenger Survival Status
Please see my Github Repo for code detail.
The name Titanic has become synonymous with disaster as a result of the "unsinkable" ship having struck an iceberg that tore through it. The ship ended up sinkingΒ on its maiden voyage on April 15, 1912.Β Due to the shortage of lifeboats, 1502 out of 2224 passengers and crew members lost their lives..
While the rule of "women and children first" was one of the factors involved in who would be more likely to get a place on the lifeboat, there were other considerations. Β 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.
Β Data
Certain groups of people appeared to have had a better chance of surviving than others depending on which category they fell into. To examine that, we look at these data points:
- 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)
3. Process
- Load data from provided on Kaggle
- Explore data set
- Data cleaning
- Impute missing data
- Encode categorical feature to numerical feature
- Modeling
- Scaling feature
- Determine number of clusters
- Model fitting
- Evaluation
Β Data Exploration
- Data preview:
- Description of numerical fields:
- Missing values by field:
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)
Β Modeling
- 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.
Β Conclusion
Β References
- Leonardo Yip. unsplash. 2018. https://unsplash.com/photos/n7GPvDOZUB8
- Kaggle Inc. βTitanic - Machine Learning from Disaster.β Kaggle. 2022. https://www.kaggle.com/c/titanic.