torch-linear-regression


Nametorch-linear-regression JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/RichieHakim/torch_linear_regression
SummaryA library of useful modules for data analysis.
upload_time2024-08-26 14:16:31
maintainerNone
docs_urlNone
authorRichard Hakim
requires_pythonNone
licenseLICENSE
keywords data analysis machine learning neuroscience
VCS
bugtrack_url
requirements numpy scikit-learn torch hypothesis
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version](https://badge.fury.io/py/torch_linear_regression.svg)](https://badge.fury.io/py/torch_linear_regression)
[![Downloads](https://pepy.tech/badge/torch_linear_regression)](https://pepy.tech/project/torch_linear_regression)
[![repo size](https://img.shields.io/github/repo-size/RichieHakim/torch_linear_regression)](https://github.com/RichieHakim/torch_linear_regression/)

#  torch_linear_regression 
A very simple library containing closed-form linear regression models using PyTorch.
Includes:
- Ordinary Least Squares (OLS) Linear Regression: `(X'X)^-1 X'Y`
- Ridge Regression: `(X'X + λI)^-1 X'Y`
- Reduced Rank Regression (RRR) with Ridge penalty: Ridge regression followed by SVD on the weights matrix

The closed-form approach results in fast and accurate results under most
conditions. However, when ``n_features`` is large and/or underdetermined
(``n_samples`` <= ``n_features``), the solution will start to diverge from
gradient-based / sklearn solutions.

Each model also includes a `model.prefit()` method that can be used to precompute
the inverse matrix and the ridge penalty matrix. This can be useful when the model
is used multiple times with the same `X` input data.

Because the models are based on PyTorch, they are significantly faster than sklearn's
models, and can be further accelerated by using GPU. Also the models can be used
in conjunction with PyTorch's autograd for gradient-based optimization.

## Installation
Install stable version:
```
pip install torch_linear_regression
```

Install development version:
```
pip install git+https://github.com/RichieHakim/torch_linear_regression.git
```

## Usage 
See the notebook for more examples: [demo notebook](https://github.com/RichieHakim/torch_linear_regression/blob/master/demo_notebook.ipynb)
```
import torch_linear_regression as tlr

import torch
import numpy as np
import sklearn
import sklearn.datasets
import matplotlib.pyplot as plt


## Generate data for regression
X, Y = sklearn.datasets.make_regression(
    n_samples=100,
    n_features=2,
    n_informative=10,
    bias=2,
    noise=50,
    random_state=42,
)

## Create model
model_ols = tlr.OLS()
## Fit model
model_ols.fit(X=X, y=Y)
## Predict
Y_pred = model_ols.predict(X)
## Score model
score = model_ols.score(X=X, y=Y)
print(f"R^2: {score}")
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/RichieHakim/torch_linear_regression",
    "name": "torch-linear-regression",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "data analysis, machine learning, neuroscience",
    "author": "Richard Hakim",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/6d/27/fb369afbdf41f4f153dcb2d902b0f275f31d5594464253f8728823fec8a5/torch_linear_regression-0.1.0.tar.gz",
    "platform": null,
    "description": "[![PyPI version](https://badge.fury.io/py/torch_linear_regression.svg)](https://badge.fury.io/py/torch_linear_regression)\n[![Downloads](https://pepy.tech/badge/torch_linear_regression)](https://pepy.tech/project/torch_linear_regression)\n[![repo size](https://img.shields.io/github/repo-size/RichieHakim/torch_linear_regression)](https://github.com/RichieHakim/torch_linear_regression/)\n\n#  torch_linear_regression \nA very simple library containing closed-form linear regression models using PyTorch.\nIncludes:\n- Ordinary Least Squares (OLS) Linear Regression: `(X'X)^-1 X'Y`\n- Ridge Regression: `(X'X + \u03bbI)^-1 X'Y`\n- Reduced Rank Regression (RRR) with Ridge penalty: Ridge regression followed by SVD on the weights matrix\n\nThe closed-form approach results in fast and accurate results under most\nconditions. However, when ``n_features`` is large and/or underdetermined\n(``n_samples`` <= ``n_features``), the solution will start to diverge from\ngradient-based / sklearn solutions.\n\nEach model also includes a `model.prefit()` method that can be used to precompute\nthe inverse matrix and the ridge penalty matrix. This can be useful when the model\nis used multiple times with the same `X` input data.\n\nBecause the models are based on PyTorch, they are significantly faster than sklearn's\nmodels, and can be further accelerated by using GPU. Also the models can be used\nin conjunction with PyTorch's autograd for gradient-based optimization.\n\n## Installation\nInstall stable version:\n```\npip install torch_linear_regression\n```\n\nInstall development version:\n```\npip install git+https://github.com/RichieHakim/torch_linear_regression.git\n```\n\n## Usage \nSee the notebook for more examples: [demo notebook](https://github.com/RichieHakim/torch_linear_regression/blob/master/demo_notebook.ipynb)\n```\nimport torch_linear_regression as tlr\n\nimport torch\nimport numpy as np\nimport sklearn\nimport sklearn.datasets\nimport matplotlib.pyplot as plt\n\n\n## Generate data for regression\nX, Y = sklearn.datasets.make_regression(\n    n_samples=100,\n    n_features=2,\n    n_informative=10,\n    bias=2,\n    noise=50,\n    random_state=42,\n)\n\n## Create model\nmodel_ols = tlr.OLS()\n## Fit model\nmodel_ols.fit(X=X, y=Y)\n## Predict\nY_pred = model_ols.predict(X)\n## Score model\nscore = model_ols.score(X=X, y=Y)\nprint(f\"R^2: {score}\")\n```\n",
    "bugtrack_url": null,
    "license": "LICENSE",
    "summary": "A library of useful modules for data analysis.",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/RichieHakim/torch_linear_regression"
    },
    "split_keywords": [
        "data analysis",
        " machine learning",
        " neuroscience"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ae21fe7394c25e961dadd16f1d006e1e71e2cc31755a13f94d3eae33e46ba229",
                "md5": "7f40b75f3c975f30fd98f7db8fa9be31",
                "sha256": "2a123359f7df08e595babd664dbc4e30fd93330af286c0f752740c4ad460c9cb"
            },
            "downloads": -1,
            "filename": "torch_linear_regression-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7f40b75f3c975f30fd98f7db8fa9be31",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8455,
            "upload_time": "2024-08-26T14:16:30",
            "upload_time_iso_8601": "2024-08-26T14:16:30.118080Z",
            "url": "https://files.pythonhosted.org/packages/ae/21/fe7394c25e961dadd16f1d006e1e71e2cc31755a13f94d3eae33e46ba229/torch_linear_regression-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6d27fb369afbdf41f4f153dcb2d902b0f275f31d5594464253f8728823fec8a5",
                "md5": "822a2bbebfb3861af2592e8ea4430b3c",
                "sha256": "7e6e2bd9bd67073946c73af34705aee093964ef32b1e91f7b23fb30ab58cdbfc"
            },
            "downloads": -1,
            "filename": "torch_linear_regression-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "822a2bbebfb3861af2592e8ea4430b3c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9750,
            "upload_time": "2024-08-26T14:16:31",
            "upload_time_iso_8601": "2024-08-26T14:16:31.217895Z",
            "url": "https://files.pythonhosted.org/packages/6d/27/fb369afbdf41f4f153dcb2d902b0f275f31d5594464253f8728823fec8a5/torch_linear_regression-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-26 14:16:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "RichieHakim",
    "github_project": "torch_linear_regression",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "scikit-learn",
            "specs": []
        },
        {
            "name": "torch",
            "specs": []
        },
        {
            "name": "hypothesis",
            "specs": []
        }
    ],
    "lcname": "torch-linear-regression"
}
        
Elapsed time: 0.55525s