Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Master the confusion matrix, Precision, Recall, F1-Score, ROC curves, and AUC for rigorous model validation.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| F-measure | Balanced classification score | |
| Positive Predictive Value | ||
| True Positive Rate |
Precision = 0.80, Recall = 0.50.
Result:
Obtain continuous prediction probabilities from model.
Count TP, FP, TN, and FN against ground-truth labels.
Adjust threshold from 0.5 to balance Precision vs Recall depending on business cost.
Problem: A missed tumor (False Negative) can be fatal; a false alarm (False Positive) merely causes a follow-up test.
Solution: Model threshold is lowered to achieve 99% recall even if precision drops slightly.
Problem: A user missing an important job offer sent to spam (False Positive) is unacceptable.
Solution: Spam filters demand very high precision before redirecting an email away from inbox.
Interactive activity component
Calculate Precision, Recall, and F1-Score on an imbalanced dataset.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install scikit-learn1 from sklearn.metrics import confusion_matrix, classification_report
from sklearn.metrics import confusion_matrix, classification_report β Evaluation metrics
1 # Ground truth: 10 non-fraud (0), 2 fraud cases (1) 2 y_true = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] 3 # Model predictions: caught 1 fraud, missed 1 fraud, flagged 1 innocent 4 y_pred = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1] 5 6 cm = confusion_matrix(y_true, y_pred) 7 print("Confusion Matrix:") 8 print(f"TN: {cm[0][0]} | FP: {cm[0][1]}") 9 print(f"FN: {cm[1][0]} | TP: {cm[1][1]}") 10 11 print("\nDetailed Classification Report:") 12 print(classification_report(y_true, y_pred, target_names=["Legit", "Fraud"]))
# Ground truth: 10 non-fraud (0), 2 fraud cases (1)
Real outcomes
y_true = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]
True labels
# Model predictions: caught 1 fraud, missed 1 fraud, flagged 1 innocent
Predictions
y_pred = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1]
Model output
cm = confusion_matrix(y_true, y_pred)
Compute confusion matrix
print("Confusion Matrix:")
Header
print(f"TN: {cm[0][0]} | FP: {cm[0][1]}")
Row 1
print(f"FN: {cm[1][0]} | TP: {cm[1][1]}")
Row 2
print("\nDetailed Classification Report:")
Header
print(classification_report(y_true, y_pred, target_names=["Legit", "Fraud"]))
Full report
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Confusion Matrix & Classification Report in Python 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install scikit-learn 8 9 # Step 2: Import libraries 10 from sklearn.metrics import confusion_matrix, classification_report 11 12 # Step 3: Run Demonstration 13 # Ground truth: 10 non-fraud (0), 2 fraud cases (1) 14 y_true = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] 15 # Model predictions: caught 1 fraud, missed 1 fraud, flagged 1 innocent 16 y_pred = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1] 17 18 cm = confusion_matrix(y_true, y_pred) 19 print("Confusion Matrix:") 20 print(f"TN: {cm[0][0]} | FP: {cm[0][1]}") 21 print(f"FN: {cm[1][0]} | TP: {cm[1][1]}") 22 23 print("\nDetailed Classification Report:") 24 print(classification_report(y_true, y_pred, target_names=["Legit", "Fraud"]))
Counts 9 True Negatives, 1 False Alarm, 1 Missed Fraud, 1 Caught Fraud.
Calculates Precision (50%), Recall (50%), and F1 (0.50) for the fraud class.
Imbalanced labels (10 legit, 2 fraud)
Confusion Matrix:
TN: 9 | FP: 1
FN: 1 | TP: 1
Detailed Classification Report:
precision recall f1-score
Fraud 0.50 0.50 0.50While overall accuracy is 83% (10/12), the classification report reveals true fraud performance is only 50% F1.
NumPy arrays, pandas DataFrames, train-test splits, and scikit-learn models.
Test your understanding with these questions. Remember, this is for learning, not examination!