Machine Learning Basics: Supervised, Unsupervised, and Reinforcement Learning (Lecture 2)

This is Lecture 2 of our AI 101 series. We’ll break down three core types of Machine Learning, explore their real-world applications, and finish with a verified scikit-learn lab that runs locally without internet access.


Table of Contents

{% toc %}


1) What Is Machine Learning?

Machine Learning (ML) is the process of teaching computers to learn patterns from data and make predictions without being explicitly programmed with rules.

Instead of telling the computer how to solve a problem, we give it examples (data) and let it find the rules itself.


Everyday ML Examples

  • Spam filters: Learn patterns of spam vs. normal emails.
  • Speech recognition: Trained on thousands of hours of audio.
  • Recommendation engines: Suggest products or content based on user behavior.
  • Self-driving cars: Learn road patterns from camera/sensor data.

2) Three Main Types of Machine Learning


2.1 Supervised Learning

  • Learns from labeled data (input + correct output).

  • Analogy: Studying with both the questions and the answer key.

  • Tasks:

    • Classification: Predict a category (spam vs. ham).
    • Regression: Predict a continuous value (house prices).
Task TypeOutput TypeExample
ClassificationCategoricalSpam/Not spam, Iris species
RegressionContinuousPredicting stock prices

2.2 Unsupervised Learning

  • Learns from unlabeled data (only inputs, no answers).

  • Analogy: Grouping similar exam questions without knowing the correct answers.

  • Tasks:

    • Clustering: Group similar data points.
    • Dimensionality Reduction: Reduce features while preserving key info.
MethodDescription
ClusteringGroups data points based on similarity.
Dimensionality ReductionCompresses features while keeping core patterns.

2.3 Reinforcement Learning

  • Learns by interacting with an environment: takes an action, gets a reward, and adjusts.

  • Analogy: Learning to play a game by trial and error to maximize the score.

  • Applications:

    • AlphaGo (Go-playing AI)
    • Robotics
    • Autonomous driving

3) General ML Development Workflow

  1. Data Collection: CSV, databases, APIs, etc.
  2. Data Preprocessing: Handle missing/outlier values, normalize features.
  3. Model Selection: Choose based on the task type.
  4. Training: Fit the model to the data.
  5. Evaluation: Measure performance (accuracy, F1-score, etc.).
  6. Deployment: Integrate the model into production.

4) Hands-On Lab: Classifying Iris Species

We’ll use scikit-learn’s built-in Iris dataset for a safe, offline, beginner-friendly supervised learning demo.

Setup

1
pip install scikit-learn matplotlib
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, ConfusionMatrixDisplay
import matplotlib.pyplot as plt

# 1. Load dataset
iris = load_iris()
X, y = iris.data, iris.target

# 2. Train/test split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# 3. Create and train model
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)

# 4. Predict
y_pred = model.predict(X_test)

# 5. Evaluate
print("Accuracy:", f"{accuracy_score(y_test, y_pred)*100:.2f}%")
print("\nClassification report:\n", classification_report(y_test, y_pred, target_names=iris.target_names))

# 6. Visualize confusion matrix
ConfusionMatrixDisplay.from_estimator(model, X_test, y_test, display_labels=iris.target_names)
plt.show()

Expected Output

1
2
3
4
5
6
Accuracy: 100.00%
Classification report:
              precision    recall  f1-score   support
    setosa       1.00      1.00      1.00        10
versicolor       1.00      1.00      1.00        10
 virginica       1.00      1.00      1.00        10

5) Key Takeaways

  • Supervised Learning: Learns from labeled data (classification/regression).
  • Unsupervised Learning: Finds structure in unlabeled data (clustering/dimensionality reduction).
  • Reinforcement Learning: Learns via trial-and-error feedback.
  • Hands-on: Successfully trained a simple supervised learning model offline.

6) FAQ (Answer Engine Optimization)

Q1. Which type of ML is most common in business? A. Supervised learning—most business problems have historical labeled data.

Q2. Do I need deep learning for every ML task? A. No. Many tasks are better handled with traditional ML models like logistic regression or random forests.

Q3. Can I mix these learning types? A. Yes, hybrid approaches exist (e.g., semi-supervised learning).

Q4. Does reinforcement learning need huge compute? A. For complex tasks, yes—but simple simulations can run on a laptop.


7) Summary Table

TypeInput DataOutputExample
SupervisedLabeledPredictionSpam filter, house price
UnsupervisedUnlabeledPatternsCustomer segmentation
ReinforcementEnvironment + RewardPolicyGame AI, robotics

8) What’s Next?

In Lecture 3, we’ll explore Deep Learning fundamentals, including CNNs for images and RNNs for sequences.