Supervised Learning - Regression Algorithms

Supervised learning uses labeled examples to learn a mapping from input features to an output. In regression, that output is a continuous numerical value rather than a discrete class. Common targets include house prices, energy demand, temperature, customer lifetime value, and the number of days a user remains active.

A regression model estimates a function that predicts a real-valued target from observed variables. Its quality depends on two central questions: whether the model captures meaningful patterns in the data, and whether its errors remain small and predictable on unseen examples.

Unlike classification, which focuses on class boundaries, regression requires models to estimate the magnitude of an outcome. This makes evaluation, metric selection, and residual analysis especially important.

Loss Functions and Training Objectives

Most regression models are trained by minimizing a loss function that quantifies the difference between predicted and observed values.

Two widely used choices are:

  • Mean Squared Error (MSE) averages the squared residuals:

    $$
    \text{MSE}=\frac{1}{n}\sum_{i=1}^{n}(y_i-\hat{y}_i)^2
    $$

    Squaring gives larger errors extra influence, which can help optimize models but also makes MSE sensitive to outliers.

  • Mean Absolute Error (MAE) averages the absolute residuals:

    $$
    \text{MAE}=\frac{1}{n}\sum_{i=1}^{n}|y_i-\hat{y}_i|
    $$

    MAE is generally more robust when extreme observations are common or when large errors are especially costly.

The right metric should reflect the business cost of mistakes. For example, underestimating demand may be more damaging than overestimating it. In such cases, a custom loss function or weighted error measure may be more appropriate than relying only on MSE or MAE.

Linear Regression and Regularized Variants

Linear regression is the natural starting point for regression modeling. It assumes that the target can be approximated as a weighted combination of the input features:

$$
\hat{y}=w_1x_1+w_2x_2+\cdots+w_nx_n+b
$$

Here, each coefficient represents the expected change in the prediction associated with a one-unit change in a feature, while $b$ is the intercept. Linear regression is fast, easy to inspect, and provides a useful baseline for more complex approaches.

Its main limitation is its reliance on an approximately linear relationship. When important patterns are curved or depend on interactions among features, a more flexible model may be needed.

Regularization introduces a penalty on model complexity:

  • Ridge regression adds an L2 penalty based on the sum of squared coefficients. It stabilizes estimates and can reduce sensitivity to noisy or highly correlated features.
  • Lasso regression adds an L1 penalty based on the sum of absolute coefficients. Because the penalty can drive some coefficients to exactly zero, Lasso also performs automatic feature selection.
  • Elastic Net combines L1 and L2 penalties. It is often useful when the dataset contains many correlated variables and a sparse solution is desirable.

The regularization parameter controls the trade-off between fitting the training data and maintaining generalization. Stronger regularization produces simpler models, while weaker regularization allows more flexible fits.

Modeling Nonlinear Relationships

When the target depends on nonlinear patterns, several alternatives can extend a model beyond a straight-line relationship.

Polynomial regression expands the feature space with powers and interactions, such as $x^2$, $x^3$, or $x_1x_2$. It can represent smooth curves effectively, but high-degree terms may create unstable behavior outside the training range. Regularization and careful validation are therefore essential.

Support Vector Regression (SVR) uses the same core principles as support vector machines while adapting them to continuous targets. It attempts to fit the target within an error tube of width $\epsilon$, penalizing predictions that fall outside it. SVR can perform well in moderate- to high-dimensional settings, although feature scaling and careful tuning are important.

Decision trees split the feature space into smaller regions, making them capable of learning nonlinear interactions without requiring explicit polynomial terms. A single tree, however, may be unstable and prone to overfitting. Tree-based ensembles address this limitation:

  • Random forests train many decorrelated trees and average their predictions.
  • Gradient boosting methods, including XGBoost, LightGBM, and CatBoost, build trees sequentially so that each new tree corrects the errors of the existing ensemble.

For structured tabular data, gradient boosting models are often strong defaults. They usually handle mixed feature types, missing values, and complex interactions with relatively little preprocessing. Their trade-off is reduced transparency, greater computational cost, and more parameters to tune.

Evaluation Beyond the Training Score

A high training score does not guarantee useful predictions. Regression models should be evaluated on data that was not used to fit them. Depending on the dataset, this may involve a held-out test set, k-fold cross-validation, or time-based splitting when observations arrive chronologically.

Common evaluation measures include:

  • Root Mean Squared Error (RMSE) expresses error in the target’s original units and retains MSE’s sensitivity to large deviations.
  • Mean Absolute Error (MAE) is easy to interpret and less influenced by extreme residuals.
  • $R^2$ measures the proportion of target variance explained by the model relative to a baseline predictor. Values closer to 1 generally indicate stronger explanatory performance, although $R^2$ can be negative when a model performs worse than the baseline.
  • Adjusted $R^2$ penalizes models for adding predictors, making it more useful when comparing models with different numbers of features.

Residual analysis should accompany summary metrics. Plotting residuals against fitted values can reveal nonlinear patterns, heteroscedasticity, influential observations, or systematic bias. If errors show a clear structure, the model may be missing important structure even when its aggregate score appears acceptable.

For time-dependent data, random cross-validation can leak future information into the training process. Rolling-origin or other chronological validation schemes are usually more realistic.

Preparing Data for Regression

Data preparation directly affects both model stability and performance. Important steps often include:

  • Handling missing values using domain-informed imputation or suitable model-specific treatment.
  • Scaling or normalizing features for distance-sensitive and gradient-based algorithms.
  • Investigating outliers rather than deleting them automatically.
  • Encoding categorical variables with methods appropriate to the selected model.
  • Transforming skewed targets when a more stable relationship is expected.
  • Creating time lags, ratios, interaction terms, or domain-specific aggregates.
  • Ensuring that preprocessing is fitted only on training data to prevent leakage.

Feature engineering can raise performance more than switching between similarly complex algorithms. Domain knowledge is often especially valuable: a compact, meaningful feature may outperform a large collection of generated variables.

Choosing and Maintaining a Model

There is no universally best regression algorithm. The choice depends on the size and structure of the dataset, the desired level of interpretability, computational constraints, and the cost of different kinds of error.

A practical workflow begins with a simple baseline, such as linear regression or a basic tree ensemble. Analysts should then compare candidate models using consistent cross-validation, inspect residuals, test meaningful feature changes, and tune only the most promising options. Finally, the selected pipeline should be evaluated once on an untouched test set or through a realistic validation process.

Production models also require ongoing monitoring. Changes in feature distributions, target behavior, missing-value patterns, or prediction errors can degrade performance even when the training code remains unchanged. Regular retraining, versioned datasets, documented model cards, and clear rollback procedures help keep regression systems reliable.

In summary, regression provides a broad toolkit ranging from interpretable linear models to highly accurate nonlinear ensembles. Starting with a simple baseline, selecting metrics that reflect real-world costs, and validating assumptions through residual analysis can lead to more dependable models than algorithm complexity alone.