| Property | Absolute Loss | Squared Loss |
|---|---|---|
| Formula | \(|y-\hat{y}|\) | \((y-\hat{y})^2\) |
| Also called | L1 loss | L2 loss |
| Penalizes large errors | Proportionally | Much more heavily |
| Sensitive to outliers | Lower | Higher |
| Common metric | MAE | MSE / RMSE |
\[ L_\delta(e) = \begin{cases} \frac{1}{2}e^2, & |e| \leq \delta \\ \delta\left(|e|-\frac{1}{2}\delta\right), & |e| > \delta \end{cases} \]
| Loss Function | Key Property | When to Consider |
|---|---|---|
| Absolute (L1) | Linear penalty | Robust to outliers |
| Squared (L2) | Large errors heavily penalized | Standard linear regression |
| Huber | Squared near 0, linear for large errors | Want robustness + smooth optimization |
| Quantile | Asymmetric penalties | Predicting specific quantiles |
scikit-learnAbsolute loss:
Squared loss:
Quantile loss:
Huber regression:
scikit-learnfrom sklearn.metrics import (
mean_absolute_error,
mean_squared_error,
mean_pinball_loss
)
y_pred = model.predict(X)
mae = mean_absolute_error(y, y_pred)
mse = mean_squared_error(y, y_pred)
pinball = mean_pinball_loss(y, y_pred, alpha=0.75)
print("MAE:", mae)
print("MSE:", mse)
print("Pinball loss:", pinball)HuberRegressor() fits a linear model using Huber loss.from sklearn.linear_model import LinearRegression, HuberRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error
model_lr = LinearRegression()
model_huber = HuberRegressor()
model_lr.fit(X, y)
model_huber.fit(X, y)
y_pred_lr = model_lr.predict(X)
y_pred_huber = model_huber.predict(X)
print("Linear Regression MSE:",
mean_squared_error(y, y_pred_lr))
print("Huber Regression MSE:",
mean_squared_error(y, y_pred_huber))Squared loss
Huber loss
Select all of the following statements which are TRUE.
| Scenario | Preferred Loss |
|---|---|
| Outliers are common and should have limited influence. | |
| Large errors are especially costly. | |
| You want a smooth loss that is robust to outliers. | |
| You want to predict the 90th percentile of a target. |
| Scenario | Preferred Loss |
|---|---|
| Outliers are common and should have limited influence. | Absolute Loss (MAE) |
| Large errors are especially costly. | Squared Loss (MSE) |
| You want a smooth loss that is robust to outliers. | Huber Loss |
| You want to predict the 90th percentile of a target. | Quantile Loss (τ = 0.90) |
scikit-learn, these ideas are available through mean_absolute_error(), mean_squared_error(), mean_pinball_loss(), and HuberRegressor().| Scenario | Data Imbalance | Main Concern | Best Metric(s) / Curve |
|---|---|---|---|
| Email Spam Detection | 10% spam | Avoid false positives | |
| Disease Screening | 1 in 10,000 | Avoid false negatives | |
| Credit Card Fraud | 0.1% fraud | Focus on rare positive class | |
| Customer Churn | 20% churn | Balance FP & FN | |
| Sentiment Analysis | 50/50 balanced | Overall correctness | |
| Face Recognition | Balanced pairs | Trade-off FP vs FN |
| Metric / Plot | When to Use | Why |
|---|---|---|
| Precision, Recall, F1 | When you care about specific error types (FP vs FN) or a fixed threshold. | Focus on particular tradeoffs. |
| PR Curve & AP Score | When the dataset is highly imbalanced (rare positives). | Ignores TNs; focuses on positives. |
| ROC Curve & AUC | When classes are moderately imbalanced. | Measures ranking ability across thresholds. |
AUC–ROC measures the probability that a randomly chosen positive example receives a higher score than a randomly chosen negative example.
class weight="balanced" (preferred method for this course)alpha hyperparameter controls model complexity.alpha.alpha hyperparameteralpha:
alpha: Simpler model, smaller coefficients.alpha: Complex model, larger coefficients.r2_scoreTransformedTargetRegressor
| Scenario | What matters most? | Best metric(s)? |
|---|---|---|
| Predicting house prices ranging from $60K–$800K. | A $30K error is huge for a $60K house but small for a $500K house. | |
| Predicting exam scores (0–100). | You want an interpretable measure of average error in points. | |
| Predicting energy consumption in a large industrial system. | Large errors are very costly and should be penalized heavily. | |
| Predicting insurance claim amounts. | You want to compare how well different models explain the variation in claims. |
| Scenario | What matters most? | Best metric(s)? |
|---|---|---|
| Predicting house prices ranging from $60K–$800K. | A $30K error is huge for a $60K house but small for a $500K house. | MAPE (Mean Absolute Percentage Error) |
| Predicting exam scores (0–100). | You want an interpretable measure of average error in points. | MAE (Mean Absolute Error) |
| Predicting energy consumption in a large industrial system. | Large errors are very costly and should be penalized heavily. | MSE (Mean Squared Error) or RMSE |
| Predicting insurance claim amounts. | You want to compare how well different models explain the variation in claims. | R² (R-squared) |
Slides adapted from Firas Moosvi