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
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:
- Generating synthetic regression data using
sklearn
. - Fitting the Linear Regression model.
- Evaluating the model's performance using regression metrics.
- 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:
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:
2. Initializing the Model
Initialize the LinearRegressionModel
with an optional random seed for reproducibility:
3. Fitting the Model
Train the model using the fit
method:
4. Predictions
Make predictions using the trained model:
5. Retrieving Model Parameters
Retrieve the coefficients and intercept:
6. Evaluating Performance
Evaluate the model using common regression metrics such as MSE, MAE, and R2 Score:
7. Visualization
Visualize the regression results using the built-in plotting function:
Sample Output
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.