Titanic Survival Prediction β€” Logistic Regression

A scikit-learn pipeline trained for the Titanic β€” Machine Learning from Disaster Kaggle competition. It predicts whether a passenger survived (1) or not (0) from passenger and travel attributes.

Scope. This is an educational project and competition baseline. It must not be used for safety, travel, insurance or any other consequential decision about people.

Model details

Task Binary classification on tabular data
Algorithm Logistic Regression (max_iter=1000) inside a scikit-learn Pipeline
Preprocessing Median imputation + standardization (numeric); most-frequent imputation + one-hot encoding (categorical)
Training data 891 labeled passengers from Kaggle's train.csv
Inputs 11 features: Pclass, Age, SibSp, Parch, Fare, FamilySize, IsAlone, HasCabin (numeric) and Sex, Embarked, Title (categorical)
Artifact logistic_regression_titanic.joblib (created with scikit-learn 1.9.1)

inference.py takes raw passenger fields and derives the model features exactly as in training:

  • FamilySize = SibSp + Parch + 1, IsAlone = (FamilySize == 1), HasCabin = a cabin value is present.
  • Title is extracted from Name ("Braund, Mr. Owen Harris" β†’ Mr); Mlle/Ms β†’ Miss, Mme β†’ Mrs, and rare titles (Dr, Rev, Col, ...) β†’ Rare.

Evaluation

Evaluation Result
Kaggle public leaderboard score 0.77033
Stratified 80/20 holdout accuracy (179 passengers) 83.24% (precision 80.0%, recall 75.4%, F1 77.6%)
5-fold CV accuracy on the 712 training passengers 82.59% Β± 2.98
5-fold CV F1 / ROC-AUC 77.28% / 0.872

The final pipeline was refit on all 891 labeled passengers before predicting the 418 competition test passengers. The Kaggle score and the local holdout are measured on different data and are different estimates.

Four classifiers (Logistic Regression, Random Forest, Extra Trees, SVM) tied within about 0.3 points of accuracy in cross-validation; Logistic Regression was kept for the highest F1 and its simplicity. In a later repeated 5Γ—5 cross-validation on all 891 rows, extra features (ticket group size, fare per person, deck) and gradient boosting changed accuracy by about one point or less, which is within fold-to-fold noise, so this baseline was not changed. The local numbers above come from a very small dataset and should be read with that uncertainty.

Usage

Install dependencies (the scikit-learn version matters for loading the artifact):

pip install -r requirements.txt

Download logistic_regression_titanic.joblib, inference.py and sample_input.json from this repository into the same folder, then run:

python inference.py --input sample_input.json --model logistic_regression_titanic.joblib

Output (one entry per passenger):

[
  {"PassengerId": 1, "predicted_survived": 0, "survival_probability": 0.07773},
  {"PassengerId": 2, "predicted_survived": 1, "survival_probability": 0.965977}
]

From Python:

from inference import predict_passengers

passengers = [{
    "PassengerId": 1, "Pclass": 3, "Name": "Example, Mr. Passenger", "Sex": "male",
    "Age": 22, "SibSp": 1, "Parch": 0, "Fare": 7.25, "Cabin": None, "Embarked": "S",
}]

print(predict_passengers("logistic_regression_titanic.joblib", passengers))

Missing Age, Fare, Embarked or Cabin values are accepted (None/NaN); the pipeline imputes them as in training. Name is only used to extract the title.

Training procedure and reproducibility

The full code is in the companion repository caueeeb/kaggle-titanic: feature engineering, pipeline, model comparison, tests and a python -m src.train command that regenerates this artifact's predictions and the Kaggle submission. The original CSV files are not included here; obtain them from Kaggle and follow the competition rules.

The helper in this repository was checked against the training code: for all 891 training and 418 test passengers it produces identical features and identical predictions.

Limitations and responsible use

  • The dataset is small (891 labeled rows) and describes one historical event; patterns learned here (for example, differences by sex and class) are associations, not causes, and reflect the social conditions of 1912.
  • Predictions for individuals are probabilities from a simple model, not facts.
  • One passenger title ("the Countess") is not grouped into Rare by the original rule; it is kept to preserve reproducibility.
  • The artifact is a serialized scikit-learn pipeline (joblib/pickle). Only load files from sources you trust, and use a compatible scikit-learn version.

Author

CauΓͺ Buzolin β€” GitHub Β· LinkedIn

Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Evaluation results

  • Holdout accuracy on Kaggle Titanic train.csv (stratified 80/20 holdout, 179 passengers)
    self-reported
    0.832
  • Holdout F1-score on Kaggle Titanic train.csv (stratified 80/20 holdout, 179 passengers)
    self-reported
    0.776