OLS_Regressorο
π Aboutο
The OLS Regression Package is a Python library designed to streamline the process of performing Ordinary Least Squares (OLS) regression analysis. Whether youβre a data scientist, researcher, or analyst, this package aims to provide a simple and efficient tool for fitting linear models to your data. It will fit a linear model with coefficients w = (w1, w2, β¦, wn) to minimize Residual Sum of Squares (RSS) between the observed targets values in the dataset, and the targets predicted by the linear approximation for the examples in the dataset.
π» Installationο
Install the package from PyPiο
Run this command to install the ols_regressor package from PyPi
pip install ols_regressor
Install the package from GitHubο
Run the following commands to install from GitHub if the installation is unsuccessful from PyPi.
Clone the repository Open your terminal, navigate to where you would like the repository to be cloned and run the following command:
$ git clone git@github.com:UBC-MDS/OLS_regressor.git
Create the conda environment and activate it Run the following command to create the conda environment which will include the necessary Python and Poetry versions and dependencies:
conda env create --name ols_regressor python=3.9 poetry==1.7.1 -y
Next, run the following command to activate the conda environment we created:
conda activate `ols_regressor`
Install the package using Poetry
Run the following command to install the package ols_regressor:
poetry install
π‘ Functionsο
fit: Fits the linear model according to the OLS mechanism.predict: Predicts target values using the fitted linear model.score: Calculates the coefficient of determination R-squared value for the prediction.cross_validate: Performs cross-validated Ordinary Least Squares (OLS) regression.
β Usageο
This guide provides a quick start to using the OLS_Regressor package, specifically the LinearRegressor class, to perform linear regression analysis. The package offers simple-to-use methods for fitting the model, making predictions, and evaluating the performance. For more details about the package, please see the vingette for detailed usage.
Importing the LinearRegressorο
from OLS_Regressor.regressor import LinearRegressor
from Ols_regressor.cross_validate import cross_validate
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_regression
Fitting the Modelο
To fit the linear regression model, you need to have your dataset ready, typically split into features (X) and the target variable (y). Hereβs how you can fit the model:
# Assuming X and y are your features and target variable respectively
regressor = LinearRegressor()
regressor.fit(X, y)
Making Predictionsο
Once the model is trained, you can make predictions on new data:
# Assuming X_new represents new data
predictions = regressor.predict(X_new)
Evaluating the Modelο
To evaluate the performance of your model, you can use the score method, which by default provides the R-squared value of the predictions:
# Evaluating the model on test data
r_squared = regressor.score(X_test, y_test)
print(f"R-squared: {r_squared}")
Cross-Validationο
The OLS_Regressor package provides a cross_validate function to help evaluate the modelβs performance across different partitions of the dataset, ensuring a more robust assessment than using a single train-test split.
To use cross_validate, you must first import it from the package, then provide it with your dataset and the model you wish to evaluate. Hereβs an example:
# Creating an instance of LinearRegressor
model = LinearRegressor()
# Performing cross-validation
results = cross_validate(model, X, y, cv=5) # cv is the number of folds
# Printing the results
print("Cross-validation results:", results)
π§ͺ Auto-testο
To run the auto-test supported by pytest, simply run the following command in the terminal or commandline tools:
pytest tests/
β
OLS_Regressor use in Python ecosystemο
The OLS Regression Package seamlessly integrates into the rich Python ecosystem, offering a specialized solution for Ordinary Least Squares (OLS) regression analysis. While various Python libraries provide general-purpose machine learning and statistical functionalities, our package focuses specifically on the simplicity and efficiency of linear regression. scikit-learn is a widely used machine learning library that encompasses regression among its many capabilities scikit-learn. Our package distinguishes itself by providing a lightweight and user-friendly interface tailored for users seeking a straightforward solution for OLS regression without the overhead of extensive machine learning or statistical functionalities. If you find that your needs align more closely with a broader set of machine learning tools or comprehensive statistical modeling, scikit-learn or statsmodels may be suitable alternatives. As of [2024-01-12], no existing package caters specifically to OLS regression with our packageβs emphasis on simplicity and ease of use.
π€ Contributorsο
Xia Yimeng (@YimengXia)
Sifan Zhang (@Sifanzzz)
Charles Xu (@charlesxch)
Waleed Mahmood (@WaleedMahmood1)
π Contributingο
Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.
π Licenseο
OLS_Regressor is licensed under the terms of the MIT license.
π Creditsο
OLS_Regressor was created with cookiecutter and the py-pkgs-cookiecutter template.
ποΈ Referencesο
Giriyewithana, N. (2023). Australian Vehicle Prices [Data set]. Kaggle. https://www.kaggle.com/datasets/nelgiriyewithana/australian-vehicle-prices
Michael, B. (2023, February 23). How Does Linear Regression Really Work. Towards Data Science. https://towardsdatascience.com/how-does-linear-regression-really-work-2387d0f11e8
scikit-learn: Machine Learning in Python. https://scikit-learn.org/stable/
pandas: A Foundational Python Library for Data Analysis and Statistics. https://pandas.pydata.org/
pytest: helps you write better programs https://pytest.org/