- Objective
- Dataset Description
- Data Preprocessing
- Model Building
- Data Visualisations
- Best Model - Random Forest Model (with Hyperparameter Tuning)
- Conclusion
- Final Thoughts
The objective of this project was to build a predictive model using the Titanic dataset to determine whether a passenger on the Titanic survived or not. This dataset is a common starting point for data science and machine learning projects due to its simplicity and the availability of relevant features.
The Titanic dataset contains information about individual passengers, including the following features:
- Pclass: Passenger class (1 = 1st, 2 = 2nd, 3 = 3rd)
- Sex: Gender of the passenger
- Age: Age of the passenger
- *SibSp#: Number of siblings or spouses aboard the Titanic
- Parch: Number of parents or children aboard the Titanic
- Ticket: Ticket number
- Fare: Passenger fare
- Cabin: Cabin number
- Embarked: Port of embarkation (C = Cherbourg; Q = Queenstown; S = Southampton)
- Survived: Survival status (0 = No, 1 = Yes) [Target variable]
Before building the models, the following preprocessing steps were undertaken:
-
Handling Missing Values: Missing values in the
Age
,Cabin
, andEmbarked
columns were addressed.Age
was imputed using the median age.Cabin
information was dropped due to a large number of missing values.- Missing values in
Embarked
were filled with the most common port (S
).
-
Feature Encoding: Categorical variables (
Sex
,Embarked
) were converted into numerical values using one-hot encoding. -
Feature Scaling: Continuous variables (
Age
,Fare
) were standardized to have a mean of 0 and a standard deviation of 1.
Two machine learning models were trained and evaluated: Logistic Regression and Random Forest. Additionally, Randomized Search Cross-Validation was used to tune the hyperparameters of the Random Forest model.
- Accuracy: 0.80
- Precision, Recall, and F1-Score:
![Screenshot 2024-06-14 at 13 44 23](https://private-user-images.githubusercontent.com/100778149/339753687-b1d6fc55-96bf-4192-b23f-54afeca86f89.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNjA4MTcsIm5iZiI6MTcyNDA2MDUxNywicGF0aCI6Ii8xMDA3NzgxNDkvMzM5NzUzNjg3LWIxZDZmYzU1LTk2YmYtNDE5Mi1iMjNmLTU0YWZlY2E4NmY4OS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwOTQxNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1hYTViYWE5ZWRhMGJhYmVlNjczNzUzYmFjNzY1YmY1ZjBjNTE5ODViYmYxMWY5MjE2Yjk2YjE1MmYxYTNhYTVjJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.FmCWNzz48ey5EAs0LPh4tAjgVcLgXDKlamBm5MqV1vk)
- Accuracy: 0.82
- Precision, Recall, and F1-Score:
![RandomForestOutput](https://private-user-images.githubusercontent.com/100778149/339753202-7e3619ec-476c-4279-8c75-81334a850d19.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNjA4MTcsIm5iZiI6MTcyNDA2MDUxNywicGF0aCI6Ii8xMDA3NzgxNDkvMzM5NzUzMjAyLTdlMzYxOWVjLTQ3NmMtNDI3OS04Yzc1LTgxMzM0YTg1MGQxOS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwOTQxNTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xMWUyNWI1ZDg5ZjkwODkzM2UwMWY4ZGE3NmY1ZGZiOTI0YzE4OWI2MzQxNWYwZTAyMDQ2OGZkNGUxYTVhZDI4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.8qc1IUlbumRTyCWr-mhtPvnejIhWo9AT9yD5uHQqzjg)
### Best Model - Random Forest Model (with Hyperparameter Tuning)
- Accuracy: 0.82
- Precision, Recall, and F1-Score:
## Model Conclusion
The Random Forest model with hyperparameter tuning performed slightly better than the Logistic Regression model, achieving an accuracy of 82%. The precision, recall, and F1-score indicate that the model is reasonably good at predicting survival on the Titanic, with a higher precision for predicting non-survival (class 0) and a balanced performance for survival (class 1).
The predictive models built using the Titanic dataset offer valuable insights into the factors that influenced survival on the Titanic. Analysis of the dataset revealed the following key points about survival:
-
Gender: Women had a significantly higher survival rate compared to men. This is reflected in the model's feature importance, where gender (Sex) was one of the most influential factors.
-
Passenger Class: Passengers in first class (Pclass = 1) had a higher survival rate compared to those in second and third classes. This indicates that socio-economic status played a crucial role in survival chances.
-
Age: Younger passengers had a better chance of survival compared to older passengers. Children, in particular, had higher survival rates.
-
Family Size: Passengers with fewer family members aboard (SibSp and Parch) tended to survive more often than those with larger families.
-
Embarkation Point: Passengers who embarked from Cherbourg (Embarked = C) had a slightly higher survival rate compared to those who boarded at Queenstown or Southampton.
In conclusion, the model successfully identifies the key determinants of survival, emphasizing the importance of gender, socio-economic status, age, and family size. These findings align with historical accounts and provide a comprehensive understanding of the factors that influenced survival during the Titanic disaster. The models developed in this project serve as an effective tool for predicting survival and demonstrate the potential of machine learning in analyzing historical datasets.