Skip to content

Example Script: linear_regression_example.py

This script demonstrates how to use the LinearRegressionModel class from the SciREX library to perform regression on a synthetic dataset.

The example includes
  • Generating synthetic regression data
  • Training a Linear Regression model
  • Evaluating model performance using regression metrics
  • Visualizing the regression results
Dependencies
  • numpy
  • matplotlib
  • scikit-learn
  • scirex.core.ml.supervised.regression.linear_regression
Authors
  • Paranidharan (paranidharan@iisc.ac.in)
Version Info
  • 16/Jan/2025: Initial version

Linear Regression Example Documentation

Introduction

This document demonstrates the usage of the LinearRegressionModel class from the SciREX library to perform regression on synthetic data. Linear Regression provides a baseline regression model without regularization, making it an interpretable and widely used technique for regression tasks.


Workflow Overview

The example covers the following steps:

  1. Generating synthetic regression data using sklearn.
  2. Fitting the Linear Regression model.
  3. Evaluating the model's performance using regression metrics.
  4. Visualizing the results.

Requirements

Ensure the following dependencies are installed before running the script:

  • numpy
  • scikit-learn
  • matplotlib
  • SciREX library

Install missing dependencies using pip:

pip install numpy scikit-learn matplotlib


Linear Regression Example Script

Here is the complete example script for Linear Regression:

import numpy as np
from sklearn.datasets import make_regression
from scirex.core.ml.supervised.regression.linear_regression import LinearRegressionModel

# Generate synthetic regression data
X, y = make_regression(
    n_samples=100,
    n_features=1,
    noise=10,
    random_state=42
)

# Initialize the Linear Regression model
model = LinearRegressionModel(random_state=42)

# Fit the model on the training data
model.fit(X, y)

# Make predictions on the training data
y_pred = model.predict(X)

# Get model parameters (coefficients and intercept)
params = model.get_model_params()
print("Model Parameters:")
print(f"Coefficients: {params['coefficients']}")
print(f"Intercept: {params['intercept']}")

# Evaluate the model using regression metrics
metrics = model.evaluation_metrics(y, y_pred)
print("\nEvaluation Metrics:")
print(f"MSE: {metrics['mse']:.2f}")
print(f"MAE: {metrics['mae']:.2f}")
print(f"R2 Score: {metrics['r2']:.2f}")

# Visualize the regression results
model.plot_regression_results(y, y_pred)

Key Steps Explained

1. Generating Synthetic Data

We use sklearn.datasets.make_regression to create a synthetic dataset with one feature and added noise for demonstration purposes:

X, y = make_regression(
    n_samples=100,
    n_features=1,
    noise=10,
    random_state=42
)

2. Initializing the Model

Initialize the LinearRegressionModel with an optional random seed for reproducibility:

model = LinearRegressionModel(random_state=42)

3. Fitting the Model

Train the model using the fit method:

model.fit(X, y)

4. Predictions

Make predictions using the trained model:

y_pred = model.predict(X)

5. Retrieving Model Parameters

Retrieve the coefficients and intercept:

params = model.get_model_params()

6. Evaluating Performance

Evaluate the model using common regression metrics such as MSE, MAE, and R2 Score:

metrics = model.evaluation_metrics(y, y_pred)

7. Visualization

Visualize the regression results using the built-in plotting function:

model.plot_regression_results(y, y_pred)

Sample Output

Model Parameters:
Coefficients: 
Intercept: 

Evaluation Metrics:
MSE: 
MAE: 
R2 Score: 

The visualization will show the actual vs. predicted values on a scatter plot with the regression line.


Conclusion

This example showcases how to use the LinearRegressionModel class from SciREX for regression tasks. Linear Regression provides a simple and interpretable baseline model, making it suitable for many applications.

For more advanced use cases and detailed documentation, visit the SciREX Documentation.


Author

This example was authored by Paranidharan (paranidharan@iisc.ac.in).


License

This document is part of the SciREX library and is licensed under the Apache License 2.0. For more information, visit Apache License 2.0.