{% extends "base.html" %} {% block title %}Supervised Learning - Data Science Lab{% endblock %} {% block content %}

📖 What is Supervised Learning?

Supervised learning trains models on labeled data — each input has a known output. The model learns the mapping from features (X) to target (y).

TaskOutputExampleAlgorithm
RegressionContinuous numberPredict property priceLinear Regression, Random Forest
ClassificationCategory labelClassify feedback sentimentLogistic Regression, SVM

🚀 Train Price Models

Train regression models on housing data. Make sure data is loaded first (run pipeline from the Pipeline page).

🔮 Predict housing Price

📖 Model Evaluation

Python
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error

# After training
y_pred = model.predict(X_test)

# Regression metrics
mse = mean_squared_error(y_test, y_pred)
rmse = mse ** 0.5
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"RMSE: ${rmse:,.0f}")    # Average prediction error in dollars
print(f"MAE:  ${mae:,.0f}")     # Median-like error
print(f"R²:   {r2:.4f}")       # % of variance explained (1.0 = perfect)

# Interpretation for price model:
# RMSE = $45,000 means our predictions are ±$45K on average
# R² = 0.82 means 82% of price variation is explained by our features

Train/Test Split — Why It Matters

Python
from sklearn.model_selection import train_test_split, cross_val_score

# Split: 80% train, 20% test
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Better: K-Fold Cross Validation
scores = cross_val_score(model, X, y, cv=5, scoring='r2')
print(f"CV R² scores: {scores}")
print(f"Mean R²: {scores.mean():.4f} ± {scores.std():.4f}")

📜 Model Training History

ModelR²RMSEMAETrained
⚠️ Common Mistakes
🧪 Exercises

1. Train both models and compare their R² and RMSE. Which performs better?

2. Predict prices for the same flat in different towns. What's the price premium for central locations?

3. Why might the model perform poorly for PREMIUM flats?

{% endblock %}