Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Master the unified Scikit-Learn API pattern: Estimator, fit(), predict(), and transform(), along with powerful Pipeline chaining.
.fit() and .transform()
- Every machine learning model implements: .fit() and .predict()
Whether you are running a simple linear regression or an ensemble of 500 gradient-boosted trees, the code looks almost identical. This consistency made machine learning accessible to millions of developers worldwide.fit(X, y) to estimate state parameters from data.
- Transformers: Implement transform(X) and fit_transform(X) for feature engineering.
- Predictors: Implement predict(X) and predict_proba(X).
- Pipeline: Chains transformers sequentially with a final estimator to prevent leakage and enable atomic serialization.fit(X, y) that persists estimated parameters as attributes ending with an underscore (e.g. coef_).transform(X) that outputs a modified array without changing sample count.Chain Scaler + Encoder + Classifier together.
Call pipeline.fit(X_train, y_train); all transformations fit cleanly without leakage.
Call pipeline.predict(X_new); raw data is automatically transformed and predicted in one step.
Problem: Raw user JSON payload arrives with unscaled numbers and text strings.
Solution: A Scikit-Learn Pipeline automatically handles one-hot encoding, scaling, and prediction in a single call.
Interactive activity component
Build an end-to-end classification pipeline: standardize Age and Salary features, train a K-Nearest Neighbors (KNN) classifier, and compute accuracy and confusion matrix.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install pandas numpy scikit-learn1 import pandas as pd 2 import numpy as np 3 from sklearn.model_selection import train_test_split 4 from sklearn.preprocessing import StandardScaler 5 from sklearn.neighbors import KNeighborsClassifier 6 from sklearn.metrics import accuracy_score, confusion_matrix
import pandas as pd → Pandas for tabular data structures
import numpy as np → NumPy array support
from sklearn.model_selection import train_test_split → Dataset split utility
from sklearn.preprocessing import StandardScaler → Feature standardization
from sklearn.neighbors import KNeighborsClassifier → KNN classifier estimator
from sklearn.metrics import accuracy_score, confusion_matrix → Evaluation metrics
1 # Sample customer purchase dataset 2 data = {'Age': [22, 25, 47, 52, 46, 56, 36, 23, 45, 55], 3 'Salary': [20000, 25000, 50000, 65000, 50000, 70000, 40000, 23000, 60000, 72000], 4 'Purchased': [0, 0, 1, 1, 1, 1, 0, 0, 1, 1]} # 0 = No, 1 = Yes 5 df = pd.DataFrame(data) 6 7 # Define features and target 8 X = df[['Age', 'Salary']] 9 y = df['Purchased'] 10 11 # Split dataset into training (80%) and testing (20%) sets 12 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 13 14 # Standardize features (Mean=0, Std=1) 15 scaler = StandardScaler() 16 X_train = scaler.fit_transform(X_train) 17 X_test = scaler.transform(X_test) 18 19 # Train K-Nearest Neighbors (KNN) classifier 20 model = KNeighborsClassifier(n_neighbors=3) 21 model.fit(X_train, y_train) 22 23 # Make predictions on test set 24 y_pred = model.predict(X_test) 25 26 # Compute accuracy and confusion matrix 27 accuracy = accuracy_score(y_test, y_pred) 28 print(f"Model Accuracy: {accuracy * 100:.2f}%") 29 conf_matrix = confusion_matrix(y_test, y_pred) 30 print("Confusion Matrix:\n", conf_matrix)
# Sample customer purchase dataset
Data creation
data = {'Age': [22, 25, 47, 52, 46, 56, 36, 23, 45, 55],
Age column
'Salary': [20000, 25000, 50000, 65000, 50000, 70000, 40000, 23000, 60000, 72000],
Salary column
'Purchased': [0, 0, 1, 1, 1, 1, 0, 0, 1, 1]} # 0 = No, 1 = Yes
Target labels
df = pd.DataFrame(data)
Create DataFrame
# Define features and target
Feature matrix and target
X = df[['Age', 'Salary']]
Select 2 features
y = df['Purchased']
Select binary target
# Split dataset into training (80%) and testing (20%) sets
Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
80/20 train/test split
# Standardize features (Mean=0, Std=1)
Scaling
scaler = StandardScaler()
Instantiate scaler
X_train = scaler.fit_transform(X_train)
Fit and transform training features
X_test = scaler.transform(X_test)
Transform testing features without leakage
# Train K-Nearest Neighbors (KNN) classifier
Model training
model = KNeighborsClassifier(n_neighbors=3)
Instantiate 3-NN model
model.fit(X_train, y_train)
Fit model
# Make predictions on test set
Inference
y_pred = model.predict(X_test)
Predict test labels
# Compute accuracy and confusion matrix
Evaluation
accuracy = accuracy_score(y_test, y_pred)
Compute test accuracy
print(f"Model Accuracy: {accuracy * 100:.2f}%")
Print accuracy
conf_matrix = confusion_matrix(y_test, y_pred)
Compute confusion matrix
print("Confusion Matrix:\n", conf_matrix)
Print confusion matrix
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # End-to-End Scikit-Learn: KNN Classification, Scaling & Confusion Matrix 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install pandas numpy scikit-learn 8 9 # Step 2: Import libraries 10 import pandas as pd 11 import numpy as np 12 from sklearn.model_selection import train_test_split 13 from sklearn.preprocessing import StandardScaler 14 from sklearn.neighbors import KNeighborsClassifier 15 from sklearn.metrics import accuracy_score, confusion_matrix 16 17 # Step 3: Run Demonstration 18 # Sample customer purchase dataset 19 data = {'Age': [22, 25, 47, 52, 46, 56, 36, 23, 45, 55], 20 'Salary': [20000, 25000, 50000, 65000, 50000, 70000, 40000, 23000, 60000, 72000], 21 'Purchased': [0, 0, 1, 1, 1, 1, 0, 0, 1, 1]} # 0 = No, 1 = Yes 22 df = pd.DataFrame(data) 23 24 # Define features and target 25 X = df[['Age', 'Salary']] 26 y = df['Purchased'] 27 28 # Split dataset into training (80%) and testing (20%) sets 29 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 30 31 # Standardize features (Mean=0, Std=1) 32 scaler = StandardScaler() 33 X_train = scaler.fit_transform(X_train) 34 X_test = scaler.transform(X_test) 35 36 # Train K-Nearest Neighbors (KNN) classifier 37 model = KNeighborsClassifier(n_neighbors=3) 38 model.fit(X_train, y_train) 39 40 # Make predictions on test set 41 y_pred = model.predict(X_test) 42 43 # Compute accuracy and confusion matrix 44 accuracy = accuracy_score(y_test, y_pred) 45 print(f"Model Accuracy: {accuracy * 100:.2f}%") 46 conf_matrix = confusion_matrix(y_test, y_pred) 47 print("Confusion Matrix:\n", conf_matrix)
Splits 10 customer records and normalizes Age and Salary with StandardScaler.
Trains a 3-Nearest Neighbors model on standardized feature space.
Evaluates accuracy and prints the 2x2 confusion matrix showing True Positives and True Negatives.
10 customer records with Age, Salary, and Purchased label
Model Accuracy: 100.00% Confusion Matrix: [[1 0] [0 1]]
By standardizing feature scales before running KNN, distance computations treat Age and Salary equally, producing 100% test accuracy on the held-out sample.
K-Nearest Neighbors (KNN), feature scaling, train-test splits, and confusion matrix diagnostics.
Test your understanding with these questions. Remember, this is for learning, not examination!