Skip to content

Understanding Regression Algorithms in Machine Learning

Regression is a supervised learning technique that predicts continuous numerical values by understanding relationships between variables in a dataset. Unlike classification, which predicts categories, regression predicts quantities.

  • Definition: Predicts a dependent variable based on a single independent variable
  • Mathematical Form: Y = mx + b
    • Y: Dependent variable (output)
    • x: Independent variable (input)
    • m: Coefficient (slope)
    • b: Intercept
  • Example: Real Estate Price Prediction
    • Input: House square footage
    • Output: House price
  • Definition: Predicts dependent variable based on multiple independent variables
  • Mathematical Form: Y = m₁x₁ + m₂x₂ + … + mₙxₙ + b
  • Real Estate Example Features:
    • Square footage
    • Number of bathrooms
    • Number of bedrooms
    • Year built
    • Location
  • Definition: Fits a curved relationship by adding powers of the predictor as extra terms. The model remains linear in its coefficients, which is why it can still be fitted by ordinary least squares.
  • Mathematical Form: Y = m₁x + m₂x² + … + mₙxⁿ + b
  • Use Case: When the relationship is curved but polynomial in the predictor
    • Example: crop yield against temperature — rising to an optimum, then falling away
  • Not the tool for exponential relationships: exponential growth or decay is modelled by taking the logarithm of the target and fitting a linear model to log(Y), not by adding squared terms. See feature transformation and scaling.
  • Caution: high-degree polynomials fit training data closely and extrapolate wildly. Keep the degree low, and do not predict outside the range the model was fitted on.
AspectRegressionClassification
ObjectivePredicts continuous valuesPredicts categories/classes
Output TypeQuantitative (numerical)Categorical (discrete)
Evaluation MetricsMSE, RMSE, R-squaredAccuracy, Precision, Recall

Practical Implementation: Linear Regression Example

Section titled “Practical Implementation: Linear Regression Example”
# Import required libraries
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Read the dataset
df = pd.read_csv('employee.csv')
# Select features for analysis
X = df[['age']] # Independent variable
y = df['salary'] # Dependent variable
# Create and train the model
model = LinearRegression()
model.fit(X, y)
# Print model parameters
print(f"Intercept: {model.intercept_:.2f}")
print(f"Coefficient: {model.coef_[0]:.2f}")
# Plot the regression line
plt.figure(figsize=(10, 6))
plt.scatter(X, y, color='blue', alpha=0.5)
plt.plot(X, model.predict(X), color='red', linewidth=2)
plt.xlabel('Age')
plt.ylabel('Salary')
plt.title('Age vs Salary Linear Regression')
plt.grid(True)
plt.show()
# Example prediction
age_test = [[35]]
predicted_salary = model.predict(age_test)
print(f"Predicted salary for age 35: ${predicted_salary[0]:,.2f}")
  • Check for missing values
  • Handle outliers
  • Normalize/standardize features if needed
  • Split data into training and testing sets
  • Consider relationship type (linear vs non-linear)
  • Evaluate complexity needs
  • Account for number of features
  • Use appropriate metrics:
    • Mean Squared Error (MSE)
    • Root Mean Squared Error (RMSE)
    • R-squared (R²)
  • Validate assumptions:
    • Linearity
    • Independence
    • Homoscedasticity
    • Normality
  • Overfitting
  • Multicollinearity in multiple regression
  • Extrapolation beyond data range
  • Ignoring outliers
  1. Feature Engineering
  2. Regularization Techniques
  3. Cross-Validation
  4. Hyperparameter Tuning