[Week 1] Understanding Machine Learning: Starting to Think with Data ๐ค
English translation
Hello! Welcome to the first session of Introduction to Machine Learning.
Today, we will explore the big picture of what Machine Learning is and its different types.
Set aside complex equations and code for now, and follow along with a focus on concepts and ideas!
๐ง Module 1: Machine Learning vs. Traditional Programming
- Traditional Programming
- A human supplies rules and data to the computer, which outputs the answer.
- Example: "If an email subject contains 'advertisement', classify it as spam!"
- Machine Learning
- Supply data and their corresponding answers to the computer, which discovers rules on its own.
- We call these 'rules' a Model.
- Example: Show tens of thousands of spam and legitimate emails, and the machine learns new rules to distinguish spam.
๐ Module 2: Three Types of Machine Learning
1. Supervised Learning (Supervised Learning)
- Key Idea
- Learn through practice (Training Data) and evaluate with an exam (Test Data).
- The Supervised Learning Process
- Learn from practice problems with 'inputs' and 'answers'.
- After learning, evaluate performance on exam problems never seen during practice.
- This checks whether the model has acquired problem-solving ability rather than simply memorizing answers.
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
# Hide unnecessary warnings
import warnings
warnings.filterwarnings(action='ignore')
# Generate the full dataset
X, y = make_blobs(n_samples=200, centers=2, random_state=2, cluster_std=1.5)
# Split the data 7:3 into 'practice problems (train)' and an 'actual exam (test)'
(X_train, X_test,
y_train, y_test) = train_test_split(X, y, test_size=0.3, random_state=42)
# Visualize the problem (the model can see only the 'practice problems')
plt.figure(figsize=(8, 6))
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap='coolwarm', s=60)
plt.title("Training Data Visualization")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
print(f"์ด ๋ฐ์ดํฐ: {len(X)}๊ฐ")
print(f"์ฐ์ต ๋ฌธ์ : {len(X_train)}๊ฐ")
print(f"์ค์ ์ํ: {len(X_test)}๊ฐ")<Figure size 800x600 with 1 Axes>
์ด ๋ฐ์ดํฐ: 200๊ฐ ์ฐ์ต ๋ฌธ์ : 140๊ฐ ์ค์ ์ํ: 60๊ฐ
# A human examines the 'practice problems' and creates a rule.
# "If feature 1 (x-axis) is less than 2.5, assign group 0; if greater, assign group 1!"
# Apply this rule to the 'actual exam' data.
rule_based_pred = (X_test[:, 0] > 2.5).astype(int)
# Visualize the results
plt.figure(figsize=(8, 6))
# Display 'actual exam' samples colored by rule-based predictions
plt.scatter(X_test[:, 0], X_test[:, 1], c=rule_based_pred, cmap='coolwarm', s=60)
plt.axvline(x=2.5, color='green', linestyle='--', label='My Rule (x=2.5)')
plt.title("Rule-based Classification Result")
plt.legend()
plt.show()
# Calculate Accuracy by comparing with the true answers for the 'actual exam'
accuracy = np.mean(rule_based_pred == y_test)
print(f"๊ท์น ๊ธฐ๋ฐ์ ์ค์ ์ํ ์ ํ๋: {accuracy * 100:.2f}%")<Figure size 800x600 with 1 Axes>
๊ท์น ๊ธฐ๋ฐ์ ์ค์ ์ํ ์ ํ๋: 51.67%
from sklearn.linear_model import LogisticRegression
# The Machine Learning model trains only on the 'practice problem' data.
model = LogisticRegression()
model.fit(X_train, y_train)
# Visualize and compare with the rule-based approach
plt.figure(figsize=(10, 7))
# Color the 'actual exam' data by their true answers
plt.scatter(X_test[:, 0], X_test[:, 1],
c=y_test, cmap='coolwarm',
s=60, alpha=0.7)
plt.axvline(x=2.5, color='green', linestyle='--', label='Rule-based')
ax = plt.gca()
xlim = ax.get_xlim()
w = model.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(xlim[0], xlim[1])
yy = a * xx - (model.intercept_[0]) / w[1]
plt.plot(xx, yy, c='black', label='Machine Learning')
plt.title("Machine Learning vs. Rule-based")
plt.legend()
plt.show()
accuracy = model.score(X_test, y_test)
print(f"๋จธ์ ๋ฌ๋์ ์ค์ ์ํ ์ ํ๋: {accuracy * 100:.2f}% (์ฒ์ ๋ณด๋ ๋ฌธ์ ๋ ๋ ์ ํด๊ฒฐํจ)")<Figure size 1000x700 with 1 Axes>
๋จธ์ ๋ฌ๋์ ์ค์ ์ํ ์ ํ๋: 100.00% (์ฒ์ ๋ณด๋ ๋ฌธ์ ๋ ๋ ์ ํด๊ฒฐํจ)
2. Unsupervised Learning (Unsupervised Learning)
- Core idea
- Learning only from data without ground truth labels.
- The Unsupervised Learning Process
- Use practice data to identify hidden structure (e.g., group centers).
- Use this structure to assign new data from test problems to groups.
from sklearn.cluster import KMeans
# Generate and split the full dataset
X_un, _ = make_blobs(n_samples=300, centers=3,
random_state=1, cluster_std=1.8)
X_train_un, X_test_un, _, _ = train_test_split(X_un, np.zeros(300),
test_size=0.3, random_state=42)
# Visualize the problem (show only the 'practice problems')
plt.figure(figsize=(8, 6))
plt.scatter(X_train_un[:, 0], X_train_un[:, 1], c='gray', s=60)
plt.title("Traning Data Visualization")
plt.show()<Figure size 800x600 with 1 Axes>
# A human examines the 'practice problems' and arbitrarily chooses group centers.
manual_centers = np.array([[-10, 0], [-2, 8], [0, 0]])
# Classify the 'actual exam' data using these centers.
distances = np.sqrt(((X_test_un - manual_centers[:, np.newaxis])**2).sum(axis=2))
manual_pred = np.argmin(distances, axis=0)
# Visualize the results
plt.figure(figsize=(8, 6))
plt.scatter(X_test_un[:, 0], X_test_un[:, 1],
c=manual_pred, cmap='viridis', s=60)
plt.scatter(manual_centers[:, 0], manual_centers[:, 1],
s=200, c='red', marker='X', label='My Centers')
plt.title("Manual Attempt")
plt.legend()
plt.show()<Figure size 800x600 with 1 Axes>
# Learn group centers from the 'practice problems'
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X_train_un)
# Use the trained model to predict the group of each 'actual exam' sample
ml_pred = kmeans.predict(X_test_un)
# Visualize and compare with the manual approach
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))
# Left: Manual approach results on the actual exam
ax1.scatter(X_test_un[:, 0], X_test_un[:, 1], c=manual_pred, cmap='viridis', s=60)
ax1.scatter(manual_centers[:, 0], manual_centers[:, 1],
s=200, c='red', marker='X')
ax1.set_title("Manual Attempt")
# Right: Machine Learning results on the actual exam
ax2.scatter(X_test_un[:, 0], X_test_un[:, 1], c=ml_pred, cmap='viridis', s=60)
ax2.scatter(kmeans.cluster_centers_[:, 0],
kmeans.cluster_centers_[:, 1],
s=200, c='red', marker='X')
ax2.set_title("Machine Learning")
plt.show()
print("๋จธ์ ๋ฌ๋์ด '์ฐ์ต ๋ฌธ์ '์์ ํ์ตํ ์ ํํ ์ค์ฌ์ ์ ๊ธฐ์ค์ผ๋ก '์ค์ ์ํ' ๋ฌธ์ ๋ค๋ ๋ ์ ๋ถ๋ฅํฉ๋๋ค.")<Figure size 1600x600 with 2 Axes>
๋จธ์ ๋ฌ๋์ด '์ฐ์ต ๋ฌธ์ '์์ ํ์ตํ ์ ํํ ์ค์ฌ์ ์ ๊ธฐ์ค์ผ๋ก '์ค์ ์ํ' ๋ฌธ์ ๋ค๋ ๋ ์ ๋ถ๋ฅํฉ๋๋ค.
3. Reinforcement Learning
- Core idea
- Learning not from 'correct answers', but from 'rewards'.
- The Reinforcement Learning Process
- Through extensive trial and error in a Training Environment, learn optimal action rules (a Policy).
- Apply this policy to real situations (Test Cases) to choose optimal actions.
# Visualize the agent's 'training environment' (the same as supervised learning's train data)
plt.figure(figsize=(8, 6))
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train,
cmap='coolwarm', s=60, alpha=0.5)
plt.title("Training Data Visualization")
plt.show()
print("์์ด์ ํธ๋ ์ด ํ๊ฒฝ ์์์ ์๋ง์ ์ ๋ค์ ๋ง๋๋ฉฐ ๋ณด์/๋ฒ์ ์ ํตํด ํ์ตํฉ๋๋ค.")<Figure size 800x600 with 1 Axes>
์์ด์ ํธ๋ ์ด ํ๊ฒฝ ์์์ ์๋ง์ ์ ๋ค์ ๋ง๋๋ฉฐ ๋ณด์/๋ฒ์ ์ ํตํด ํ์ตํฉ๋๋ค.
# Implement simple Reinforcement Learning using the Q-learning idea
# 1. Define States: Divide the space into a grid
grid_x = np.linspace(X_train[:, 0].min(), X_train[:, 0].max(), 10)
grid_y = np.linspace(X_train[:, 1].min(), X_train[:, 1].max(), 10)
# 2. Initialize the Q-table: Store the value of each action (0 or 1) in each grid cell (State)
# Initialize all action values to 0
q_table = np.zeros((len(grid_x), len(grid_y), 2)) # (x-grid cell, y-grid cell, action[0,1])
# 3. Training parameters
learning_rate = 0.1
n_episodes = 1000 # Number of training iterations
# 4. Start training
for episode in range(n_episodes):
# Randomly select one practice sample
idx = np.random.randint(len(X_train))
state_pos, true_label = X_train[idx], y_train[idx]
# Find the grid cell containing the current position
state_x_idx = np.digitize(state_pos[0], grid_x) - 1
state_y_idx = np.digitize(state_pos[1], grid_y) - 1
# Select an action (choose the one with the higher value)
action = np.argmax(q_table[state_x_idx, state_y_idx])
# Calculate the reward
reward = 1 if action == true_label else -1
# Update the Q-table: Gradually move the selected action's value toward the reward
# Q(s,a) <- Q(s,a) + lr * (reward - Q(s,a))
current_q = q_table[state_x_idx, state_y_idx, action]
new_q = current_q + learning_rate * (reward - current_q)
q_table[state_x_idx, state_y_idx, action] = new_q
print("๊ฐํํ์ต ์๋ฃ! ๊ฐ ๊ณต๊ฐ ์์ญ๋ณ ์ต์ ํ๋(์ ์ฑ
)์ด Q-ํ
์ด๋ธ์ ์ ์ฅ๋์์ต๋๋ค.")
# Visualize the learned Policy
learned_policy = np.argmax(q_table, axis=2)
plt.figure(figsize=(8, 6))
plt.imshow(learned_policy.T, origin='lower',
extent=[grid_x[0], grid_x[-1], grid_y[0], grid_y[-1]],
aspect='auto', cmap='coolwarm', alpha=0.7)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap='coolwarm', s=20)
plt.title("Tranined Q-table Visualization")
plt.show()๊ฐํํ์ต ์๋ฃ! ๊ฐ ๊ณต๊ฐ ์์ญ๋ณ ์ต์ ํ๋(์ ์ฑ )์ด Q-ํ ์ด๋ธ์ ์ ์ฅ๋์์ต๋๋ค.
<Figure size 800x600 with 1 Axes>
# A 'real situation' occurs: An unseen point appears (select one from the test data)
test_case_pos = X_test[1]
test_case_label = y_test[1]
# Use the learned Q-table (Policy) to choose the optimal action
test_x_idx = np.digitize(test_case_pos[0], grid_x) - 1
test_y_idx = np.digitize(test_case_pos[1], grid_y) - 1
learned_action = np.argmax(q_table[test_x_idx, test_y_idx])
# Visualize the results
fig, ax = plt.subplots(figsize=(8, 6))
# Background showing the learned Policy
ax.imshow(learned_policy.T, origin='lower',
extent=[grid_x[0], grid_x[-1], grid_y[0], grid_y[-1]],
aspect='auto', cmap='coolwarm', alpha=0.7)
# Location of the actual test sample
ax.scatter(test_case_pos[0], test_case_pos[1], c='green', s=200, marker='*')
ax.set_title("Reinforcement Learning Result")
if learned_action == test_case_label:
reward_text, reward_color = "+1 ๐", 'blue'
else:
reward_text, reward_color = "-1 ๐ฅ", 'red'
ax.text(test_case_pos[0], test_case_pos[1] + 0.5,
f"Action: Predict {'Blue' if learned_action==0 else 'Red'}"
+f"\nResult: {reward_text}",
ha='center', fontsize=15, color=reward_color)
plt.show()
print("์๋ง์ ๋ณด์/๋ฒ์ ๊ฒฝํ์ผ๋ก ํ์ต๋ ์ ์ฑ
์,")
print(f"์ฒ์ ๋ณด๋ ์ํฉ์์๋ ๋ณด์์ ๋ฐ์ ํ๋ฅ ์ด ๊ฐ์ฅ ๋์ ์ต์ ์ ํ๋์ ํฉ๋๋ค.")<Figure size 800x600 with 1 Axes>
์๋ง์ ๋ณด์/๋ฒ์ ๊ฒฝํ์ผ๋ก ํ์ต๋ ์ ์ฑ ์, ์ฒ์ ๋ณด๋ ์ํฉ์์๋ ๋ณด์์ ๋ฐ์ ํ๋ฅ ์ด ๊ฐ์ฅ ๋์ ์ต์ ์ ํ๋์ ํฉ๋๋ค.
๐ฆ Module 2.5: What Is a 'Model', the Core of Machine Learning?
While exploring supervised, unsupervised, and reinforcement learning, we have repeatedly used the words 'model' and 'agent'.
The model is the most important concept in Machine Learning.
- A simple definition of a model
- A 'set of rules' or 'mathematical function' that learns patterns in data to make predictions or decisions about new data.
- 'A Box Containing Decision Criteria'
- Feed new data into this box (model) as Input,
- and it produces a prediction as Output according to its learned internal criteria (rules).
- Goals of Machine Learning
- Build the optimal box (model) to best explain and predict the data. (Modeling)
- Automatically adjust its internal decision criteria (Parameters) using data. (Training)
- Use the well-built box to predict future data. (Inference)
From now on, using what is called 'Linear Regression' (Linear Regression),
We will learn what the world's simplest 'model box' looks like and how it learns.
# Represent a 'model' as a simple Python 'function'.
# This model takes 'study hours' as input and predicts 'whether the student passes the exam'.
def simple_pass_fail_model(study_hours):
"""
์ด ํจ์ ์์ฒด๊ฐ ํ๋์ ๊ฐ๋จํ '๋ชจ๋ธ'์
๋๋ค.
๋ด๋ถ์๋ '4์๊ฐ ์ด์ ๊ณต๋ถํ๋ฉด ํต๊ณผ'๋ผ๋ ๊ท์น(ํ๋จ ๊ธฐ์ค)์ด ์์ต๋๋ค.
"""
# The model's decision criterion (Rule)
if study_hours > 4:
return "Pass"
else:
return "Fail"
# Example model use (Inference)
new_student_A_hours = 7
new_student_B_hours = 3
prediction_A = simple_pass_fail_model(new_student_A_hours)
prediction_B = simple_pass_fail_model(new_student_B_hours)
print(f"ํ์ A (๊ณต๋ถ ์๊ฐ: {new_student_A_hours}์๊ฐ) -> ์์ธก ๊ฒฐ๊ณผ: {prediction_A}")
print(f"ํ์ B (๊ณต๋ถ ์๊ฐ: {new_student_B_hours}์๊ฐ) -> ์์ธก ๊ฒฐ๊ณผ: {prediction_B}")ํ์ A (๊ณต๋ถ ์๊ฐ: 7์๊ฐ) -> ์์ธก ๊ฒฐ๊ณผ: Pass ํ์ B (๊ณต๋ถ ์๊ฐ: 3์๊ฐ) -> ์์ธก ๊ฒฐ๊ณผ: Fail
๐ Module 3: My First Machine Learning Model, "Linear Regression"
Why a 'Linear' Model?
- The simplest and most intuitive model
- Although many real-world phenomena are very complex,
- approximating their relationships with the simplest form, a straight line, is the starting point of analysis.
- Easy to interpret
- It is especially easy to understand why the model made a prediction.
- e.g., "With 1 more hour of study, the score will increase by W points."
- It is especially easy to understand why the model made a prediction.
- The Foundation for Everything
- Understanding this simple model helps you see why more complex Deep Learning models developed as they did.
'Linear' has the same meaning as the 1st-degree function learned in middle school.
(In Machine Learning, only the variable names change.)
- Hypothesis
- Cost Function
- Goal
- Find and that minimize !
# Generate and split exam-score data based on study hours
study_hours = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
exam_scores = np.array([15, 35, 45, 50, 65, 70, 85, 90, 92, 98])
(X_train_reg, X_test_reg,
y_train_reg, y_test_reg) = train_test_split(study_hours,
exam_scores,
test_size=0.3,
random_state=42)
# Define 3 candidate models
W1, b1 = 5, 20 # A poor model
W2, b2 = 12, -5 # A mediocre model
W3, b3 = 9.5, 8 # A good model
# Calculate each model's Cost on the 'practice problem' data
pred1_train = W1 * X_train_reg + b1
pred2_train = W2 * X_train_reg + b2
pred3_train = W3 * X_train_reg + b3
cost1_train = np.mean((pred1_train - y_train_reg)**2)
cost2_train = np.mean((pred2_train - y_train_reg)**2)
cost3_train = np.mean((pred3_train - y_train_reg)**2)
# Visualize the 'practice problems'
plt.figure(figsize=(10, 7))
plt.scatter(X_train_reg, y_train_reg, label='Train Data', s=100)
plt.plot(study_hours, W1 * study_hours + b1,
'r--', label=f'Model 1 (Train Cost: {cost1_train:.2f})')
plt.plot(study_hours, W2 * study_hours + b2,
'g:', label=f'Model 2 (Train Cost: {cost2_train:.2f})')
plt.plot(study_hours, W3 * study_hours + b3,
'b-', label=f'Model 3 (Train Cost: {cost3_train:.2f})')
plt.title("Finding a good model")
plt.xlabel("Study Hours")
plt.ylabel("Exam Scores")
plt.legend()
plt.show()
print("์ฐ์ต ๋ฌธ์ ์์๋ ๋ชจ๋ธ 3์ Cost๊ฐ ๊ฐ์ฅ ๋ฎ์ผ๋ฏ๋ก, ๋ชจ๋ธ 3์ ์ต์ข
๋ชจ๋ธ๋ก ์ ํํฉ๋๋ค.")<Figure size 1000x700 with 1 Axes>
์ฐ์ต ๋ฌธ์ ์์๋ ๋ชจ๋ธ 3์ Cost๊ฐ ๊ฐ์ฅ ๋ฎ์ผ๋ฏ๋ก, ๋ชจ๋ธ 3์ ์ต์ข ๋ชจ๋ธ๋ก ์ ํํฉ๋๋ค.
# Calculate each model's Cost on the 'actual exam' data
pred1_test = W1 * X_test_reg + b1
pred2_test = W2 * X_test_reg + b2
pred3_test = W3 * X_test_reg + b3
cost1_test = np.mean((pred1_test - y_test_reg)**2)
cost2_test = np.mean((pred2_test - y_test_reg)**2)
cost3_test = np.mean((pred3_test - y_test_reg)**2)
# Visualize the 'actual exam'
plt.figure(figsize=(10, 7))
# Show the 'practice problems' faintly in the background
plt.scatter(X_train_reg, y_train_reg,
color='gray', alpha=0.3, label='Train Data')
# Show the 'actual exam' data prominently
plt.scatter(X_test_reg, y_test_reg,
color='blue', label='Test Data', s=100, zorder=5)
# Compare the same 3 candidate lines with the 'actual exam' data
plt.plot(study_hours, W1 * study_hours + b1,
'r--', label=f'Model 1 (Test Cost: {cost1_test:.2f})')
plt.plot(study_hours, W2 * study_hours + b2,
'g:', label=f'Model 2 (Test Cost: {cost2_test:.2f})')
plt.plot(study_hours, W3 * study_hours + b3,
'b-', label=f'Model 3 (Test Cost: {cost3_test:.2f})')
plt.title("Testing a model")
plt.xlabel("Study Hours")
plt.ylabel("Exam Scores")
plt.legend()
plt.show()
print("โจ ๊ฒฐ๊ณผ: ์ฐ์ต ๋ฌธ์ ์์ ๊ฐ์ฅ ์ข์๋ ๋ชจ๋ธ 3์ด,")
print(f"์ฒ์ ๋ณด๋ ์ค์ ์ํ์์๋ ๊ฐ์ฅ ๋ฎ์ Cost๋ฅผ ๊ธฐ๋กํ์ต๋๋ค!")
print("\n๋ชจ๋ธ์ด ๋ฐ์ดํฐ๋ฅผ '์๊ธฐ'ํ ๊ฒ์ด ์๋๋ผ, ๊ด๊ณ๋ฅผ 'ํ์ต(์ผ๋ฐํ)'ํ๋ค๋ ์ฆ๊ฑฐ์
๋๋ค.")<Figure size 1000x700 with 1 Axes>
โจ ๊ฒฐ๊ณผ: ์ฐ์ต ๋ฌธ์ ์์ ๊ฐ์ฅ ์ข์๋ ๋ชจ๋ธ 3์ด, ์ฒ์ ๋ณด๋ ์ค์ ์ํ์์๋ ๊ฐ์ฅ ๋ฎ์ Cost๋ฅผ ๊ธฐ๋กํ์ต๋๋ค! ๋ชจ๋ธ์ด ๋ฐ์ดํฐ๋ฅผ '์๊ธฐ'ํ ๊ฒ์ด ์๋๋ผ, ๊ด๊ณ๋ฅผ 'ํ์ต(์ผ๋ฐํ)'ํ๋ค๋ ์ฆ๊ฑฐ์ ๋๋ค.