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.
Supervised Learning
A machine learning method that learns from labeled data. The most fundamental and widely used ML paradigm, solving classification and regression problems.
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.
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)))}")
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free