This project implements a neural network model for a binary classification problem to predict survival on the Titanic. The model is trained, validated, and tested on a preprocessed Titanic dataset. Key features include custom gradient descent optimization, forward and backward propagation, and evaluation using metrics like accuracy.
The goal of this project is to predict passenger survival (binary classification) using a neural network built from scratch with NumPy. The project focuses on:
- Preprocessing the Titanic dataset for missing values and feature encoding.
- Dividing the dataset into training, validation, and test sets.
- Training the model using gradient descent and monitoring loss.
- Evaluating the model on unseen test data using accuracy as the primary metric.
The dataset used is the Titanic dataset, which contains information about passengers, such as:
- Features: Passenger attributes (e.g.,
Pclass,Age,Fare,Sex). - Target Variable:
Survived(binary classification: 1 = Survived, 0 = Did not survive).
- Handle Missing Values:
- Dropped the
Cabincolumn due to excessive missing data. - Filled missing values in the
Agecolumn with the mean. - Filled missing values in the
Embarkedcolumn with the mode.
- Dropped the
- Feature Encoding:
- Encoded the
Sexcolumn as numerical values usingLabelEncoder.
- Encoded the
- Feature Selection:
- Removed unnecessary columns:
Name,Ticket,PassengerId, andEmbarked.
- Removed unnecessary columns:
- Data Split:
- Training Set: 60%
- Validation Set: 20%
- Test Set: 20%
The neural network has the following structure:
- Input Layer: Accepts 7 features (after preprocessing).
- Hidden Layer: Contains 64 neurons with the sigmoid activation function.
- Output Layer: 1 neuron with the sigmoid activation function for binary classification.
- Gradient Descent: Optimized weights and biases using gradient descent.
- Forward and Backward Propagation: Implemented manually with NumPy for a two-layer neural network.
- Evaluation Metrics: Accuracy calculated on the test set.
- Forward Propagation:
- Calculated activations for the hidden and output layers using the sigmoid function.
- Computed training and validation losses using binary cross-entropy.
- Backward Propagation:
- Calculated gradients of weights and biases for both layers.
- Updated weights and biases using gradient descent with a learning rate of 0.21.
- Epochs:
- Trained the model for 50,000 epochs, logging losses every 1,000 epochs.
After training, the model was evaluated on the test set with the following result:
| Metric | Value (%) |
|---|---|
| Accuracy | 81.67 |
- Data Preprocessing: Cleaned and prepared the Titanic dataset for training.
- Neural Network Training:
- Implemented forward propagation to compute outputs.
- Used backward propagation to calculate gradients and optimize weights.
- Testing: Evaluated the model on the test set using accuracy as the primary metric.
The project uses the following libraries:
- NumPy: For mathematical computations.
- Pandas: For data handling.
- scikit-learn: For preprocessing and splitting datasets.
- SciPy: For the numerically stable sigmoid function.
Install dependencies via:
pip install numpy pandas scikit-learn scipy