In [8]:
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
import matplotlib.pyplot as plt

# Load the IRIS dataset
iris = load_iris()
X = iris.data
y = iris.target

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a decision tree classifier
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)

# Make predictions on the test set
y_pred = clf.predict(X_test)

# Generate the confusion matrix
cm = confusion_matrix(y_test, y_pred)

# Display the confusion matrix
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=iris.target_names)
disp.plot(cmap=plt.cm.Blues)
plt.title('Confusion Matrix for Decision Tree Classifier')
plt.show()
No description has been provided for this image
In [9]:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, classification_report
import matplotlib.pyplot as plt

# --- 1. Load and Clean Data (from Activity 1) ---
# Load the dataset from your Downloads folder
file_path = '~/Downloads/titanic.csv'
data = pd.read_csv(file_path)

# Fill missing 'Age' values with the mean
data['Age'] = data['Age'].fillna(data['Age'].mean())

# Fill missing 'Embarked' values with the most common port
# 'mode()[0]' gets the most frequent value (which is 'S')
most_common_port = data['Embarked'].mode()[0]
data['Embarked'] = data['Embarked'].fillna(most_common_port)

# Drop columns that are not useful for the model
data.drop(['Cabin', 'PassengerId', 'Name', 'Ticket'], axis=1, inplace=True)


# --- 2. Prepare Data for Modeling (Feature Engineering) ---
# Convert categorical columns ('Sex', 'Embarked') into numbers
# pd.get_dummies creates new columns for each category (e.g., Embarked_S, Embarked_C)
data = pd.get_dummies(data, columns=['Sex', 'Embarked'], drop_first=True)

# Separate the features (X) from the target (y)
X = data.drop('Survived', axis=1)
y = data['Survived']

print("--- Final Features for the Model ---")
print(X.head())
print("\n")


# --- 3. Split Data into Training and Testing Sets ---
# This is the same process as in the Iris example
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

print(f"Data split complete: {len(X_train)} training samples, {len(X_test)} testing samples.\n")


# --- 4. Train a Decision Tree Classifier ---
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)
print("Decision Tree model has been trained.\n")


# --- 5. Make Predictions and Evaluate the Model ---
# Make predictions on the unseen test data
y_pred = clf.predict(X_test)

# Print the classification report for detailed metrics
print("--- Classification Report ---")
# This shows precision, recall, and f1-score for each class
print(classification_report(y_test, y_pred, target_names=['Died', 'Survived']))

# Generate and display the confusion matrix
print("--- Confusion Matrix ---")
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=['Died', 'Survived'])
disp.plot(cmap=plt.cm.Blues)
plt.title('Confusion Matrix for Titanic Survival Prediction')
plt.show()
--- Final Features for the Model ---
   Pclass   Age  SibSp  Parch     Fare  Sex_male  Embarked_Q  Embarked_S
0       3  22.0      1      0   7.2500      True       False        True
1       1  38.0      1      0  71.2833     False       False       False
2       3  26.0      0      0   7.9250     False       False        True
3       1  35.0      1      0  53.1000     False       False        True
4       3  35.0      0      0   8.0500      True       False        True


Data split complete: 712 training samples, 179 testing samples.

Decision Tree model has been trained.

--- Classification Report ---
              precision    recall  f1-score   support

        Died       0.81      0.84      0.83       105
    Survived       0.76      0.73      0.74        74

    accuracy                           0.79       179
   macro avg       0.79      0.78      0.79       179
weighted avg       0.79      0.79      0.79       179

--- Confusion Matrix ---
No description has been provided for this image
In [ ]: