π· WEEK 3 Lesson 3
Title:
Classification Models
Theme:
From Notebook Experiments β Real AI Systems
Why This Lesson Exists
Most beginners learn machine learning like this:
Load data β Train model β Accuracy β Done β
But real-world AI works differently.
In industry:
AI is a living system, not a one-time model.
Models must be:
built,
deployed,
watched,
repaired,
retrained continuously.
This lesson introduces production thinking β the mindset used by professional AI teams...
Learning Objectives
By the AI Specialization Program β’ Technical Tracks
AI Engineering & Data β’ Lessons
π· WEEK 3 Lesson 3
Title: Classification Models
Lesson Objective
By the end of this lesson, learners will:
Understand what classification models are
Learn the intuition behind logistic regression
Understand decision trees and how they split data
Understand the concept of k-Nearest Neighbors (KNN)
Learn how classification models are evaluated
Understand confusion matrix, precision, recall, and F1-score
This lesson introduces predicting categories instead of numbers.
1. What is Classification?
Classification is a type of supervised learning where the goal is to predict categories or labels.
Examples:
Label
βWin money now!β
Spam
βMeeting tomorrowβ
Not Spam
The model learns patterns that separate categories.
Examples of classification problems:
Spam email detection
Fraud detection
Disease diagnosis
Sentiment analysis
Image recognition
Classification answers:
Text
Copy code
Which category does this belong to?
Instead of predicting numbers like regression, classification predicts classes.
2. Logistic Regression Intuition
Despite the name, logistic regression is a classification algorithm.
It predicts the probability of a class.
Example:
Predict whether an email is spam.
Output might be:
Text
Copy code
Spam probability = 0.82
If probability > 0.5 β classify as Spam.
Logistic regression uses an S-shaped curve called the sigmoid function.
Conceptually:
Text
Copy code
Input β Sigmoid Function β Probability β Class
This allows the model to make binary decisions.
3. Decision Trees Basics
Decision Trees make decisions using ifβthen rules.
Example:
Text
Copy code
Is income > 50,000?
|
Yes
|
Is credit score > 700?
|
Approve Loan
The model splits the data step by step.
Each split reduces uncertainty.
Advantages:
Easy to interpret
Works with both numeric and categorical data
No scaling required
Decision trees are often the first interpretable model engineers try.
4. k-Nearest Neighbors (KNN) Intuition
KNN makes predictions based on similar data points.
Concept:
A new data point looks at its k closest neighbors.
Example:
Text
Copy code
New point β look at 5 nearest neighbors
If most neighbors are class A, the new point becomes A.
KNN works using distance calculations.
Important idea:
Similar inputs should produce similar outputs.
However, KNN can become slow with very large datasets.
5. The Confusion Matrix
To evaluate classification models, we use a confusion matrix.
Example:
Predicted Positive
Predicted Negative
Actual Positive
True Positive
False Negative
Actual Negative
False Positive
True Negative
This matrix shows how predictions compare to reality.
From this matrix we calculate important metrics.
6. Precision, Recall, and F1 Score
Accuracy alone can be misleading.
Instead, we use better metrics.
Precision
Precision measures how many predicted positives were correct.
Text
Copy code
Precision = True Positives / (True Positives + False Positives)
Important when false alarms are costly.
Example:
Spam detection.
Recall
Recall measures how many real positives were detected.
Text
Copy code
Recall = True Positives / (True Positives + False Negatives)
Important when missing a case is dangerous.
Example:
Disease detection.
F1 Score
F1-score balances precision and recall.
Text
Copy code
F1 = 2 Γ (Precision Γ Recall) / (Precision + Recall)
This gives a single balanced metric.
7. Implementing a Classification Model in Python
Example using logistic regression.
Python
Copy code
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
X = df[['feature1','feature2']]
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print(classification_report(y_test, predictions))
This produces precision, recall, and F1-score.
8. Engineering Mindset for Classification
Engineers must think about:
Class imbalance
Data quality
Feature importance
Evaluation metrics
Deployment requirements
A model with high accuracy but poor recall may still fail in production.
Choosing the right metric is critical.
Mini Practical Exercise
Students should:
Load a classification dataset.
Train a logistic regression model.
Make predictions.
Generate a confusion matrix.
Calculate precision and recall.
Write a short explanation:
Which metric matters most for your problem?
Explain why.
Week 3 β Lesson 3 Outcome
Students now:
β Understand classification problems
β Understand logistic regression intuition
β Understand decision trees and KNN
β Understand confusion matrix evaluation
β Understand precision, recall, and F1-score
β Can build and evaluate a classification model
Students can now build both regression and classification ML systems.
Next:
π· Week 3 Lesson 4 β Model Evaluation & Overfitting
Where students learn how to diagnose why models fail and how to fix them.
3. Evaluation
Before deployment, we test performance.
Common Metrics
Problem Type
Metrics
Classification
Accuracy, Precision, Recall
Regression
MAE, RMSE
Imbalanced data
F1-score
Example
Fraud detection:
Accuracy: 95%
But fraud cases missed β
Hence: Precision & Recall matter more.
Validation Split
Python
train_test_split()
Prevents overfitting.
4. Deployment
Deployment = making AI usable by real people.
The model leaves the notebook.
Deployment Forms
Web API
Mobile app
Backend service
Edge device
Dashboard integration
Simplified Flow
Text
User Input β API β Model β Prediction β Application Response
Example
AFRA platform risk scoring:
User transaction β model predicts risk β system approves or flags.
5. Monitoring (Most Ignored Step)
After deployment, performance changes.
Why?
Because the world changes.
What We Monitor
prediction accuracy
input data distribution
latency
error rates
business impact
Example
Customer behavior changes during holidays β model accuracy drops.
6. Dataset Drift (Critical Concept)
Dataset drift happens when real-world data changes over time.
Types of Drift
Data Drift
Input patterns change.
Example:
new payment methods appear.
Concept Drift
Relationship changes.
Example:
fraudsters change tactics.
Visual Idea
Text
Training Data (Past)
β
Real World (Present)
β
Model assumptions break
Consequence
Models silently become worse.
This is called:
Model Decay.
7. Iteration (AI Never Finishes)
Professional AI cycle:
Text
Collect New Data
β
Retrain Model
β
Evaluate Again
β
Redeploy
AI systems evolve continuously.
Production Thinking Mindset
Students move from:
β βI built a model.β
to
β βI built a system that learns over time.β
Real Industry Workflow
Text
Data Engineers β prepare pipelines
Data Scientists β build models
ML Engineers β deploy systems
Product Teams β monitor impact
Modern role: π Full-stack AI thinker (what this course is training).
Mini System Architecture Example
Text
User App
β
Backend API
β
Feature Processing
β
ML Model
β
Prediction Database
β
Monitoring Dashboard
Students understand AI as infrastructure.
Hands-On Concept Exercise
Students design lifecycle for:
Loan Approval AI
They must define:
Data source
Training process
Evaluation metric
Deployment method
Monitoring strategy
Retraining trigger
(No coding β system thinking.).
Common Beginner Mistakes
β Training once and never updating
β Ignoring monitoring
β Measuring only accuracy
β Deploying without validation
β Forgetting data versioning
Golden Rule of Production AI
Text
The hardest part of AI is not building models.
It is keeping them working.
Why This Lesson Matters Before Deep ML
Students now understand:
AI is engineering
AI is lifecycle management
AI is continuous learning systems
Deep learning later becomes meaningful because they know where models fit.
Week 2 Integration Summary
Students can now:
β Load and explore data (Pandas)
β Clean datasets systematically
β Build transformation pipelines
β Engineer meaningful features
β Understand full AI lifecycle
They have crossed from:
Beginner Coders β Junior AI Practitioners
Outcome Achieved
Students now understand the complete AI system lifecycle before studying advanced machine learning.
Week 3 β Introduction to Machine Learning Proper
(where models finally enter β but now students are prepared like professionals, not beginners).
Powered by Soft AI Africa | Training the Next Generation of AI Leaders in Africa.