
#


[](https://badge.fury.io/py/contextualized-ml)

[](https://pepy.tech/project/contextualized-ml)

<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
[](https://doi.org/10.21105/joss.06469)
An SKLearn-style toolbox for estimating and analyzing models, distributions, and functions with context-specific parameters.
Context-specific parameters:
- Find hidden heterogeneity in data -- are all samples the same?
- Identify context-specific predictors -- are there different reasons for outcomes?
- Enable domain adaptation -- can learned models extrapolate to new contexts?
Most models can be contextualized. For example, [linear regression](https://en.wikipedia.org/wiki/Linear_regression#Formulation) is
```math
Y = X\beta + \epsilon
```
Contextualized linear regression is
```math
Y = X\beta(C) + \epsilon
```
where the coefficients $\beta$ are now a function of context $C$, allowing the model to adapt to context-specific changes.
Contextualized implements this for many types of statistical models, including linear regression, logistic regression, Bayesian networks, correlation networks, and Markov networks.
For more details, see the [Contextualized Machine Learning whitepaper](https://arxiv.org/abs/2310.11340).
## Quick Start
### Installation
```
pip install contextualized-ml
```
Take a look at the [easy demo](docs/models/easy_regression.ipynb) for a quickstart with sklearn-style wrappers.
### Build a Contextualized Model
```
from contextualized.easy import ContextualizedRegressor
model = ContextualizedRegressor()
model.fit(C, X, Y)
```
This builds a contextualized linear regression model by fitting a deep-learning model to generate context-specific coefficients $\beta(C)$.
### Predict Context-Specific Parameters
```
model.predict_params(C)
```
See the [docs](https://contextualized.ml/docs) for more examples.
### Important links
- [Documentation](https://contextualized.ml/docs)
- [Pypi package index](https://pypi.python.org/pypi/contextualized-ml)
## Citing
If you use this software, please cite the software [publication](https://doi.org/10.21105/joss.06469):
```
@article{Ellington2024,
doi = {10.21105/joss.06469},
url = {https://doi.org/10.21105/joss.06469},
year = {2024},
publisher = {The Open Journal},
volume = {9},
number = {97},
pages = {6469},
author = {Caleb N. Ellington and Benjamin J. Lengerich and Wesley Lo and Aaron Alvarez and Andrea Rubbi and Manolis Kellis and Eric P. Xing},
title = {Contextualized: Heterogeneous Modeling Toolbox},
journal = {Journal of Open Source Software}
}
```
## Contributing
Add your own contributions by sending a PR or request an improvement by creating an [issue](https://github.com/cnellington/Contextualized/issues). See [CONTRIBUTING.md](https://github.com/cnellington/Contextualized/blob/main/CONTRIBUTING.md) for more info.
## Thanks to all our contributors
<a href="https://github.com/cnellington/contextualized/graphs/contributors">
<img src="https://contributors-img.web.app/image?repo=cnellington/contextualized" />
</a>
Contextualized ML was originally implemented by [Caleb Ellington](https://calebellington.com/) (CMU) and [Ben Lengerich](http://web.mit.edu/~blengeri/www) (MIT).
Beyond code contributions, many people have helped. Check out [ACKNOWLEDGEMENTS.md](https://github.com/cnellington/Contextualized/blob/main/ACKNOWLEDGEMENTS.md)!
## Related Publications and Pre-prints
- [Contextualized Machine Learning](https://arxiv.org/abs/2310.11340)
- [Contextualized: Heterogeneous Modeling Toolbox](https://doi.org/10.21105/joss.06469)
- Networks
- [Contextualized Networks Reveal Heterogeneous Transcriptomic Regulation in Tumors at Sample-Specific Resolution](https://www.biorxiv.org/content/10.1101/2023.12.01.569658v1)
- [NOTMAD: Estimating Bayesian Networks with Sample-Specific Structures and Parameters](http://arxiv.org/abs/2111.01104)
- Applications
- [Patient-Specific Models of Treatment Effects Explain Heterogeneity in Tuberculosis](https://arxiv.org/abs/2411.10645)
- [Contextualized Policy Recovery: Modeling and Interpreting Medical Decisions with Adaptive Imitation Learning](https://arxiv.org/abs/2310.07918)
- [Automated Interpretable Discovery of Heterogeneous Treatment Effectiveness: A COVID-19 Case Study](https://www.sciencedirect.com/science/article/pii/S1532046422001022)
- [Discriminative Subtyping of Lung Cancers from Histopathology Images via Contextual Deep Learning](https://www.medrxiv.org/content/10.1101/2020.06.25.20140053v1.abstract)
- [Personalized Survival Prediction with Contextual Explanation Networks](http://arxiv.org/abs/1801.09810)
- [Contextual Explanation Networks](https://jmlr.org/papers/v21/18-856.html)
- Background reading:
- [Varying-Coefficient Models](https://academic.oup.com/jrsssb/article-abstract/55/4/757/7028270)
## Videos
- [Cold Spring Harbor Laboratory: Contextualized Graphical Models Reveal Sample-Specific Transcriptional Networks for 7000 Tumors](https://www.youtube.com/watch?v=MTcjFK-YwCw)
- [Sample-Specific Models for Interpretable Analysis with Applications to Disease Subtyping](http://www.birs.ca/events/2022/5-day-workshops/22w5055/videos/watch/202205051559-Lengerich.html)
## Contact Us
Please get in touch with any questions, feature requests, or applications by using the [GitHub discussions page](https://github.com/cnellington/Contextualized/discussions).
Raw data
{
"_id": null,
"home_page": null,
"name": "contextualized-ml",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "contextual modeling, graphical models, machine learning, meta-learning, multitask learning",
"author": null,
"author_email": "Caleb Ellington <cellingt@cs.cmu.edu>, Ben Lengerich <blengeri@mit.edu>",
"download_url": "https://files.pythonhosted.org/packages/45/fb/b43495f3d9965c119b5b1862f97127d78614e27b6731a2da30e9f95e93df/contextualized_ml-0.2.9.tar.gz",
"platform": null,
"description": "\n#\n\n\n\n[](https://badge.fury.io/py/contextualized-ml)\n\n[](https://pepy.tech/project/contextualized-ml)\n\n<a href=\"https://github.com/psf/black\"><img alt=\"Code style: black\" src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"></a>\n[](https://doi.org/10.21105/joss.06469)\n\n\nAn SKLearn-style toolbox for estimating and analyzing models, distributions, and functions with context-specific parameters.\n\nContext-specific parameters:\n- Find hidden heterogeneity in data -- are all samples the same?\n- Identify context-specific predictors -- are there different reasons for outcomes?\n- Enable domain adaptation -- can learned models extrapolate to new contexts?\n\nMost models can be contextualized. For example, [linear regression](https://en.wikipedia.org/wiki/Linear_regression#Formulation) is\n```math\nY = X\\beta + \\epsilon\n```\n\nContextualized linear regression is\n```math\nY = X\\beta(C) + \\epsilon\n```\nwhere the coefficients $\\beta$ are now a function of context $C$, allowing the model to adapt to context-specific changes. \nContextualized implements this for many types of statistical models, including linear regression, logistic regression, Bayesian networks, correlation networks, and Markov networks.\n\nFor more details, see the [Contextualized Machine Learning whitepaper](https://arxiv.org/abs/2310.11340).\n\n## Quick Start\n\n### Installation\n```\npip install contextualized-ml\n```\n\nTake a look at the [easy demo](docs/models/easy_regression.ipynb) for a quickstart with sklearn-style wrappers.\n\n### Build a Contextualized Model\n```\nfrom contextualized.easy import ContextualizedRegressor\nmodel = ContextualizedRegressor()\nmodel.fit(C, X, Y)\n```\nThis builds a contextualized linear regression model by fitting a deep-learning model to generate context-specific coefficients $\\beta(C)$.\n\n### Predict Context-Specific Parameters\n```\nmodel.predict_params(C)\n```\n\nSee the [docs](https://contextualized.ml/docs) for more examples.\n\n### Important links\n\n- [Documentation](https://contextualized.ml/docs)\n- [Pypi package index](https://pypi.python.org/pypi/contextualized-ml)\n\n## Citing\nIf you use this software, please cite the software [publication](https://doi.org/10.21105/joss.06469):\n```\n@article{Ellington2024,\n doi = {10.21105/joss.06469},\n url = {https://doi.org/10.21105/joss.06469},\n year = {2024},\n publisher = {The Open Journal},\n volume = {9},\n number = {97},\n pages = {6469},\n author = {Caleb N. Ellington and Benjamin J. Lengerich and Wesley Lo and Aaron Alvarez and Andrea Rubbi and Manolis Kellis and Eric P. Xing},\n title = {Contextualized: Heterogeneous Modeling Toolbox},\n journal = {Journal of Open Source Software}\n}\n```\n\n## Contributing\n\nAdd your own contributions by sending a PR or request an improvement by creating an [issue](https://github.com/cnellington/Contextualized/issues). See [CONTRIBUTING.md](https://github.com/cnellington/Contextualized/blob/main/CONTRIBUTING.md) for more info.\n\n## Thanks to all our contributors\n\n<a href=\"https://github.com/cnellington/contextualized/graphs/contributors\">\n <img src=\"https://contributors-img.web.app/image?repo=cnellington/contextualized\" />\n</a>\n\nContextualized ML was originally implemented by [Caleb Ellington](https://calebellington.com/) (CMU) and [Ben Lengerich](http://web.mit.edu/~blengeri/www) (MIT).\n\nBeyond code contributions, many people have helped. Check out [ACKNOWLEDGEMENTS.md](https://github.com/cnellington/Contextualized/blob/main/ACKNOWLEDGEMENTS.md)!\n\n## Related Publications and Pre-prints\n- [Contextualized Machine Learning](https://arxiv.org/abs/2310.11340)\n- [Contextualized: Heterogeneous Modeling Toolbox](https://doi.org/10.21105/joss.06469)\n- Networks\n - [Contextualized Networks Reveal Heterogeneous Transcriptomic Regulation in Tumors at Sample-Specific Resolution](https://www.biorxiv.org/content/10.1101/2023.12.01.569658v1)\n - [NOTMAD: Estimating Bayesian Networks with Sample-Specific Structures and Parameters](http://arxiv.org/abs/2111.01104)\n- Applications\n - [Patient-Specific Models of Treatment Effects Explain Heterogeneity in Tuberculosis](https://arxiv.org/abs/2411.10645)\n - [Contextualized Policy Recovery: Modeling and Interpreting Medical Decisions with Adaptive Imitation Learning](https://arxiv.org/abs/2310.07918)\n - [Automated Interpretable Discovery of Heterogeneous Treatment Effectiveness: A COVID-19 Case Study](https://www.sciencedirect.com/science/article/pii/S1532046422001022)\n - [Discriminative Subtyping of Lung Cancers from Histopathology Images via Contextual Deep Learning](https://www.medrxiv.org/content/10.1101/2020.06.25.20140053v1.abstract)\n - [Personalized Survival Prediction with Contextual Explanation Networks](http://arxiv.org/abs/1801.09810)\n - [Contextual Explanation Networks](https://jmlr.org/papers/v21/18-856.html)\n- Background reading:\n - [Varying-Coefficient Models](https://academic.oup.com/jrsssb/article-abstract/55/4/757/7028270)\n\n\n## Videos\n- [Cold Spring Harbor Laboratory: Contextualized Graphical Models Reveal Sample-Specific Transcriptional Networks for 7000 Tumors](https://www.youtube.com/watch?v=MTcjFK-YwCw)\n- [Sample-Specific Models for Interpretable Analysis with Applications to Disease Subtyping](http://www.birs.ca/events/2022/5-day-workshops/22w5055/videos/watch/202205051559-Lengerich.html)\n\n## Contact Us\nPlease get in touch with any questions, feature requests, or applications by using the [GitHub discussions page](https://github.com/cnellington/Contextualized/discussions).\n",
"bugtrack_url": null,
"license": null,
"summary": "A statistical machine learning toolbox for estimating models, distributions, and functions with sample-specific parameters.",
"version": "0.2.9",
"project_urls": {
"Homepage": "https://contextualized.ml/",
"Source": "https://github.com/cnellington/Contextualized/"
},
"split_keywords": [
"contextual modeling",
" graphical models",
" machine learning",
" meta-learning",
" multitask learning"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "22d4238ad5893ca3b2590497f3600022663ae5c2836c1fbe62b18ed9e5d1721d",
"md5": "e928a4d1153c9854725fdc0b68568360",
"sha256": "6909c287c623cbeccf489ed6547819bd52df17b59ac4439f6b095b15b3861c12"
},
"downloads": -1,
"filename": "contextualized_ml-0.2.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e928a4d1153c9854725fdc0b68568360",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 72429,
"upload_time": "2025-02-02T17:02:56",
"upload_time_iso_8601": "2025-02-02T17:02:56.735244Z",
"url": "https://files.pythonhosted.org/packages/22/d4/238ad5893ca3b2590497f3600022663ae5c2836c1fbe62b18ed9e5d1721d/contextualized_ml-0.2.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "45fbb43495f3d9965c119b5b1862f97127d78614e27b6731a2da30e9f95e93df",
"md5": "7fb2b134ebd8d9dbc6d2fdb5ba6a002a",
"sha256": "5540ce46b680d4ee8910c93621eaa1d1b5faa4d72e59451260c6579fb5c37338"
},
"downloads": -1,
"filename": "contextualized_ml-0.2.9.tar.gz",
"has_sig": false,
"md5_digest": "7fb2b134ebd8d9dbc6d2fdb5ba6a002a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 19705051,
"upload_time": "2025-02-02T17:02:59",
"upload_time_iso_8601": "2025-02-02T17:02:59.650196Z",
"url": "https://files.pythonhosted.org/packages/45/fb/b43495f3d9965c119b5b1862f97127d78614e27b6731a2da30e9f95e93df/contextualized_ml-0.2.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-02 17:02:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cnellington",
"github_project": "Contextualized",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "contextualized-ml"
}