Name | smoothcon JSON |
Version |
0.0.5
JSON |
| download |
home_page | None |
Summary | Get basis and penalty matrices from mgcv and convert them to numpy arrays |
upload_time | 2025-08-11 20:36:46 |
maintainer | None |
docs_url | None |
author | Johannes Brachem |
requires_python | >=3.13 |
license | None |
keywords |
machine-learning
statistics
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# SmoothCon
[](https://github.com/liesel-devs/smoothcon/actions/workflows/pre-commit.yml)
[](https://github.com/liesel-devs/smoothcon/actions/workflows/pytest.yml)
[](https://github.com/liesel-devs/smoothcon/actions/workflows/pytest.yml)
This is a small wrapper that pulls basis and penalty matrices from the R packge [mgcv](https://cran.r-project.org/web/packages/mgcv/index.html) and converts them to numpy arrays.
Although `smoothcon` is part of the [`liesel`](https://github.com/liesel-devs/liesel)
ecosystem, it has no dependence on Liesel and can be used independently.
`smoothcon` works well together with [`liesel_gam`](https://github.com/liesel-devs/liesel_gam)
for building generalized additive distributional regresion models, see also the
[notebooks](https://github.com/liesel-devs/smoothcon/tree/main/notebooks) for
examples.
## Disclaimer
This package is experimental and under active development. That means:
- The API cannot be considered stable. If you depend on this package, pin the version.
- Testing has not been extensive as of now. Please check and verify!
- There is currently no documentation beyond this readme.
In any case, this package comes with no warranty or guarantees.
## Installation
You can install `smoothcon` from pypi:
```bash
pip install smoothcon
```
You can install the development version from GitHub via pip:
```bash
pip install git+https://github.com/liesel-devs/smoothcon.git
```
Smoothcon requires the following R packages:
```r
install.packages("arrow") # for general usage of ryp
install.packages("svglite") # for plotting in jupyter notebooks
```
## Usage
We illustrate usage with random data:
```python
# import packages
import numpy as np
from smoothcon import SmoothCon
# generate some random data
rng = np.random.default_rng(seed=1)
n = 100
x = rng.uniform(-2.0, 2.0, size=n)
y = x + rng.normal(loc=0.0, scale=1.0, size=n)
mcycle = {"accel": y, "times": x} # imitating the MASS:mcycle dataset
```
Now we initialize the smooth. What's special here is that the `spec` argument of the
`SmoothCon` class can simply be a string containing the R code that you would usually
use to specify a smooth in `mgcv`. Any smooth specification accepted by `mgcv::SmoothCon`
is permitted.
```python
# construct smooth
smooth = SmoothCon(
spec="s(times, bs='ps', k=20, m=c(3,2))", # mgcv smooth specification
data=mcycle, # dictionary, pandas dataframe, or polars dataframe
knots=None, # knots; if None (default), mgcv will create the knots
absorb_cons=True, # If True, constraints (e.g. sum-to-zero) will be absorbed into the basis matrix
diagonal_penalty=True, # If True, the penalty will be diagonalized
pass_to_r=None, # dictionary of data that should be made available to the R environment
)
```
Access smooth information:
```python
# shortcuts to smooth information
smooth.basis # if there is only one basis in the smooth
smooth.penalty # if there is only one penalty in the smooth
smooth.knots
# full smooth information
smooth.all_bases() # list of all bases in the smooth
smooth.all_penalties() # list of all penalties in the smooth
# prediction
new_x = rng.uniform(-1.0, 2.0, size=5)
newdata = {"times": new_x}
smooth.predict(data=newdata) # compute single basis at new covariate values
smooth.predict_all_bases(data=newdata) # compute all bases at new covariate values
smooth(new_x) # alternative syntax for .predict
```
### SmoothFactory
If you want to initialize several smooths, you might not want to pass the data each time
to `SmoothCon`. Passing the data each time is not only cumbersome, but also inefficient,
because it will be converted to an R dataframe each time. So you probably want to
use the `SmoothFactory` class to initialize your `SmoothCon` objects in most cases:
```python
from smoothcon import SmoothFactory
sf = SmoothFactory(data=df, pass_to_r=None) # pass data to R only once
smooth_x = sf("s(x, bs='ps', k=20)") # call to initialize a SmoothCon object
```
## Usage with `liesel_gam`: Example Notebooks
Advanced usage for building generalized additive distributional regression models with `liesel` and `liesel_gam` is illustrated in the following notebooks.
- [notebooks/test_gam_gibbs.ipynb](https://github.com/liesel-devs/liesel_gam/blob/main/notebooks/test_gam_gibbs.ipynb): A generalized addition location-scale model, using inverse gamma priors an Gibbs kernels for the inverse smoothing parameters.
- [notebooks/test_gam_manual.ipynb](https://github.com/liesel-devs/liesel_gam/blob/main/notebooks/test_gam_manual.ipynb): A generalized addition location-scale model, using a manually initialized inverse smoothing parameter with a Weibull prior.
Raw data
{
"_id": null,
"home_page": null,
"name": "smoothcon",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.13",
"maintainer_email": null,
"keywords": "machine-learning, statistics",
"author": "Johannes Brachem",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/5a/52/7b6c14c1e4e4b3513bbab03b07caf9fbe53f52b14cd7e0af6b2456d53041/smoothcon-0.0.5.tar.gz",
"platform": null,
"description": "# SmoothCon\n\n[](https://github.com/liesel-devs/smoothcon/actions/workflows/pre-commit.yml)\n[](https://github.com/liesel-devs/smoothcon/actions/workflows/pytest.yml)\n[](https://github.com/liesel-devs/smoothcon/actions/workflows/pytest.yml)\n\nThis is a small wrapper that pulls basis and penalty matrices from the R packge [mgcv](https://cran.r-project.org/web/packages/mgcv/index.html) and converts them to numpy arrays.\n\nAlthough `smoothcon` is part of the [`liesel`](https://github.com/liesel-devs/liesel)\necosystem, it has no dependence on Liesel and can be used independently.\n`smoothcon` works well together with [`liesel_gam`](https://github.com/liesel-devs/liesel_gam)\nfor building generalized additive distributional regresion models, see also the\n[notebooks](https://github.com/liesel-devs/smoothcon/tree/main/notebooks) for\nexamples.\n\n## Disclaimer\n\nThis package is experimental and under active development. That means:\n\n- The API cannot be considered stable. If you depend on this package, pin the version.\n- Testing has not been extensive as of now. Please check and verify!\n- There is currently no documentation beyond this readme.\n\nIn any case, this package comes with no warranty or guarantees.\n\n## Installation\n\nYou can install `smoothcon` from pypi:\n\n```bash\npip install smoothcon\n```\n\nYou can install the development version from GitHub via pip:\n\n```bash\npip install git+https://github.com/liesel-devs/smoothcon.git\n```\n\nSmoothcon requires the following R packages:\n\n```r\ninstall.packages(\"arrow\") # for general usage of ryp\ninstall.packages(\"svglite\") # for plotting in jupyter notebooks\n```\n\n## Usage\n\nWe illustrate usage with random data:\n\n```python\n# import packages\nimport numpy as np\nfrom smoothcon import SmoothCon\n\n\n# generate some random data\nrng = np.random.default_rng(seed=1)\nn = 100\nx = rng.uniform(-2.0, 2.0, size=n)\ny = x + rng.normal(loc=0.0, scale=1.0, size=n)\nmcycle = {\"accel\": y, \"times\": x} # imitating the MASS:mcycle dataset\n```\n\nNow we initialize the smooth. What's special here is that the `spec` argument of the\n`SmoothCon` class can simply be a string containing the R code that you would usually\nuse to specify a smooth in `mgcv`. Any smooth specification accepted by `mgcv::SmoothCon`\nis permitted.\n\n```python\n# construct smooth\nsmooth = SmoothCon(\n spec=\"s(times, bs='ps', k=20, m=c(3,2))\", # mgcv smooth specification\n data=mcycle, # dictionary, pandas dataframe, or polars dataframe\n knots=None, # knots; if None (default), mgcv will create the knots\n absorb_cons=True, # If True, constraints (e.g. sum-to-zero) will be absorbed into the basis matrix\n diagonal_penalty=True, # If True, the penalty will be diagonalized\n pass_to_r=None, # dictionary of data that should be made available to the R environment\n)\n```\n\nAccess smooth information:\n\n```python\n# shortcuts to smooth information\nsmooth.basis # if there is only one basis in the smooth\nsmooth.penalty # if there is only one penalty in the smooth\nsmooth.knots\n\n# full smooth information\nsmooth.all_bases() # list of all bases in the smooth\nsmooth.all_penalties() # list of all penalties in the smooth\n\n# prediction\nnew_x = rng.uniform(-1.0, 2.0, size=5)\nnewdata = {\"times\": new_x}\nsmooth.predict(data=newdata) # compute single basis at new covariate values\nsmooth.predict_all_bases(data=newdata) # compute all bases at new covariate values\nsmooth(new_x) # alternative syntax for .predict\n```\n\n### SmoothFactory\n\nIf you want to initialize several smooths, you might not want to pass the data each time\nto `SmoothCon`. Passing the data each time is not only cumbersome, but also inefficient,\nbecause it will be converted to an R dataframe each time. So you probably want to\nuse the `SmoothFactory` class to initialize your `SmoothCon` objects in most cases:\n\n```python\nfrom smoothcon import SmoothFactory\n\nsf = SmoothFactory(data=df, pass_to_r=None) # pass data to R only once\nsmooth_x = sf(\"s(x, bs='ps', k=20)\") # call to initialize a SmoothCon object\n```\n\n## Usage with `liesel_gam`: Example Notebooks\n\nAdvanced usage for building generalized additive distributional regression models with `liesel` and `liesel_gam` is illustrated in the following notebooks.\n\n- [notebooks/test_gam_gibbs.ipynb](https://github.com/liesel-devs/liesel_gam/blob/main/notebooks/test_gam_gibbs.ipynb): A generalized addition location-scale model, using inverse gamma priors an Gibbs kernels for the inverse smoothing parameters.\n- [notebooks/test_gam_manual.ipynb](https://github.com/liesel-devs/liesel_gam/blob/main/notebooks/test_gam_manual.ipynb): A generalized addition location-scale model, using a manually initialized inverse smoothing parameter with a Weibull prior.\n",
"bugtrack_url": null,
"license": null,
"summary": "Get basis and penalty matrices from mgcv and convert them to numpy arrays",
"version": "0.0.5",
"project_urls": null,
"split_keywords": [
"machine-learning",
" statistics"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "1c6e167847ecaf045d88f6bcc064985d1aa696961f9d82354fa8bdb979a49e9f",
"md5": "ec56b0fdae3cdc531dc8677fe9ed2508",
"sha256": "3cad2a4ecb37bc361b8f99c7047b5f6131f12b947a5bea5ad8aa1d37383edda9"
},
"downloads": -1,
"filename": "smoothcon-0.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ec56b0fdae3cdc531dc8677fe9ed2508",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.13",
"size": 6517,
"upload_time": "2025-08-11T20:36:45",
"upload_time_iso_8601": "2025-08-11T20:36:45.900703Z",
"url": "https://files.pythonhosted.org/packages/1c/6e/167847ecaf045d88f6bcc064985d1aa696961f9d82354fa8bdb979a49e9f/smoothcon-0.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "5a527b6c14c1e4e4b3513bbab03b07caf9fbe53f52b14cd7e0af6b2456d53041",
"md5": "cfcf5fccec7df4cfb71e6b3d475db679",
"sha256": "5b4c6d886542f310d7c82c4e28a3e04fc1f654dc4a35b12e03bb3399502277e7"
},
"downloads": -1,
"filename": "smoothcon-0.0.5.tar.gz",
"has_sig": false,
"md5_digest": "cfcf5fccec7df4cfb71e6b3d475db679",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.13",
"size": 68137,
"upload_time": "2025-08-11T20:36:46",
"upload_time_iso_8601": "2025-08-11T20:36:46.834964Z",
"url": "https://files.pythonhosted.org/packages/5a/52/7b6c14c1e4e4b3513bbab03b07caf9fbe53f52b14cd7e0af6b2456d53041/smoothcon-0.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-11 20:36:46",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "smoothcon"
}