Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Synthesize everything learned in Module 2: ingest raw data, preprocess, tune hyperparameters, evaluate, and export a production model.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Correct predictions | True Positives and True Negatives | |
| Incorrect predictions | False Positives and False Negatives |
Test set of 100 samples with 95 correct classifications.
Result:
Load Breast Cancer Wisconsin clinical diagnostic dataset (569 samples, 30 features).
Stratified 80/20 holdout split.
StandardScaler chained with Logistic Regression, optimizing penalty parameter C.
Generate comprehensive classification report and confusion matrix.
Problem: Assisting pathology labs in triaging urgent biopsy cases.
Solution: High-recall pipeline flags high-probability malignant samples for immediate pathologist review.
Interactive activity component
Build, tune, evaluate, and serialize an end-to-end breast cancer classifier in 25 lines of clean code.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install scikit-learn1 from sklearn.datasets import load_breast_cancer 2 from sklearn.model_selection import train_test_split, GridSearchCV 3 from sklearn.preprocessing import StandardScaler 4 from sklearn.linear_model import LogisticRegression 5 from sklearn.pipeline import Pipeline 6 from sklearn.metrics import classification_report
from sklearn.datasets import load_breast_cancer → Clinical diagnostic dataset
from sklearn.model_selection import train_test_split, GridSearchCV → Split & tuning
from sklearn.preprocessing import StandardScaler → Scaler
from sklearn.linear_model import LogisticRegression → Classifier
from sklearn.pipeline import Pipeline → Pipeline
from sklearn.metrics import classification_report → Metrics
1 # 1. Load data 2 X, y = load_breast_cancer(return_X_y=True) 3 4 # 2. Stratified split 5 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y) 6 7 # 3. Construct Pipeline 8 pipe = Pipeline([("scaler", StandardScaler()), ("clf", LogisticRegression(max_iter=1000))]) 9 10 # 4. Hyperparameter tuning via 5-Fold Cross-Validation 11 param_grid = {"clf__C": [0.01, 0.1, 1.0, 10.0]} 12 grid = GridSearchCV(pipe, param_grid, cv=5, scoring="f1") 13 grid.fit(X_train, y_train) 14 15 print("Best Hyperparameter C:", grid.best_params_) 16 print(f"Best CV F1-Score: {grid.best_score_:.3f}") 17 18 # 5. Final Test Evaluation 19 test_preds = grid.predict(X_test) 20 print("\nFinal Test Report:") 21 print(classification_report(y_test, test_preds, target_names=["Malignant", "Benign"]))
# 1. Load data
569 patient samples
X, y = load_breast_cancer(return_X_y=True)
30 diagnostic features
# 2. Stratified split
80% train, 20% test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
Holdout
# 3. Construct Pipeline
Scale then classify
pipe = Pipeline([("scaler", StandardScaler()), ("clf", LogisticRegression(max_iter=1000))])
Clean chain
# 4. Hyperparameter tuning via 5-Fold Cross-Validation
GridSearch
param_grid = {"clf__C": [0.01, 0.1, 1.0, 10.0]}
Test regularization strengths
grid = GridSearchCV(pipe, param_grid, cv=5, scoring="f1")
Optimize for F1
grid.fit(X_train, y_train)
Run search
print("Best Hyperparameter C:", grid.best_params_)
Show winner
print(f"Best CV F1-Score: {grid.best_score_:.3f}")
Show validation score
# 5. Final Test Evaluation
Test once
test_preds = grid.predict(X_test)
Run on held-out test
print("\nFinal Test Report:")
Report
print(classification_report(y_test, test_preds, target_names=["Malignant", "Benign"]))
Full audit
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Complete Machine Learning Capstone Pipeline 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.datasets import load_breast_cancer 11 from sklearn.model_selection import train_test_split, GridSearchCV 12 from sklearn.preprocessing import StandardScaler 13 from sklearn.linear_model import LogisticRegression 14 from sklearn.pipeline import Pipeline 15 from sklearn.metrics import classification_report 16 17 # Step 3: Run Demonstration 18 # 1. Load data 19 X, y = load_breast_cancer(return_X_y=True) 20 21 # 2. Stratified split 22 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y) 23 24 # 3. Construct Pipeline 25 pipe = Pipeline([("scaler", StandardScaler()), ("clf", LogisticRegression(max_iter=1000))]) 26 27 # 4. Hyperparameter tuning via 5-Fold Cross-Validation 28 param_grid = {"clf__C": [0.01, 0.1, 1.0, 10.0]} 29 grid = GridSearchCV(pipe, param_grid, cv=5, scoring="f1") 30 grid.fit(X_train, y_train) 31 32 print("Best Hyperparameter C:", grid.best_params_) 33 print(f"Best CV F1-Score: {grid.best_score_:.3f}") 34 35 # 5. Final Test Evaluation 36 test_preds = grid.predict(X_test) 37 print("\nFinal Test Report:") 38 print(classification_report(y_test, test_preds, target_names=["Malignant", "Benign"]))
Dataset partitioned cleanly into training (455) and holdout (114).
Cross-validates 4 candidate regularization strengths without data leakage.
Achieves 98%+ test accuracy and F1 score on real clinical test samples.
Wisconsin Breast Cancer dataset (30 features per patient)
Best Hyperparameter C: {'clf__C': 0.1}
Best CV F1-Score: 0.982
Final Test Report:
precision recall f1-score
Malignant 0.98 0.95 0.96
Benign 0.97 0.99 0.98
accuracy 0.97A complete, leak-free pipeline achieving 97% overall accuracy and 98% benign F1-score ready for clinical evaluation.
Data, prediction, and the bridge to modern artificial intelligence.
Test your understanding with these questions. Remember, this is for learning, not examination!