Titanic Survival Prediction Using Machine Learning
1. Introduction
Titanic Survival Prediction is one of the most famous and beginner-friendly Machine Learning projects. The main objective of this project is to predict whether a passenger would survive or not survive the Titanic disaster based on information about the passenger and their travel details.
The RMS Titanic was a passenger ship that sank in 1912 after hitting an iceberg. The historical passenger data contains information such as passenger class, age, gender, and fare paid. Machine Learning can be used to analyze these factors and identify patterns associated with passenger survival.
Some of the important factors that can influence the prediction include:
Passenger Class (Pclass)
Age
Gender
Fare Paid
This is a Supervised Machine Learning problem because the training data contains passenger information along with the known survival status.
The target variable has two possible categories:
1 – Survived
0 – Did Not Survive
Therefore, this is a Binary Classification problem. In this project, Logistic Regression is used because it is a simple and effective classification algorithm for predicting two possible outcomes.
2. Objective
The main objective of this project is to build a Machine Learning model that learns patterns from Titanic passenger data and predicts whether a new passenger is likely to survive.
The project demonstrates important Machine Learning concepts such as:
Creating a dataset
Selecting input features
Encoding categorical information
Selecting a target variable
Binary classification
Logistic Regression
Model training
Prediction
Interpreting classification results
3. Features Used
The model uses four passenger-related features.
Passenger Class
Pclass represents the passenger's travel class:
1 – First Class
2 – Second Class
3 – Third Class
Passenger class is included because passengers from different classes may have had different access to resources and evacuation facilities.
Age
Age represents the passenger's age in years. Age can be an important feature when analyzing survival patterns.
Gender
Gender is represented numerically in this dataset:
0 – Female
1 – Male
This numerical representation allows the Machine Learning algorithm to process gender as an input feature.
Fare
Fare represents the amount paid for the passenger's ticket. It can also provide information related to passenger class and travel conditions.
The target variable is:
Survived – Indicates whether the passenger survived the disaster.
4. Technologies Used
The following technologies are used:
Python – Main programming language.
Pandas – Used to create and manage the Titanic dataset.
Scikit-learn – Used to implement the Machine Learning model.
Logistic Regression – Used for binary classification.
5. Dataset
The project uses a small sample dataset containing eight Titanic passengers.
Each record contains:
Passenger class
Age
Gender
Fare
Survival status
The survival status is represented as:
ValueMeaning0Did Not Survive1Survived
Dataset
Pclass Age Gender Fare Survived
0 1 25 0 80 1
1 3 38 1 15 0
2 2 30 1 30 1
3 1 22 0 100 1
4 3 45 1 8 0
5 2 28 0 25 1
6 1 19 0 120 1
7 3 35 1 10 0
The model learns patterns from these historical passenger records.
6. Working Principle
The Titanic Survival Prediction system follows a basic Machine Learning classification workflow.
First, a sample dataset containing passenger information and survival labels is created using Pandas.
The four input features—Pclass, Age, Gender, and Fare—are selected and stored in X.
The Survived column is selected as the target variable and stored in y.
A Logistic Regression model is then created using Scikit-learn. The model is trained using the sample passenger data.
During training, the model analyzes the relationship between passenger characteristics and the known survival outcomes.
After training, information about a new passenger is provided to the model. The model then predicts whether the passenger belongs to the Survived or Did Not Survive class.
7. Python Implementation
The following Python program implements the Titanic Survival Prediction system.
# Import required libraries
import pandas as pd
from sklearn.linear_model import LogisticRegression
# --------------------------------------------
# Step 1: Create the dataset
# --------------------------------------------
# Sample Titanic passenger data
data = {
"Pclass": [1, 3, 2, 1, 3, 2, 1, 3],
"Age": [25, 38, 30, 22, 45, 28, 19, 35],
"Gender": [0, 1, 1, 0, 1, 0, 0, 1],
"Fare": [80, 15, 30, 100, 8, 25, 120, 10],
"Survived": [1, 0, 1, 1, 0, 1, 1, 0]
}
# Convert dictionary into DataFrame
df = pd.DataFrame(data)
# Display dataset
print("Dataset:")
print(df)
# --------------------------------------------
# Step 2: Select input features (X)
# --------------------------------------------
X = df[
[
"Pclass",
"Age",
"Gender",
"Fare"
]
]
# --------------------------------------------
# Step 3: Select target variable (y)
# --------------------------------------------
y = df["Survived"]
# --------------------------------------------
# Step 4: Create the Logistic Regression model
# --------------------------------------------
model = LogisticRegression()
# --------------------------------------------
# Step 5: Train the model
# --------------------------------------------
model.fit(X, y)
# --------------------------------------------
# Step 6: Predict passenger survival
# --------------------------------------------
# Passenger details:
# Class = 2
# Age = 27
# Gender = Female (0)
# Fare = 40
new_passenger = [[2, 27, 0, 40]]
prediction = model.predict(new_passenger)
# --------------------------------------------
# Step 7: Display prediction
# --------------------------------------------
if prediction[0] == 1:
print("\nPrediction: Survived")
else:
print("\nPrediction: Did Not Survive")
8. Explanation of the Python Program
The program starts by importing Pandas and Logistic Regression from Scikit-learn.
Pandas is used to create the sample Titanic passenger dataset, while Logistic Regression is used to classify passengers into two possible survival categories.
Step 1: Create the Dataset
A Python dictionary is created containing information about eight sample passengers.
For example:
Pclass = 1
Age = 25
Gender = 0
Fare = 80
Survived = 1
The dictionary is converted into a Pandas DataFrame.
Step 2: Select Input Features
The four passenger-related features are selected:
X = df[
[
"Pclass",
"Age",
"Gender",
"Fare"
]
]
These features are used by the model to predict survival.
Step 3: Select Target Variable
The Survived column is selected as the target:
y = df["Survived"]
The values represent:
0 = Did Not Survive
1 = Survived
Step 4: Create the Model
A Logistic Regression model is created:
model = LogisticRegression()
Logistic Regression is suitable because this is a binary classification problem.
Step 5: Train the Model
The model is trained using:
model.fit(X, y)
During training, the model learns patterns between passenger information and survival outcomes.
Step 6: Predict a New Passenger
The new passenger has the following details:
Passenger Class = 2
Age = 27
Gender = Female
Fare = 40
Since the dataset represents Female as 0, the input is:
new_passenger = [[2, 27, 0, 40]]
The trained model then predicts the passenger's survival status.
9. Prediction
Input
Passenger Class = 2
Age = 27
Gender = Female
Fare = 40
Output
Prediction: Survived
Based on the patterns learned from the sample dataset, the model predicts that the new passenger Survived.
This is a Machine Learning prediction based on the provided sample data and should not be interpreted as a historical certainty about an individual passenger.
10. How Logistic Regression Works
Logistic Regression is a classification algorithm that estimates the probability of an input belonging to a particular class.
In this project, the two possible classes are:
0 → Did Not Survive
1 → Survived
The algorithm learns weights for the input features during training.
The model considers the combination of:
Passenger class
Age
Gender
Fare
It then uses these learned relationships to estimate which survival category is most likely for a new passenger.
For example, the model can learn patterns from the sample data where female passengers and passengers in higher classes are associated with survival. However, the actual relationship in historical Titanic data is more complex than this small educational example.
11. Advantages
The Titanic Survival Prediction project has several advantages:
Simple and beginner-friendly.
Demonstrates binary classification.
Uses multiple input features.
Easy to implement using Python.
Demonstrates a famous real-world Machine Learning dataset.
Helps beginners understand Logistic Regression.
Can be extended with additional passenger features.
Can be evaluated using standard classification metrics.
12. Limitations
The biggest limitation of this project is the extremely small sample dataset. Only eight passenger records are used for training, which is not sufficient for an accurate real-world survival prediction system.
The actual Titanic dataset contains many more passenger records and additional features. Important factors such as passenger name, ticket information, family size, cabin information, and embarkation location are not included in this simple example.
The model also does not use a separate test dataset, so its performance on unseen data has not been properly evaluated.
Additionally, the historical survival outcome was influenced by many complex circumstances. Therefore, a simple Machine Learning model cannot completely explain why individual passengers survived or did not survive.
13. Future Improvements
The project can be improved by using the complete Titanic dataset with a much larger number of passenger records.
Additional features can be included, such as:
Passenger name
Number of siblings or spouses
Number of parents or children
Ticket number
Cabin
Embarkation port
Family size
Categorical variables can be properly encoded, and missing values can be handled before training.
The dataset can also be divided into training and testing sets. This would allow the model to be evaluated using metrics such as:
Accuracy
Precision
Recall
F1-Score
Confusion Matrix
Other Machine Learning algorithms can also be compared with Logistic Regression, including:
Decision Tree
Random Forest
K-Nearest Neighbors
Support Vector Machine
Gradient Boosting
14. Real-World Learning Applications
Although this project uses historical Titanic data, the same Machine Learning concepts can be applied to many classification problems.
For example, similar models can be used for:
Customer churn prediction
Loan approval prediction
Fraud detection
Email spam classification
Customer response prediction
Employee attrition prediction
The important concept is that the model learns from historical labeled data and uses the learned patterns to classify new observations.
15. Conclusion
The Titanic Survival Prediction Using Machine Learning project demonstrates how a supervised classification algorithm can be used to predict whether a passenger belongs to a survival category based on personal and travel information.
The project uses Logistic Regression with four input features: passenger class, age, gender, and fare. The model is trained using sample Titanic passenger data containing known survival outcomes.
For the new passenger with:
Passenger Class = 2
Age = 27
Gender = Female
Fare = 40
the model produces the following result:
Prediction: Survived
This project provides a simple introduction to supervised learning, binary classification, feature selection, data representation, model training, and prediction. By using a larger real-world dataset and additional features, the project can be extended into a more complete Machine Learning classification system.
