Predicting Titanic Survival with Scikit-learn's Decision Tree Classifier

The DecisionTreeClassifier from scikit-learn is a versatile supervised learning algorithm used for classification tasks. It operates by constructing a tree-like model of decisions, where each internal node represents a "test" on an attribute, each branch represents the outcome of the test, and each leaf node represents a class label (the decision taken after computing all attributes). This article demonstartes its practical application in predicting passenger survival on the Titanic, leveraging key features such as passenger class, age, and sex.

The process typically involves the following stages:

  1. Data Loading and Preprocessing: Reading the dataset and handling missing values to prepare it for machine learning.
  2. Feature Vectorization: Transforming raw categorical and numerical features into a format suitable for the algorithm (e.g., numerical arrays).
  3. Model Training: Instantiating and training the DecisionTreeClassifier on the prepared data.
  4. Prediction: Using the trained model to predict outcomes for new, unseen data points.

The Python implementation below illustrates these steps. It utilizes the pandas library for data manipulation and scikit-learn for the decision tree model and feature transformation.

import pandas as pd
from sklearn.feature_extraction import DictVectorizer
from sklearn.tree import DecisionTreeClassifier
import numpy as np

def analyze_titanic_survival_predictor():
    """
    Trains a DecisionTreeClassifier to predict Titanic survival based on
    passenger class, age, and sex, then demonstrates a prediction for a
    hypothetical new passenger.
    """
    # 1. Data Loading and Preprocessing
    try:
        passenger_dataset = pd.read_csv('Titanic.csv')
    except FileNotFoundError:
        print("Error: 'Titanic.csv' not found. Please ensure the dataset file is in the current directory.")
        return

    # Fill any missing 'Age' values with the mean age of all passengers
    passenger_dataset['Age'].fillna(passenger_dataset['Age'].mean(), inplace=True)

    # Define the features (independent variables) and the target (dependent variable)
    selected_features = ['Pclass', 'Age', 'Sex']
    survival_target = 'Survived'

    # Convert the selected feature rows into a list of dictionaries,
    # which is the input format for DictVectorizer
    feature_observation_dicts = passenger_dataset[selected_features].to_dict(orient='records')

    # Extract the target labels (survival status)
    # .values gets the numpy array, .ravel() ensures it's a 1D array as expected by .fit()
    target_labels = passenger_dataset[survival_target].values.ravel()

    # 2. Feature Vectorization
    # Initialize DictVectorizer to convert feature dictionaries into a numerical
    # feature matrix, handling categorical features like 'Sex' automatically.
    feature_encoder = DictVectorizer(sparse=False)
    X_training_data = feature_encoder.fit_transform(feature_observation_dicts)

    # 3. Model Training
    # Create an instance of the Decision Tree Classifier.
    # random_state ensures reproducibility of results.
    survival_model = DecisionTreeClassifier(random_state=42)

    # Train the model using the vectorized features and the survival labels
    survival_model.fit(X_training_data, target_labels)

    # 4. Making a Prediction for a New Passenger
    # Define attributes for a hypothetical new passenger
    new_passenger_attributes = {
        'Pclass': 1,      # Example: First Class
        'Age': 60,        # Example: 60 years old
        'Sex': 'female'   # Example: Female
    }

    # Transform the new passenger's attributes using the *same* encoder
    # that was fitted on the training data. This is crucial to maintain consistency.
    X_new_passenger_encoded = feature_encoder.transform(new_passenger_attributes)

    # Predict the survival outcome for the new passenger
    predicted_survival_category = survival_model.predict(X_new_passenger_encoded)

    # Interpret and print the prediction
    if predicted_survival_category[0] == 1:
        outcome_text = "SURVIVED"
    else:
        outcome_text = "DID NOT SURVIVE"

    print(f"For a passenger with attributes: {new_passenger_attributes}")
    print(f"The model predicts: {outcome_text}")

# Execute the analysis and prediction function
if __name__ == "__main__":
    analyze_titanic_survival_predictor()

Tags: DecisionTreeClassifier scikit-learn python MachineLearning Titanic

Posted on Wed, 15 Jul 2026 16:20:40 +0000 by lwq