Skip to content

Random Forest

Regression analysis is a fundamental statistical and machine learning technique used for modeling the relationship between a dependent variable (the target or outcome) and one or more independent variables (features or predictors). The primary goal of regression in supervised learning is to predict a continuous numerical value.

Unlike classification, which predicts discrete categories, regression predicts quantities like price, temperature, height, sales figures, or sensor readings. The model learns the association between input features and the corresponding continuous output values from the training data.

Let X represent the input features (independent variables) and y represent the continuous target variable (dependent variable). A regression model aims to learn a function f such that y ≈ f(X).

Based on the number of independent variables, regression models can be broadly classified:

  • Simple Linear Regression: Models the relationship between a single independent variable (feature) and a continuous dependent variable (target) using a straight line (y = mx + c).
  • Multiple Linear Regression: Models the relationship between multiple independent variables (features) and a continuous dependent variable using a linear equation (y = b₀ + b₁x₁ + b₂x₂ + ... + b<0xE2><0x82><0x99>x<0xE2><0x82><0x99>).

Beyond linear models, various other regression algorithms exist to capture more complex, non-linear relationships.

Building a Regressor in Python with Scikit-learn

Section titled “Building a Regressor in Python with Scikit-learn”

Similar to classifiers, Scikit-learn provides a consistent interface for building regression models. Let’s build a basic Simple Linear Regressor.

The steps typically involve:

  1. Import necessary libraries.
  2. Load or generate the dataset.
  3. Split the data into training and testing sets.
  4. Choose and instantiate the regression model.
  5. Train (fit) the model on the training data.
  6. Make predictions on the test data.
  7. Evaluate the model’s performance.
# Step 1: Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import pandas as pd # Optional: for loading data if in CSV
# Step 2: Load or generate data
# Example using generated data for simplicity
np.random.seed(42)
X = 2 * np.random.rand(100, 1) # Single feature
y = 4 + 3 * X + np.random.randn(100, 1) # y = 4 + 3x + noise
# If loading from a file (like the original example):
# input_file = 'your_data_file.csv' # Replace with your file path
# data = pd.read_csv(input_file)
# X = data[['feature_column']].values # Ensure X is 2D
# y = data['target_column'].values
# Step 3: Split data into training and testing sets
# test_size=0.3 means 30% for testing, 70% for training
# random_state ensures reproducibility
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Step 4: Choose and instantiate the model
reg_linear = LinearRegression()
# Step 5: Train the model
reg_linear.fit(X_train, y_train)
# Step 6: Make predictions
y_pred = reg_linear.predict(X_test)
# Step 7: Evaluate the model
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
print("Regressor model performance:")
print(f"Mean Absolute Error (MAE): {mae:.4f}")
print(f"Mean Squared Error (MSE): {mse:.4f}")
print(f"Root Mean Squared Error (RMSE): {rmse:.4f}")
print(f"R-squared (R²): {r2:.4f}")
# Optional: Print learned parameters
print(f"\nIntercept (b₀): {reg_linear.intercept_[0]:.4f}")
print(f"Coefficient (b₁): {reg_linear.coef_[0][0]:.4f}")
# Step 8: Visualize the results
plt.figure(figsize=(8, 6))
sns.scatterplot(x=X_test.flatten(), y=y_test.flatten(), label='Actual Data')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Regression Line')
plt.title('Linear Regression Fit')
plt.xlabel('Feature (X)')
plt.ylabel('Target (y)')
plt.legend()
plt.show()

The output will show:

  • Performance metrics (MAE, MSE, RMSE, R²): Lower error values (MAE, MSE, RMSE) and R² closer to 1 indicate better performance.
  • Learned parameters: Intercept and coefficient(s) of the linear model.
  • A plot showing the actual data points (scatter plot) and the fitted regression line.

(Note: The original tutorial’s output showed a negative R², indicating a very poor fit for that specific data/split. This highlights the importance of evaluating models; a simple linear model isn’t always appropriate.)

Beyond simple and multiple linear regression, other important algorithms include:

  • Polynomial Regression: Models non-linear relationships by adding polynomial terms (e.g., x², x³) to the features.
  • Ridge Regression: Linear regression with L2 regularization to prevent overfitting by penalizing large coefficients.
  • Lasso Regression: Linear regression with L1 regularization, which can also perform feature selection by shrinking some coefficients exactly to zero.
  • ElasticNet Regression: Combines L1 and L2 regularization.
  • Support Vector Regression (SVR): An adaptation of Support Vector Machines for regression tasks.
  • Decision Tree Regression: Uses tree-like structures to make predictions.
  • Random Forest Regression: An ensemble method using multiple decision trees to improve robustness and accuracy.
  • Gradient Boosting Regression (e.g., XGBoost, LightGBM, CatBoost): Powerful ensemble methods that build trees sequentially, each correcting the errors of the previous ones.

Regression finds wide application across various domains:

  • Predictive Analysis/Forecasting: Predicting future values like sales, stock prices, weather conditions, energy demand, or patient recovery times.
  • Understanding Relationships: Quantifying the impact of independent variables on a dependent variable (e.g., how advertising spend affects sales, how study time impacts test scores).
  • Optimization: Finding optimal settings for processes (e.g., optimizing pricing based on demand prediction).
  • Risk Assessment: Predicting financial risk, insurance premiums, or loan default probability.
  • Scientific Modeling: Modeling physical phenomena, biological processes, or economic trends.

Further Resources: