================================================================================
  Question 53: Titanic Passenger Survival Classification
================================================================================

FULL QUESTION:
Load the Titanic dataset ('titanic.csv') and develop classification models to predict passenger survival.
Preprocess the data by selecting relevant features ('Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare'),
handling missing numerical and categorical values, and converting categorical variables to numerical format.
Split the dataset into training and testing subsets (e.g., 80% train / 20% test).
Train and evaluate three distinct machine learning classifiers:
1. Logistic Regression
2. Decision Tree Classifier
3. Random Forest Classifier
Compute and compare model performance using Accuracy Score, Confusion Matrix, and Classification Report.

SHORT QUESTION:
Titanic passenger survival classification comparing Logistic Regression, Decision Tree, and Random Forest.

================================================================================
README & SETUP INSTRUCTIONS:
================================================================================
# Question 53: Titanic Passenger Survival Classification

## Requirements
- Python 3.x
- Libraries: pandas, scikit-learn, matplotlib

## Dataset
- 'titanic.csv' placed in the working directory.

## How to Run
```bash
python question_53_titanic.py
```

================================================================================
SOURCE CODE FILES:
================================================================================

1. question_53_titanic.py
--------------------------------------------------------------------------------
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, ConfusionMatrixDisplay

# Load the Titanic dataset (Assumes 'titanic.csv' is in your directory)
df = pd.read_csv('titanic.csv')

# Preprocessing: Select features and handle missing values
features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare']
X = df[features].copy()
y = df['Survived']

X['Sex'] = X['Sex'].map({'female': 0, 'male': 1})
X['Age'] = X['Age'].fillna(X['Age'].median())
X['Fare'] = X['Fare'].fillna(X['Fare'].median())

# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Logistic Regression
log_model = LogisticRegression(max_iter=200, random_state=42)
log_model.fit(X_train, y_train)
log_pred = log_model.predict(X_test)
print("--- Logistic Regression ---")
print("Accuracy:", accuracy_score(y_test, log_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, log_pred))
print("\nClassification Report:\n", classification_report(y_test, log_pred))

# Decision Tree Classifier
tree_clf = DecisionTreeClassifier(random_state=42)
tree_clf.fit(X_train, y_train)
tree_pred = tree_clf.predict(X_test)
print("\n--- Decision Tree Classifier ---")
print("Accuracy:", accuracy_score(y_test, tree_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, tree_pred))
print("\nClassification Report:\n", classification_report(y_test, tree_pred))

# Random Forest Classifier
rf_model = RandomForestClassifier(random_state=42)
rf_model.fit(X_train, y_train)
rf_pred = rf_model.predict(X_test)
print("\n--- Random Forest Classifier ---")
print("Accuracy:", accuracy_score(y_test, rf_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, rf_pred))
print("\nClassification Report:\n", classification_report(y_test, rf_pred))

# Visualize Confusion Matrix for Logistic Regression
fig, ax = plt.subplots(figsize=(8, 6))
ConfusionMatrixDisplay.from_predictions(y_test, log_pred, ax=ax, cmap=plt.cm.Blues)
plt.title("Confusion Matrix - Logistic Regression (Titanic)")
plt.show()
--------------------------------------------------------------------------------
