Course materials ยท Introduction to Machine Learning

[Week 12] First Steps in Deep Learning: Beyond Classical Models with MLP ๐Ÿš€

English translation

Welcome to Week 12!
So far, we have learned powerful classical Machine Learning models
such as Decision Trees and Random Forests.
These models learned which questions to ask (rules).

Today, we finally meet the most basic Deep Learning model:
the MLP (Multilayer Perceptron).

An MLP solves nonlinear problems by 'stacking multiple layers' of simple perceptrons,
which briefly appeared in the Week 2 history lesson as a cause of the 'AI winter'.
Implement this model using MLPClassifier from scikit-learn,
and revisit the Iris problem from Week 10 to examine its performance.

๐Ÿง  Module 1: What Is an MLP (Multilayer Perceptron)?

Think of MLP as a 'company making complex decisions'.

1. Input Layer โ€” 'New Employees'

2. Hidden Layer(s) โ€” 'Middle Managers'

3. Output Layer โ€” 'Chief Executive Officer (CEO)'

Neurons and Nonlinear Activation Functions (ReLU)

Each MLP node (neuron) essentially does two things.

1. Linear Calculation: As in Logistic Regression, multiply inputs (xx) by weights (WW) and add a bias (bb).

2. Nonlinear Activation: Pass the calculated zz value through a nonlinear function.

ReLU (Rectified Linear Unit):

Learning Process: Backpropagation

How Does an MLP Learn? (Review of Week 2 History)

  1. Feed data into the input layer and calculate through to the output layer (Forward Propagation).
  2. Compare the output layer's prediction with the true answer to calculate the Error.
  3. Propagate this error backward from the CEO to the new employees.
  4. Each node (neuron) calculates its contribution to the error
    and slightly adjusts its decision criteria (weight W and bias b).
  5. Repeat this process over all data hundreds or thousands of times (Epochs) to minimize the error.

๐ŸŒธ Module 2: Revisiting the Iris Problem with an MLP

We will now solve the Iris classification problem from Week 10,
previously addressed with Decision Trees and Random Forests, using MLP.

[Important] Data Preprocessing for Deep Learning Models: Scaling

Decision Trees split on feature values (e.g., 5cm), making them less sensitive to scale,
while Neural Network models such as MLP are very sensitive to input data scale.

# Import required libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import (classification_report,
                             accuracy_score)

# 1. Load and prepare data
iris = load_iris()
X = iris.data   # Use all 4 features
y = iris.target
feature_names = iris.feature_names
target_names = iris.target_names

# 2. Split data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)

# 3. Scale data (important!)
# Create a StandardScaler object
scaler = StandardScaler()

# [Important] Fit scaler only on *Train data*!
# (Information from the 'real exam' data must not leak into training)
scaler.fit(X_train)

# Use the fitted scaler to transform both train/test data
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)

print("--- ์Šค์ผ€์ผ๋ง ์ „ Train ๋ฐ์ดํ„ฐ (์ผ๋ถ€) ---")
print(np.round(X_train[:5], 2))
print("\n--- ์Šค์ผ€์ผ๋ง ํ›„ Train ๋ฐ์ดํ„ฐ (์ผ๋ถ€) ---")
print(np.round(X_train_scaled[:5], 2))
--- ์Šค์ผ€์ผ๋ง ์ „ Train ๋ฐ์ดํ„ฐ (์ผ๋ถ€) ---
[[5.1 2.5 3.  1.1]
 [6.2 2.2 4.5 1.5]
 [5.1 3.8 1.5 0.3]
 [6.8 3.2 5.9 2.3]
 [5.7 2.8 4.1 1.3]]

--- ์Šค์ผ€์ผ๋ง ํ›„ Train ๋ฐ์ดํ„ฐ (์ผ๋ถ€) ---
[[-0.9  -1.22 -0.44 -0.14]
 [ 0.38 -1.88  0.4   0.38]
 [-0.9   1.64 -1.29 -1.17]
 [ 1.08  0.32  1.19  1.41]
 [-0.2  -0.56  0.18  0.12]]

Creating and Training an MLP Model

Now create MLPClassifier and train it on the scaled data.
The key is to design the model structure using the hidden_layer_sizes parameter.

# 1. Create an MLP model
# hidden_layer_sizes=(10, 5)
# : A first hidden layer with 10 neurons,
# and a second hidden layer with 5 neurons
# (Company analogy: 10 junior managers, 5 department managers)
# activation='relu' : Use ReLU as the nonlinear function
# max_iter=1000 : Repeat training (Backpropagation) up to 1000 times
# random_state=42 : Fix the seed for reproducible results
# solver='adam' : Optimization algorithm (default, efficient)
model_mlp = MLPClassifier(
    hidden_layer_sizes=(10, 5),
    activation='relu',
    max_iter=1000,
    random_state=42,
    solver='adam'
)

# 2. Train the model on 'scaled practice problems' (fit)
model_mlp.fit(X_train_scaled, y_train)

print("MLP ๋ชจ๋ธ ํ•™์Šต ์™„๋ฃŒ!")
MLP ๋ชจ๋ธ ํ•™์Šต ์™„๋ฃŒ!

Performance Evaluation and Comparison

Evaluate the trained MLP using scaled actual exam data.

# Make predictions on 'scaled real exam' data
y_pred_mlp = model_mlp.predict(X_test_scaled)

# Evaluate performance
accuracy_mlp = accuracy_score(y_test, y_pred_mlp)
report_mlp = classification_report(y_test, y_pred_mlp,
                                   target_names=target_names)

print("--- MLPClassifier ์„ฑ๋Šฅ ํ‰๊ฐ€ (Test Data) ---")
print(f"์ •ํ™•๋„: {accuracy_mlp:.4f}")
print("\nClassification Report:")
print(report_mlp)
--- MLPClassifier ์„ฑ๋Šฅ ํ‰๊ฐ€ (Test Data) ---
์ •ํ™•๋„: 0.9556

Classification Report:
              precision    recall  f1-score   support

      setosa       1.00      1.00      1.00        15
  versicolor       0.93      0.93      0.93        15
   virginica       0.93      0.93      0.93        15

    accuracy                           0.96        45
   macro avg       0.96      0.96      0.96        45
weighted avg       0.96      0.96      0.96        45

โœจ Wrap-up

Today, we learned about MLPs (Multilayer Perceptrons), a basic Deep Learning model.

Next time, we will use MLP to address the more complex problem of classifying high-dimensional image data.