# Regression splines
This module includes two spline implementations splines suitable for regression: linear splines using a hinge function basis, and natural cubic splines.
```python
import numpy as np
import matplotlib.pyplot as plt
from regspline import LinearSpline
plt.close('all')
knots = [0,1,2]
coeffs = [1,2,3]
s = LinearSpline(knots, coeffs)
y=s(np.linspace(0,1))
x=np.linspace(0,np.pi)
y=np.sin(x)
xobs = np.repeat(x,50)
yobs = np.repeat(y,50) + 0.01*np.random.randn(*xobs.shape)
s, res = LinearSpline.from_data(xobs, yobs,
knots=np.linspace(0,np.pi,30),
method='OLS',
return_estim_result=True,
prune=True)
plt.plot(x,y)
plt.plot(x,s(x))
```
Several regression types are supported to extract the splines from data, including OLS, LASSO, and quantile regression. See the example files.
## Installation
You can install this library directly from github:
```bash
pip install regspline.git
```
There are two optional dependencies: `scikit-learn`, and `cvxopt`. They are only required to estimate splines on data with, respectively, support vector regressions, and LASSO.
## Background
The module contains two splines:
- A linear spline represented by Hinge functions: $h_i(x) = \max(x-k_i,0)$, where $k_i$ are the knots.
- A natural cubic spline.
The splines chosen:
- have coefficents that have a one-to-one correspondence with the knots.
- have the ability that knots can be removed, e.g., when the corresponding coefficient is small or insignificant, without changing the basis functions corresponding to other knots.
- have the ability to represent functions with sparse basis.
One way to interpret, e.g., the linear spline in the hings basis is as follows. $h_1(x)$ sets an initial slope from the first knot onwards. Then next basis function $h_2(x)$ can adjust the slope at the knot $k_2$, if no adjustment is required, its coefficient is insignificant and the knot can be removed from the spline without any impact on the other basis functions.
## Related projects
Some projects with related methods:
- [basis-expansions](https://github.com/madrury/basis-expansions)
- [py-earth](https://github.com/scikit-learn-contrib/py-earth)
- Quantile regression using decision trees [scikit-garden](https://scikit-garden.github.io/)
The module differs from these implementations as it implements the splines as functions, and they are not integrated within an estimation framework.
## Development
For development purposes, clone the repo:
```bash
git clone https://github.com/mvds314/regspline.git
```
Then navigate to the folder containing `setup.py` and run
```bash
pip install -e .
```
to install the package in edit mode.
Run unittests with `pytest`.
Install the optional dependencies to test all functionality.
Raw data
{
"_id": null,
"home_page": null,
"name": "regspline",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "statistics, regression, splines",
"author": "Martin van der Schans",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/df/3d/ae578971b5d75e30590dea4994a05b6b91df9aaf360623f70314cd9fae83/regspline-25.7.1.tar.gz",
"platform": null,
"description": "# Regression splines\n\nThis module includes two spline implementations splines suitable for regression: linear splines using a hinge function basis, and natural cubic splines.\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom regspline import LinearSpline\nplt.close('all')\n\nknots = [0,1,2]\ncoeffs = [1,2,3]\ns = LinearSpline(knots, coeffs)\ny=s(np.linspace(0,1))\n\nx=np.linspace(0,np.pi)\ny=np.sin(x)\nxobs = np.repeat(x,50)\nyobs = np.repeat(y,50) + 0.01*np.random.randn(*xobs.shape)\n\ns, res = LinearSpline.from_data(xobs, yobs,\n knots=np.linspace(0,np.pi,30),\n method='OLS',\n return_estim_result=True,\n prune=True)\n\nplt.plot(x,y)\nplt.plot(x,s(x))\n```\n\nSeveral regression types are supported to extract the splines from data, including OLS, LASSO, and quantile regression. See the example files.\n\n## Installation\n\nYou can install this library directly from github:\n\n```bash\npip install regspline.git\n```\n\nThere are two optional dependencies: `scikit-learn`, and `cvxopt`. They are only required to estimate splines on data with, respectively, support vector regressions, and LASSO.\n\n## Background\n\nThe module contains two splines:\n\n- A linear spline represented by Hinge functions: $h_i(x) = \\max(x-k_i,0)$, where $k_i$ are the knots.\n- A natural cubic spline.\n\nThe splines chosen:\n\n- have coefficents that have a one-to-one correspondence with the knots.\n- have the ability that knots can be removed, e.g., when the corresponding coefficient is small or insignificant, without changing the basis functions corresponding to other knots.\n- have the ability to represent functions with sparse basis.\n\nOne way to interpret, e.g., the linear spline in the hings basis is as follows. $h_1(x)$ sets an initial slope from the first knot onwards. Then next basis function $h_2(x)$ can adjust the slope at the knot $k_2$, if no adjustment is required, its coefficient is insignificant and the knot can be removed from the spline without any impact on the other basis functions.\n\n## Related projects\n\nSome projects with related methods:\n\n- [basis-expansions](https://github.com/madrury/basis-expansions)\n- [py-earth](https://github.com/scikit-learn-contrib/py-earth)\n- Quantile regression using decision trees [scikit-garden](https://scikit-garden.github.io/)\n\nThe module differs from these implementations as it implements the splines as functions, and they are not integrated within an estimation framework.\n\n## Development\n\nFor development purposes, clone the repo:\n\n```bash\ngit clone https://github.com/mvds314/regspline.git\n```\n\nThen navigate to the folder containing `setup.py` and run\n\n```bash\npip install -e .\n```\n\nto install the package in edit mode.\n\nRun unittests with `pytest`.\n\nInstall the optional dependencies to test all functionality.\n\n",
"bugtrack_url": null,
"license": null,
"summary": "Regression spline",
"version": "25.7.1",
"project_urls": {
"repository": "https://github.com/mvds314/regspline"
},
"split_keywords": [
"statistics",
" regression",
" splines"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "7c2fff50030c9ddd55d04d219d746274c757d5f2403c4cd169d616cccb1268e0",
"md5": "eaf71f423cef0fd64ec213dcb7ff10e2",
"sha256": "5af99b2bb51cf191104d36990cae10775d4a27caa90d4405409d6c5f4d22fe86"
},
"downloads": -1,
"filename": "regspline-25.7.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "eaf71f423cef0fd64ec213dcb7ff10e2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 13208,
"upload_time": "2025-07-08T12:55:32",
"upload_time_iso_8601": "2025-07-08T12:55:32.851043Z",
"url": "https://files.pythonhosted.org/packages/7c/2f/ff50030c9ddd55d04d219d746274c757d5f2403c4cd169d616cccb1268e0/regspline-25.7.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "df3dae578971b5d75e30590dea4994a05b6b91df9aaf360623f70314cd9fae83",
"md5": "9d33632d83b5468ccba4d5467dc2a474",
"sha256": "2f71e9100dbd72602bc582a370ea6c55ae164544cb8e604b5ad682ec2d63bc97"
},
"downloads": -1,
"filename": "regspline-25.7.1.tar.gz",
"has_sig": false,
"md5_digest": "9d33632d83b5468ccba4d5467dc2a474",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 11044,
"upload_time": "2025-07-08T12:55:33",
"upload_time_iso_8601": "2025-07-08T12:55:33.818645Z",
"url": "https://files.pythonhosted.org/packages/df/3d/ae578971b5d75e30590dea4994a05b6b91df9aaf360623f70314cd9fae83/regspline-25.7.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-08 12:55:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mvds314",
"github_project": "regspline",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "regspline"
}