python3 -m venv kaggle-titanic
source kaggle-titanic/bin/activate
pip freeze > requirements.txt
jupyter notebook --port=8891
- Project Description
- Dataset
- Preprocessing
- Feature Engineering
- Model
- Model Evaluation
- Final Submission
- Requirements
- How to Run
This project aims to predict the survival of passengers aboard the Titanic using machine learning techniques. Using a Random Forest Classifier, we predict whether a passenger survived or not based on various features like age, sex, class, and family size. The model is trained and evaluated using cross-validation, and the final model is used to predict survival for the test dataset.
As of now, the best score achieved on Kaggle is 0.79665.
The dataset used for this project is the Titanic dataset, which is available on Kaggle. It consists of two files:
train.csv- Contains the training data with both features and the target variable (Survived).test.csv- Contains the test data with features only (no target variable), used for generating predictions.
The preprocessing steps are as follows:
- Reading the Data: We load the training and test datasets from CSV files using
pandas. - Handling Missing Values:
- We use
SimpleImputerto fill missing values in theAgecolumn with the median of that column. - We handle other missing values by filling them with
0.
- We use
- Categorical Variables:
- We use
pd.get_dummies()to convert categorical variables likeEmbarked,Sex, andCabininto numeric features.
- We use
- Feature Engineering:
- We create a new feature called
FamilySizeby combining theSibSp(siblings/spouses aboard) andParch(parents/children aboard) columns. - We create an
IsAlonefeature to identify whether the passenger is traveling alone (FamilySize == 1). - We create a
HasCabinbinary feature to indicate whether a passenger has cabin information available.
- We create a new feature called
- Family Size: The size of the passenger's family is derived by summing
SibSpandParchand adding1(for the passenger). - IsAlone: This is a binary feature indicating whether a passenger is traveling alone. If
FamilySizeequals 1, then the passenger is alone. - HasCabin: A binary feature to indicate whether the passenger's cabin information is missing or not.
We use the Random Forest Classifier, a powerful ensemble learning method based on decision trees:
- Hyperparameters:
n_estimators = 100: The number of trees in the forest.max_depth = 10: The maximum depth of the trees.random_state = 3: Ensures reproducibility.
The model is evaluated using 5-fold cross-validation to estimate its generalization performance.
We use cross_val_score to perform 5-fold cross-validation and obtain an average accuracy score. This method divides the data into 5 subsets (folds), trains the model on 4 folds, and tests it on the remaining fold, repeating the process for each fold.
The mean accuracy from cross-validation is printed to the console, allowing us to evaluate the model's performance.
Mean cross-validation score: 0.8170736300295023
Kaggle website score : 0.79665