{"id":24946615,"url":"https://github.com/avoss84/bayes-anomaly","last_synced_at":"2025-04-10T05:44:28.941Z","repository":{"id":61060704,"uuid":"547838490","full_name":"AVoss84/bayes-anomaly","owner":"AVoss84","description":"A Python library for explainable Bayesian Anomaly Detection","archived":false,"fork":false,"pushed_at":"2025-01-26T00:31:57.000Z","size":4368,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T06:54:45.642Z","etag":null,"topics":["anomaly-detection","bayesian-inference","explainability","unsupervised-machine-learning"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AVoss84.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-08T11:49:39.000Z","updated_at":"2025-01-26T00:31:53.000Z","dependencies_parsed_at":"2023-11-08T12:29:31.443Z","dependency_job_id":"53df9925-4528-4145-885d-05f003836177","html_url":"https://github.com/AVoss84/bayes-anomaly","commit_stats":null,"previous_names":["avoss84/bayes-anomaly","avoss84/bhad"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AVoss84%2Fbayes-anomaly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AVoss84%2Fbayes-anomaly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AVoss84%2Fbayes-anomaly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AVoss84%2Fbayes-anomaly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AVoss84","download_url":"https://codeload.github.com/AVoss84/bayes-anomaly/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166881,"owners_count":21058479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["anomaly-detection","bayesian-inference","explainability","unsupervised-machine-learning"],"created_at":"2025-02-02T20:57:55.107Z","updated_at":"2025-04-10T05:44:28.923Z","avatar_url":"https://github.com/AVoss84.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🔥 *Bayesian Histogram Anomaly Detection (BHAD)* 🔥\n\nPython implementation of the *Bayesian Histogram-based Anomaly Detection (BHAD)* algorithm, see [Vosseler, A. (2022): Unsupervised Insurance Fraud Prediction Based on Anomaly Detector Ensembles](https://www.researchgate.net/publication/361463552_Unsupervised_Insurance_Fraud_Prediction_Based_on_Anomaly_Detector_Ensembles) and [Vosseler, A. (2023): BHAD: Explainable anomaly detection using Bayesian histograms](https://www.researchgate.net/publication/364265660_BHAD_Explainable_anomaly_detection_using_Bayesian_histograms). The package was presented at *PyCon DE \u0026 PyData Berlin 2023* ([watch talk here](https://www.youtube.com/watch?v=_8zfgPTD-d8\u0026list=PLGVZCDnMOq0peDguAzds7kVmBr8avp46K\u0026index=8)) and at the *42nd International Workshop on Bayesian Inference and Maximum Entropy Methods in Science and Engineering* ([MaxEnt 2023](https://www.mdpi.com/2673-9984/9/1/1)), at Max-Planck-Institute for Plasma Physics, Garching, Germany. \n\n## Package installation\n\nWe opt here for using [*uv*](https://github.com/astral-sh/uv) as a package manager due to its speed and stability, but the same installation works using *pip* with *venv* for Python 3.12: \n```bash\n# curl -LsSf https://astral.sh/uv/install.sh | sh       # Optional: install uv for the first time\nuv venv .env_bhad --python 3.12                         # create the usual virtual environment\nsource .env_bhad/bin/activate\n```\n\nFor local development (only):\n```bash\nuv pip install -r pyproject.toml  \nuv pip install -e .\n```\n\nInstall directly from PyPi:\n```bash\nuv pip install bhad                                       \n```\n\n\n## Model usage\n\n1.) Preprocess the input data: discretize continuous features and conduct Bayesian model selection (*optional*).\n\n2.) Train the model using discrete data.\n\nFor convenience these two steps can be wrapped up via a scikit-learn pipeline (*optional*). \n\n```python\nfrom sklearn.pipeline import Pipeline\nfrom bhad.model import BHAD\nfrom bhad.utils import Discretize\n\nnum_cols = [....]   # names of numeric features\ncat_cols = [....]   # categorical features\n\n# Setting nbins = None infers the Bayes-optimal number of bins (=only parameter)\n# using the MAP estimate\npipe = Pipeline(steps=[\n   ('discrete', Discretize(nbins = None)),   \n   ('model', BHAD(contamination = 0.01, num_features = num_cols, cat_features = cat_cols))\n])\n```\n\nFor a given dataset get binary model decisons and anomaly scores:\n\n```python\ny_pred = pipe.fit_predict(X = dataset)        \n\nanomaly_scores = pipe.decision_function(dataset)\n```\n\nGet *global* model explanation as well as for *individual* observations:\n\n```python\nfrom bhad.explainer import Explainer\n\nlocal_expl = Explainer(bhad_obj = pipe.named_steps['model'], discretize_obj = pipe.named_steps['discrete']).fit()\n\nlocal_expl.get_explanation(nof_feat_expl = 5, append = False)          # individual explanations\n\nprint(local_expl.global_feat_imp)                                      # global explanation\n```\n\nA detailed *toy example* using synthetic data can be found [here](https://github.com/AVoss84/bhad/blob/main/src/notebooks/Toy_Example.ipynb). An example using the Titanic dataset illustrating *model explanability* with BHAD can be found [here](https://github.com/AVoss84/bhad/blob/main/src/notebooks/Titanic_Example.ipynb).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favoss84%2Fbayes-anomaly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Favoss84%2Fbayes-anomaly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favoss84%2Fbayes-anomaly/lists"}