Module Assessment
Overview
This assessment brings together everything you've learned in module 09: binary classification, model evaluation metrics, and extending classifiers to multi-class settings. You'll work with the mushroom dataset — a preview of the case study in module 10. Your goal is to build and compare multiple classifiers, evaluate them rigorously, and justify your choices.
Complete the assessment using this Colaboratory notebook.
Learning Objective
By the end of this assessment, you will have demonstrated the ability to:
- Build and evaluate binary classifiers end-to-end using appropriate metrics.
- Compare multiple classification algorithms on the same dataset.
- Tune a model's decision threshold based on domain requirements.
- Interpret confusion matrices and classification reports.
The Dataset
The mushroom dataset contains physical descriptions of hypothetical mushrooms, each labeled as either edible or poisonous. This is a high-stakes binary classification problem: a false negative (predicting "edible" when a mushroom is actually poisonous) has severe real-world consequences.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import (
classification_report, confusion_matrix,
ConfusionMatrixDisplay, roc_auc_score, f1_score, recall_score
)
import matplotlib.pyplot as plt
# Load the mushroom dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data"
columns = [
"class", "cap_shape", "cap_surface", "cap_color", "bruises",
"odor", "gill_attachment", "gill_spacing", "gill_size", "gill_color",
"stalk_shape", "stalk_root", "stalk_surface_above", "stalk_surface_below",
"stalk_color_above", "stalk_color_below", "veil_type", "veil_color",
"ring_number", "ring_type", "spore_print_color", "population", "habitat"
]
df = pd.read_csv(url, header=None, names=columns)
print(df.shape)
print(df["class"].value_counts())
Output:
(8124, 23)
e 4208 ← edible
p 3916 ← poisonous
Coding Assessment
Practice the concepts from this module using this notebook. After completing all exercises, save your notebook to GitHub and submit the link for grading.
The notebook walks you through the following tasks:
Task 1: Data Preparation
Encode all categorical features and split the data into training and test sets with stratification.
# Encode target
df["label"] = (df["class"] == "p").astype(int) # 1 = poisonous, 0 = edible
# Encode features
X = df.drop(columns=["class", "label"])
y = df["label"]
# Encode all categorical columns to integers
for col in X.columns:
X[col] = LabelEncoder().fit_transform(X[col])
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
print(f"Train: {X_train.shape}, Test: {X_test.shape}")
Task 2: Train and Evaluate a Logistic Regression Baseline
Train a logistic regression model and generate a full classification report. Note which metric matters most for this domain.
Hint: In mushroom safety prediction, what is the cost of a false negative?
Task 3: Train a Decision Tree
Train a DecisionTreeClassifier with max_depth=5. Compare its confusion matrix to logistic regression. Which model makes fewer false negatives (predicting edible when the mushroom is actually poisonous)?
Task 4: Adjust the Decision Threshold
For the logistic regression model, lower the classification threshold to 0.3. Does this improve recall for the poisonous class? What happens to precision?
# Example: adjusting threshold
y_proba = clf.predict_proba(X_test)[:, 1]
y_pred_adjusted = (y_proba >= 0.3).astype(int)
Task 5: Comparison Table
Fill in this table with your results:
| Model | Accuracy | Precision (poisonous) | Recall (poisonous) | F1 (poisonous) | AUC |
|---|---|---|---|---|---|
| Logistic Regression (threshold=0.5) | |||||
| Logistic Regression (threshold=0.3) | |||||
| Decision Tree (max_depth=5) |
Task 6: Recommendation
Write 2–3 sentences justifying which model and threshold you would deploy in a real application that helps hikers identify safe mushrooms, and why.