πŸ€– AI/ML

Supervised Learning

Supervised Learning

A machine learning method that learns from labeled data. The most fundamental and widely used ML paradigm, solving classification and regression problems.

πŸ“– Detailed Explanation

Supervised Learning is a machine learning methodology that trains a model using a dataset in which input data (X) and correct-answer labels (Y) are given as pairs. Just as a teacher teaches a student by giving problems along with their answers, the model learns the relationship between inputs and outputs. When new data comes in, it can predict results based on the learned patterns.

The history of supervised learning goes back to the Perceptron of the 1950s. The Perceptron, developed by Frank Rosenblatt, was one of the first supervised learning algorithms. Afterward, as various algorithms such as decision trees, SVMs, and neural networks developed, supervised learning established itself as a core field of machine learning.

Supervised learning is broadly divided into two types: Classification and Regression. Classification predicts discrete categories, such as whether an email is spam or identifying objects in an image. Regression predicts continuous numerical values, such as housing prices or stock prices. During training, the model adjusts its parameters in the direction that minimizes the Loss Function.

In practice, supervised learning is the most widely used ML paradigm. Supervised learning is the foundation of most AI application areas, including medical diagnosis, credit scoring, image recognition, and natural language processing. Deep learning is also essentially an extension of supervised learning, and LLMs such as GPT also undergo supervised fine-tuning after pre-training.

πŸ’» Code Example

This is a classification and regression example using scikit-learn.

# Supervised Learning 기본 예제
from sklearn.datasets import load_iris, load_boston
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.metrics import accuracy_score, mean_squared_error, classification_report
import numpy as np

# ===== 1. λΆ„λ₯˜(Classification) 예제 =====
print("=" * 50)
print("λΆ„λ₯˜(Classification) - Iris 데이터셋")
print("=" * 50)

# 데이터 λ‘œλ“œ
iris = load_iris()
X_cls, y_cls = iris.data, iris.target

# ν•™μŠ΅/ν…ŒμŠ€νŠΈ λΆ„ν•  (80% ν•™μŠ΅, 20% ν…ŒμŠ€νŠΈ)
X_train, X_test, y_train, y_test = train_test_split(
    X_cls, y_cls, test_size=0.2, random_state=42
)

# λͺ¨λΈ ν•™μŠ΅
classifier = RandomForestClassifier(n_estimators=100, random_state=42)
classifier.fit(X_train, y_train)  # 지도 ν•™μŠ΅: X와 yλ₯Ό ν•¨κ»˜ 전달

# 예츑 및 평가
y_pred = classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)

print(f"정확도(Accuracy): {accuracy:.4f}")
print(f"ν…ŒμŠ€νŠΈ μƒ˜ν”Œ 수: {len(y_test)}")
print("\nλΆ„λ₯˜ 리포트:")
print(classification_report(y_test, y_pred, target_names=iris.target_names))

# ===== 2. νšŒκ·€(Regression) 예제 =====
print("\n" + "=" * 50)
print("νšŒκ·€(Regression) - ν•©μ„± 데이터")
print("=" * 50)

# νšŒκ·€μš© ν•©μ„± 데이터 생성
np.random.seed(42)
X_reg = np.random.rand(200, 4) * 10  # 200개 μƒ˜ν”Œ, 4개 νŠΉμ„±
y_reg = 3*X_reg[:, 0] + 2*X_reg[:, 1] - X_reg[:, 2] + np.random.randn(200) * 2

# ν•™μŠ΅/ν…ŒμŠ€νŠΈ λΆ„ν• 
X_train_r, X_test_r, y_train_r, y_test_r = train_test_split(
    X_reg, y_reg, test_size=0.2, random_state=42
)

# νšŒκ·€ λͺ¨λΈ ν•™μŠ΅
regressor = RandomForestRegressor(n_estimators=100, random_state=42)
regressor.fit(X_train_r, y_train_r)

# 예츑 및 평가
y_pred_r = regressor.predict(X_test_r)
mse = mean_squared_error(y_test_r, y_pred_r)
rmse = np.sqrt(mse)

print(f"평균 제곱 였차(MSE): {mse:.4f}")
print(f"평균 제곱근 였차(RMSE): {rmse:.4f}")
print(f"νŠΉμ„± μ€‘μš”λ„: {regressor.feature_importances_.round(3)}")

# ===== 3. μƒˆλ‘œμš΄ 데이터 예츑 =====
print("\n" + "=" * 50)
print("μƒˆλ‘œμš΄ 데이터 예츑")
print("=" * 50)

# μƒˆλ‘œμš΄ 뢓꽃 λ°μ΄ν„°λ‘œ ν’ˆμ’… 예츑
new_sample = [[5.1, 3.5, 1.4, 0.2]]  # setosa νŠΉμ„±
prediction = classifier.predict(new_sample)
probability = classifier.predict_proba(new_sample)

print(f"μž…λ ₯ νŠΉμ„±: {new_sample[0]}")
print(f"예츑 ν’ˆμ’…: {iris.target_names[prediction[0]]}")
print(f"예츑 ν™•λ₯ : {dict(zip(iris.target_names, probability[0].round(3)))}")

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"We have enough labeled data for this problem, so let's approach it with Supervised Learning. Let's aim for an accuracy of 95% or higher for the classification model, set a baseline with RandomForest first, and then improve it with XGBoost."
πŸ’¬ In an Interview
"The key difference between Supervised Learning and Unsupervised Learning is whether labels exist. Supervised learning trains on data with correct answers to make predictions, while unsupervised learning finds patterns in the data itself. In practice, supervised learning is used most of the time, but if labeling costs are high, we consider Semi-supervised or Self-supervised learning."
πŸ’¬ In a Technical Discussion
"Preventing overfitting is important in supervised learning. You need to split the data into Train/Validation/Test, select models with Cross-validation, and apply Early stopping or Regularization. In particular, K-fold CV is essential when data is limited."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Data Leakage
If test data information is included in training, performance will be overestimated compared to actual performance. Fit preprocessing (scaling, encoding) only on the training data, and apply only transform to the test data.
❌
Ignoring Class Imbalance
When positive samples are 1%, as in anomaly detection, simple accuracy is meaningless. Check Precision, Recall, and F1-score, and apply SMOTE or class weight adjustment.
βœ…
Correct Approach
1) Preprocess after splitting the data, 2) select appropriate evaluation metrics, 3) validate the model with Cross-validation, 4) use the Validation set when tuning hyperparameters, 5) measure final performance on the Test set only once.

πŸ”— Related Terms

πŸ“š Learn More