diff options
| author | CoprDistGit <infra@openeuler.org> | 2023-04-12 04:29:26 +0000 |
|---|---|---|
| committer | CoprDistGit <infra@openeuler.org> | 2023-04-12 04:29:26 +0000 |
| commit | 3fca0c2eb0a59da095728521984a361b29c2d5af (patch) | |
| tree | 464a6d17cad684c64c84fc36e44d02523a93638d /python-bnlearn.spec | |
| parent | 31ba116f7056118d84f5d6b1b53a71869bf4dc77 (diff) | |
automatic import of python-bnlearn
Diffstat (limited to 'python-bnlearn.spec')
| -rw-r--r-- | python-bnlearn.spec | 1015 |
1 files changed, 1015 insertions, 0 deletions
diff --git a/python-bnlearn.spec b/python-bnlearn.spec new file mode 100644 index 0000000..c33dc64 --- /dev/null +++ b/python-bnlearn.spec @@ -0,0 +1,1015 @@ +%global _empty_manifest_terminate_build 0 +Name: python-bnlearn +Version: 0.7.14 +Release: 1 +Summary: Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods. +License: MIT License +URL: https://erdogant.github.io/bnlearn +Source0: https://mirrors.nju.edu.cn/pypi/web/packages/15/ec/4bda7df6fb11131364b02e2f1dc3a8f7b13935a28ab2fd3af8b3bf98dd53/bnlearn-0.7.14.tar.gz +BuildArch: noarch + +Requires: python3-pgmpy +Requires: python3-networkx +Requires: python3-matplotlib +Requires: python3-numpy +Requires: python3-pandas +Requires: python3-tqdm +Requires: python3-ismember +Requires: python3-scikit-learn +Requires: python3-funcsigs +Requires: python3-statsmodels +Requires: python3-louvain +Requires: python3-packaging +Requires: python3-df2onehot +Requires: python3-fsspec +Requires: python3-pypickle +Requires: python3-tabulate +Requires: python3-ipywidgets +Requires: python3-pyvis +Requires: python3-requests + +%description +# bnlearn - Library for Bayesian network learning and inference + +[](https://img.shields.io/pypi/pyversions/bnlearn) +[](https://pypi.org/project/bnlearn/) + +[](https://github.com/erdogant/bnlearn/blob/master/LICENSE) +[](https://github.com/erdogant/bnlearn/network) +[](https://github.com/erdogant/bnlearn/issues) +[](http://www.repostatus.org/#active) +[](https://pepy.tech/project/bnlearn/) +[](https://pepy.tech/project/bnlearn) +[](https://zenodo.org/badge/latestdoi/231263493) +[](https://erdogant.github.io/bnlearn/) +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#medium-blog) + +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#) +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#colab-notebook) +<!---[](https://www.buymeacoffee.com/erdogant)--> +<!---[](https://erdogant.github.io/donate/?currency=USD&amount=5)--> + + +### + +``bnlearn`` is Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods. +Because probabilistic graphical models can be difficult in usage, Bnlearn for python (this package) is build on the <a href="https://github.com/pgmpy/pgmpy">pgmpy</a> package and contains the most-wanted pipelines. Navigate to [API documentations](https://erdogant.github.io/bnlearn/) for more detailed information. + +# +**⭐️ Star this repo if you like it ⭐️** +# + +### Blogs + +Read the blogs to get a structured overview of bayesian methods and detailed usage of ``bnlearn``. + +* [Step-by-step guide for structure learning.](https://towardsdatascience.com/a-step-by-step-guide-in-detecting-causal-relationships-using-bayesian-structure-learning-in-python-c20c6b31cee5) +* [Step-by-step guide for knowledge-driven models.](https://towardsdatascience.com/a-step-by-step-guide-in-designing-knowledge-driven-models-using-bayesian-theorem-7433f6fd64be) + +# + + +### [Documentation pages](https://erdogant.github.io/bnlearn/) + +On the [documentation pages](https://erdogant.github.io/bnlearn/) you can find detailed information about the working of the ``bnlearn`` with many examples. + +# + +### Installation + +##### It is advisable to create a new environment (e.g. with Conda). +```bash +conda create -n env_bnlearn python=3.8 +conda activate env_bnlearn +``` + +##### Install bnlearn from PyPI +```bash +pip install bnlearn +``` + +##### Install bnlearn from github source +```bash +pip install git+https://github.com/erdogant/bnlearn +``` + +##### The following functions are available after installation: + +```python +# Import library +import bnlearn as bn + +# Structure learning +bn.structure_learning.fit() + +# Compute edge strength with the test statistic +bn.independence_test(model, df, test='chi_square', prune=True) + +# Parameter learning +bn.parameter_learning.fit() + +# Inference +bn.inference.fit() + +# Make predictions +bn.predict() + +# Based on a DAG, you can sample the number of samples you want. +bn.sampling() + +# Load well known examples to play arround with or load your own .bif file. +bn.import_DAG() + +# Load simple dataframe of sprinkler dataset. +bn.import_example() + +# Compare 2 graphs +bn.compare_networks() + +# Plot graph +bn.plot() + +# To make the directed grapyh undirected +bn.to_undirected() + +# Convert to one-hot datamatrix +bn.df2onehot() + +# Derive the topological ordering of the (entire) graph +bn.topological_sort() + +# See below for the exact working of the functions +``` + +##### The following methods are also included: +* inference +* sampling +* comparing two networks +* loading bif files +* conversion of directed to undirected graphs + + +# + +### Method overview +Learning a Bayesian network can be split into the underneath problems which are all implemented in this package: + +* **Structure learning**: Given the data: Estimate a DAG that captures the dependencies between the variables. + * There are multiple manners to perform structure learning. + * Exhaustivesearch + * Hillclimbsearch + * NaiveBayes + * TreeSearch + * Chow-liu + * Tree-augmented Naive Bayes (TAN) + +* **Parameter learning**: Given the data and DAG: Estimate the (conditional) probability distributions of the individual variables. +* **Inference**: Given the learned model: Determine the exact probability values for your queries. + +# + + +### Examples + +A structured overview of all examples are now available on the [documentation pages](https://erdogant.github.io/bnlearn/). + +##### Structure learning + +* [Example: Learn structure on the Sprinkler dataset based on a simple dataframe](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-1) + +* [Example: Comparison method and scoring types types for structure learning](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-2) + +* [Example: Learn structure on more complex dataset (Asia)](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-3) + +##### Parameter learning + +* [Example: Parameter learning using a DAG and dataframe](https://erdogant.github.io/bnlearn/pages/html/Examples.html#parameter-learning) + + +##### Inferences + +* [Example: Make predictions on a dataframe using inference](https://erdogant.github.io/bnlearn/pages/html/Predict.html) + + +##### Sampling + +* [Example: Sampling to create datasets](https://erdogant.github.io/bnlearn/pages/html/Sampling%20and%20datasets.html) + + +##### Complete examples + +* [Example: Create a Bayesian Network, learn its parameters from data and perform the inference](https://erdogant.github.io/bnlearn/pages/html/Examples.html#create-a-bayesian-network-learn-its-parameters-from-data-and-perform-the-inference) + +* [Example: Use case in the medical domain](https://erdogant.github.io/bnlearn/pages/html/UseCases.html) + +* [Example: Use case Titanic](https://erdogant.github.io/bnlearn/pages/html/UseCases.html#) + + + +##### Plotting +* [Example: Interactive plotting](https://erdogant.github.io/bnlearn/pages/html/Plot.html#) + +* [Example: Static plotting](https://erdogant.github.io/bnlearn/pages/html/Plot.html#static-plot) + +* [Example: Comparison of two networks](https://erdogant.github.io/bnlearn/pages/html/Plot.html#comparison-of-two-networks) + +##### Various + +* [Example: Saving and loading of bnlearn models](https://erdogant.github.io/bnlearn/pages/html/saving%20and%20loading.html) + +* [Example: Data conversions such as creating sparse datamatrix from source-target and weights](https://erdogant.github.io/bnlearn/pages/html/dataframe%20conversions.html?highlight=target#) + +* [Example: Load DAG from BIF files](https://erdogant.github.io/bnlearn/pages/html/Examples.html?highlight=comparison#import-from-bif) + + + # + +### Various basic examples + + +```python + + import bnlearn as bn + # Example dataframe sprinkler_data.csv can be loaded with: + df = bn.import_example() + # df = pd.read_csv('sprinkler_data.csv') +``` + +##### df looks like this + +```python + +Cloudy Sprinkler Rain Wet_Grass +0 0 1 0 1 +1 1 1 1 1 +2 1 0 1 1 +3 0 0 1 1 +4 1 0 1 1 +.. ... ... ... ... +995 0 0 0 0 +996 1 0 0 0 +997 0 0 1 0 +998 1 1 0 1 +999 1 0 1 1 + +``` + +```python + + model = bn.structure_learning.fit(df) + # Compute edge strength with the chi_square test statistic + model = bn.independence_test(model, df) + G = bn.plot(model) +``` + +<p align="center"> + <img src="https://github.com/erdogant/bnlearn/blob/master/docs/figs/fig_sprinkler_sl.png" width="600" /> +</p> + +* Choosing various methodtypes and scoringtypes: + +```python + + model_hc_bic = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic') + model_hc_k2 = bn.structure_learning.fit(df, methodtype='hc', scoretype='k2') + model_hc_bdeu = bn.structure_learning.fit(df, methodtype='hc', scoretype='bdeu') + model_ex_bic = bn.structure_learning.fit(df, methodtype='ex', scoretype='bic') + model_ex_k2 = bn.structure_learning.fit(df, methodtype='ex', scoretype='k2') + model_ex_bdeu = bn.structure_learning.fit(df, methodtype='ex', scoretype='bdeu') + model_cl = bn.structure_learning.fit(df, methodtype='cl', root_node='Wet_Grass') + model_tan = bn.structure_learning.fit(df, methodtype='tan', root_node='Wet_Grass', class_node='Rain') +``` + +## Example: Parameter Learning +```python + import bnlearn as bn + # Import dataframe + df = bn.import_example() + # As an example we set the CPD at False which returns an "empty" DAG + model = bn.import_DAG('sprinkler', CPD=False) + # Now we learn the parameters of the DAG using the df + model_update = bn.parameter_learning.fit(model, df) + # Make plot + G = bn.plot(model_update) +``` + +## Example: Inference +```python + import bnlearn as bn + model = bn.import_DAG('sprinkler') + query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1,'Sprinkler':0, 'Wet_Grass':1}) + print(query) + print(query.df) + + # Lets try another inference + query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1}) + print(query) + print(query.df) + +``` + +<hr> + +### References +* https://erdogant.github.io/bnlearn/ +* http://pgmpy.org +* https://programtalk.com/python-examples/pgmpy.factors.discrete.TabularCPD/ +* http://www.bnlearn.com/bnrepository/ + + +### Contributors +Setting up and maintaining bnlearn has been possible thanks to users and contributors. Thanks to: + +<p align="left"> + <a href="https://github.com/erdogant/bnlearn/graphs/contributors"> + <img src="https://contrib.rocks/image?repo=erdogant/bnlearn" /> + </a> +</p> + + +### Citation +Please cite ``bnlearn`` in your publications if this is useful for your research. See column right for citation information. + +### Maintainer +* Erdogan Taskesen, github: [erdogant](https://github.com/erdogant) +* Contributions are welcome. +* If you wish to buy me a <a href="https://erdogant.github.io/donate/?currency=USD&amount=5">Coffee</a> for this work, it is very appreciated :) + + + + + +%package -n python3-bnlearn +Summary: Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods. +Provides: python-bnlearn +BuildRequires: python3-devel +BuildRequires: python3-setuptools +BuildRequires: python3-pip +%description -n python3-bnlearn +# bnlearn - Library for Bayesian network learning and inference + +[](https://img.shields.io/pypi/pyversions/bnlearn) +[](https://pypi.org/project/bnlearn/) + +[](https://github.com/erdogant/bnlearn/blob/master/LICENSE) +[](https://github.com/erdogant/bnlearn/network) +[](https://github.com/erdogant/bnlearn/issues) +[](http://www.repostatus.org/#active) +[](https://pepy.tech/project/bnlearn/) +[](https://pepy.tech/project/bnlearn) +[](https://zenodo.org/badge/latestdoi/231263493) +[](https://erdogant.github.io/bnlearn/) +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#medium-blog) + +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#) +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#colab-notebook) +<!---[](https://www.buymeacoffee.com/erdogant)--> +<!---[](https://erdogant.github.io/donate/?currency=USD&amount=5)--> + + +### + +``bnlearn`` is Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods. +Because probabilistic graphical models can be difficult in usage, Bnlearn for python (this package) is build on the <a href="https://github.com/pgmpy/pgmpy">pgmpy</a> package and contains the most-wanted pipelines. Navigate to [API documentations](https://erdogant.github.io/bnlearn/) for more detailed information. + +# +**⭐️ Star this repo if you like it ⭐️** +# + +### Blogs + +Read the blogs to get a structured overview of bayesian methods and detailed usage of ``bnlearn``. + +* [Step-by-step guide for structure learning.](https://towardsdatascience.com/a-step-by-step-guide-in-detecting-causal-relationships-using-bayesian-structure-learning-in-python-c20c6b31cee5) +* [Step-by-step guide for knowledge-driven models.](https://towardsdatascience.com/a-step-by-step-guide-in-designing-knowledge-driven-models-using-bayesian-theorem-7433f6fd64be) + +# + + +### [Documentation pages](https://erdogant.github.io/bnlearn/) + +On the [documentation pages](https://erdogant.github.io/bnlearn/) you can find detailed information about the working of the ``bnlearn`` with many examples. + +# + +### Installation + +##### It is advisable to create a new environment (e.g. with Conda). +```bash +conda create -n env_bnlearn python=3.8 +conda activate env_bnlearn +``` + +##### Install bnlearn from PyPI +```bash +pip install bnlearn +``` + +##### Install bnlearn from github source +```bash +pip install git+https://github.com/erdogant/bnlearn +``` + +##### The following functions are available after installation: + +```python +# Import library +import bnlearn as bn + +# Structure learning +bn.structure_learning.fit() + +# Compute edge strength with the test statistic +bn.independence_test(model, df, test='chi_square', prune=True) + +# Parameter learning +bn.parameter_learning.fit() + +# Inference +bn.inference.fit() + +# Make predictions +bn.predict() + +# Based on a DAG, you can sample the number of samples you want. +bn.sampling() + +# Load well known examples to play arround with or load your own .bif file. +bn.import_DAG() + +# Load simple dataframe of sprinkler dataset. +bn.import_example() + +# Compare 2 graphs +bn.compare_networks() + +# Plot graph +bn.plot() + +# To make the directed grapyh undirected +bn.to_undirected() + +# Convert to one-hot datamatrix +bn.df2onehot() + +# Derive the topological ordering of the (entire) graph +bn.topological_sort() + +# See below for the exact working of the functions +``` + +##### The following methods are also included: +* inference +* sampling +* comparing two networks +* loading bif files +* conversion of directed to undirected graphs + + +# + +### Method overview +Learning a Bayesian network can be split into the underneath problems which are all implemented in this package: + +* **Structure learning**: Given the data: Estimate a DAG that captures the dependencies between the variables. + * There are multiple manners to perform structure learning. + * Exhaustivesearch + * Hillclimbsearch + * NaiveBayes + * TreeSearch + * Chow-liu + * Tree-augmented Naive Bayes (TAN) + +* **Parameter learning**: Given the data and DAG: Estimate the (conditional) probability distributions of the individual variables. +* **Inference**: Given the learned model: Determine the exact probability values for your queries. + +# + + +### Examples + +A structured overview of all examples are now available on the [documentation pages](https://erdogant.github.io/bnlearn/). + +##### Structure learning + +* [Example: Learn structure on the Sprinkler dataset based on a simple dataframe](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-1) + +* [Example: Comparison method and scoring types types for structure learning](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-2) + +* [Example: Learn structure on more complex dataset (Asia)](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-3) + +##### Parameter learning + +* [Example: Parameter learning using a DAG and dataframe](https://erdogant.github.io/bnlearn/pages/html/Examples.html#parameter-learning) + + +##### Inferences + +* [Example: Make predictions on a dataframe using inference](https://erdogant.github.io/bnlearn/pages/html/Predict.html) + + +##### Sampling + +* [Example: Sampling to create datasets](https://erdogant.github.io/bnlearn/pages/html/Sampling%20and%20datasets.html) + + +##### Complete examples + +* [Example: Create a Bayesian Network, learn its parameters from data and perform the inference](https://erdogant.github.io/bnlearn/pages/html/Examples.html#create-a-bayesian-network-learn-its-parameters-from-data-and-perform-the-inference) + +* [Example: Use case in the medical domain](https://erdogant.github.io/bnlearn/pages/html/UseCases.html) + +* [Example: Use case Titanic](https://erdogant.github.io/bnlearn/pages/html/UseCases.html#) + + + +##### Plotting +* [Example: Interactive plotting](https://erdogant.github.io/bnlearn/pages/html/Plot.html#) + +* [Example: Static plotting](https://erdogant.github.io/bnlearn/pages/html/Plot.html#static-plot) + +* [Example: Comparison of two networks](https://erdogant.github.io/bnlearn/pages/html/Plot.html#comparison-of-two-networks) + +##### Various + +* [Example: Saving and loading of bnlearn models](https://erdogant.github.io/bnlearn/pages/html/saving%20and%20loading.html) + +* [Example: Data conversions such as creating sparse datamatrix from source-target and weights](https://erdogant.github.io/bnlearn/pages/html/dataframe%20conversions.html?highlight=target#) + +* [Example: Load DAG from BIF files](https://erdogant.github.io/bnlearn/pages/html/Examples.html?highlight=comparison#import-from-bif) + + + # + +### Various basic examples + + +```python + + import bnlearn as bn + # Example dataframe sprinkler_data.csv can be loaded with: + df = bn.import_example() + # df = pd.read_csv('sprinkler_data.csv') +``` + +##### df looks like this + +```python + +Cloudy Sprinkler Rain Wet_Grass +0 0 1 0 1 +1 1 1 1 1 +2 1 0 1 1 +3 0 0 1 1 +4 1 0 1 1 +.. ... ... ... ... +995 0 0 0 0 +996 1 0 0 0 +997 0 0 1 0 +998 1 1 0 1 +999 1 0 1 1 + +``` + +```python + + model = bn.structure_learning.fit(df) + # Compute edge strength with the chi_square test statistic + model = bn.independence_test(model, df) + G = bn.plot(model) +``` + +<p align="center"> + <img src="https://github.com/erdogant/bnlearn/blob/master/docs/figs/fig_sprinkler_sl.png" width="600" /> +</p> + +* Choosing various methodtypes and scoringtypes: + +```python + + model_hc_bic = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic') + model_hc_k2 = bn.structure_learning.fit(df, methodtype='hc', scoretype='k2') + model_hc_bdeu = bn.structure_learning.fit(df, methodtype='hc', scoretype='bdeu') + model_ex_bic = bn.structure_learning.fit(df, methodtype='ex', scoretype='bic') + model_ex_k2 = bn.structure_learning.fit(df, methodtype='ex', scoretype='k2') + model_ex_bdeu = bn.structure_learning.fit(df, methodtype='ex', scoretype='bdeu') + model_cl = bn.structure_learning.fit(df, methodtype='cl', root_node='Wet_Grass') + model_tan = bn.structure_learning.fit(df, methodtype='tan', root_node='Wet_Grass', class_node='Rain') +``` + +## Example: Parameter Learning +```python + import bnlearn as bn + # Import dataframe + df = bn.import_example() + # As an example we set the CPD at False which returns an "empty" DAG + model = bn.import_DAG('sprinkler', CPD=False) + # Now we learn the parameters of the DAG using the df + model_update = bn.parameter_learning.fit(model, df) + # Make plot + G = bn.plot(model_update) +``` + +## Example: Inference +```python + import bnlearn as bn + model = bn.import_DAG('sprinkler') + query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1,'Sprinkler':0, 'Wet_Grass':1}) + print(query) + print(query.df) + + # Lets try another inference + query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1}) + print(query) + print(query.df) + +``` + +<hr> + +### References +* https://erdogant.github.io/bnlearn/ +* http://pgmpy.org +* https://programtalk.com/python-examples/pgmpy.factors.discrete.TabularCPD/ +* http://www.bnlearn.com/bnrepository/ + + +### Contributors +Setting up and maintaining bnlearn has been possible thanks to users and contributors. Thanks to: + +<p align="left"> + <a href="https://github.com/erdogant/bnlearn/graphs/contributors"> + <img src="https://contrib.rocks/image?repo=erdogant/bnlearn" /> + </a> +</p> + + +### Citation +Please cite ``bnlearn`` in your publications if this is useful for your research. See column right for citation information. + +### Maintainer +* Erdogan Taskesen, github: [erdogant](https://github.com/erdogant) +* Contributions are welcome. +* If you wish to buy me a <a href="https://erdogant.github.io/donate/?currency=USD&amount=5">Coffee</a> for this work, it is very appreciated :) + + + + + +%package help +Summary: Development documents and examples for bnlearn +Provides: python3-bnlearn-doc +%description help +# bnlearn - Library for Bayesian network learning and inference + +[](https://img.shields.io/pypi/pyversions/bnlearn) +[](https://pypi.org/project/bnlearn/) + +[](https://github.com/erdogant/bnlearn/blob/master/LICENSE) +[](https://github.com/erdogant/bnlearn/network) +[](https://github.com/erdogant/bnlearn/issues) +[](http://www.repostatus.org/#active) +[](https://pepy.tech/project/bnlearn/) +[](https://pepy.tech/project/bnlearn) +[](https://zenodo.org/badge/latestdoi/231263493) +[](https://erdogant.github.io/bnlearn/) +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#medium-blog) + +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#) +[](https://erdogant.github.io/bnlearn/pages/html/Documentation.html#colab-notebook) +<!---[](https://www.buymeacoffee.com/erdogant)--> +<!---[](https://erdogant.github.io/donate/?currency=USD&amount=5)--> + + +### + +``bnlearn`` is Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods. +Because probabilistic graphical models can be difficult in usage, Bnlearn for python (this package) is build on the <a href="https://github.com/pgmpy/pgmpy">pgmpy</a> package and contains the most-wanted pipelines. Navigate to [API documentations](https://erdogant.github.io/bnlearn/) for more detailed information. + +# +**⭐️ Star this repo if you like it ⭐️** +# + +### Blogs + +Read the blogs to get a structured overview of bayesian methods and detailed usage of ``bnlearn``. + +* [Step-by-step guide for structure learning.](https://towardsdatascience.com/a-step-by-step-guide-in-detecting-causal-relationships-using-bayesian-structure-learning-in-python-c20c6b31cee5) +* [Step-by-step guide for knowledge-driven models.](https://towardsdatascience.com/a-step-by-step-guide-in-designing-knowledge-driven-models-using-bayesian-theorem-7433f6fd64be) + +# + + +### [Documentation pages](https://erdogant.github.io/bnlearn/) + +On the [documentation pages](https://erdogant.github.io/bnlearn/) you can find detailed information about the working of the ``bnlearn`` with many examples. + +# + +### Installation + +##### It is advisable to create a new environment (e.g. with Conda). +```bash +conda create -n env_bnlearn python=3.8 +conda activate env_bnlearn +``` + +##### Install bnlearn from PyPI +```bash +pip install bnlearn +``` + +##### Install bnlearn from github source +```bash +pip install git+https://github.com/erdogant/bnlearn +``` + +##### The following functions are available after installation: + +```python +# Import library +import bnlearn as bn + +# Structure learning +bn.structure_learning.fit() + +# Compute edge strength with the test statistic +bn.independence_test(model, df, test='chi_square', prune=True) + +# Parameter learning +bn.parameter_learning.fit() + +# Inference +bn.inference.fit() + +# Make predictions +bn.predict() + +# Based on a DAG, you can sample the number of samples you want. +bn.sampling() + +# Load well known examples to play arround with or load your own .bif file. +bn.import_DAG() + +# Load simple dataframe of sprinkler dataset. +bn.import_example() + +# Compare 2 graphs +bn.compare_networks() + +# Plot graph +bn.plot() + +# To make the directed grapyh undirected +bn.to_undirected() + +# Convert to one-hot datamatrix +bn.df2onehot() + +# Derive the topological ordering of the (entire) graph +bn.topological_sort() + +# See below for the exact working of the functions +``` + +##### The following methods are also included: +* inference +* sampling +* comparing two networks +* loading bif files +* conversion of directed to undirected graphs + + +# + +### Method overview +Learning a Bayesian network can be split into the underneath problems which are all implemented in this package: + +* **Structure learning**: Given the data: Estimate a DAG that captures the dependencies between the variables. + * There are multiple manners to perform structure learning. + * Exhaustivesearch + * Hillclimbsearch + * NaiveBayes + * TreeSearch + * Chow-liu + * Tree-augmented Naive Bayes (TAN) + +* **Parameter learning**: Given the data and DAG: Estimate the (conditional) probability distributions of the individual variables. +* **Inference**: Given the learned model: Determine the exact probability values for your queries. + +# + + +### Examples + +A structured overview of all examples are now available on the [documentation pages](https://erdogant.github.io/bnlearn/). + +##### Structure learning + +* [Example: Learn structure on the Sprinkler dataset based on a simple dataframe](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-1) + +* [Example: Comparison method and scoring types types for structure learning](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-2) + +* [Example: Learn structure on more complex dataset (Asia)](https://erdogant.github.io/bnlearn/pages/html/Examples.html#example-3) + +##### Parameter learning + +* [Example: Parameter learning using a DAG and dataframe](https://erdogant.github.io/bnlearn/pages/html/Examples.html#parameter-learning) + + +##### Inferences + +* [Example: Make predictions on a dataframe using inference](https://erdogant.github.io/bnlearn/pages/html/Predict.html) + + +##### Sampling + +* [Example: Sampling to create datasets](https://erdogant.github.io/bnlearn/pages/html/Sampling%20and%20datasets.html) + + +##### Complete examples + +* [Example: Create a Bayesian Network, learn its parameters from data and perform the inference](https://erdogant.github.io/bnlearn/pages/html/Examples.html#create-a-bayesian-network-learn-its-parameters-from-data-and-perform-the-inference) + +* [Example: Use case in the medical domain](https://erdogant.github.io/bnlearn/pages/html/UseCases.html) + +* [Example: Use case Titanic](https://erdogant.github.io/bnlearn/pages/html/UseCases.html#) + + + +##### Plotting +* [Example: Interactive plotting](https://erdogant.github.io/bnlearn/pages/html/Plot.html#) + +* [Example: Static plotting](https://erdogant.github.io/bnlearn/pages/html/Plot.html#static-plot) + +* [Example: Comparison of two networks](https://erdogant.github.io/bnlearn/pages/html/Plot.html#comparison-of-two-networks) + +##### Various + +* [Example: Saving and loading of bnlearn models](https://erdogant.github.io/bnlearn/pages/html/saving%20and%20loading.html) + +* [Example: Data conversions such as creating sparse datamatrix from source-target and weights](https://erdogant.github.io/bnlearn/pages/html/dataframe%20conversions.html?highlight=target#) + +* [Example: Load DAG from BIF files](https://erdogant.github.io/bnlearn/pages/html/Examples.html?highlight=comparison#import-from-bif) + + + # + +### Various basic examples + + +```python + + import bnlearn as bn + # Example dataframe sprinkler_data.csv can be loaded with: + df = bn.import_example() + # df = pd.read_csv('sprinkler_data.csv') +``` + +##### df looks like this + +```python + +Cloudy Sprinkler Rain Wet_Grass +0 0 1 0 1 +1 1 1 1 1 +2 1 0 1 1 +3 0 0 1 1 +4 1 0 1 1 +.. ... ... ... ... +995 0 0 0 0 +996 1 0 0 0 +997 0 0 1 0 +998 1 1 0 1 +999 1 0 1 1 + +``` + +```python + + model = bn.structure_learning.fit(df) + # Compute edge strength with the chi_square test statistic + model = bn.independence_test(model, df) + G = bn.plot(model) +``` + +<p align="center"> + <img src="https://github.com/erdogant/bnlearn/blob/master/docs/figs/fig_sprinkler_sl.png" width="600" /> +</p> + +* Choosing various methodtypes and scoringtypes: + +```python + + model_hc_bic = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic') + model_hc_k2 = bn.structure_learning.fit(df, methodtype='hc', scoretype='k2') + model_hc_bdeu = bn.structure_learning.fit(df, methodtype='hc', scoretype='bdeu') + model_ex_bic = bn.structure_learning.fit(df, methodtype='ex', scoretype='bic') + model_ex_k2 = bn.structure_learning.fit(df, methodtype='ex', scoretype='k2') + model_ex_bdeu = bn.structure_learning.fit(df, methodtype='ex', scoretype='bdeu') + model_cl = bn.structure_learning.fit(df, methodtype='cl', root_node='Wet_Grass') + model_tan = bn.structure_learning.fit(df, methodtype='tan', root_node='Wet_Grass', class_node='Rain') +``` + +## Example: Parameter Learning +```python + import bnlearn as bn + # Import dataframe + df = bn.import_example() + # As an example we set the CPD at False which returns an "empty" DAG + model = bn.import_DAG('sprinkler', CPD=False) + # Now we learn the parameters of the DAG using the df + model_update = bn.parameter_learning.fit(model, df) + # Make plot + G = bn.plot(model_update) +``` + +## Example: Inference +```python + import bnlearn as bn + model = bn.import_DAG('sprinkler') + query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1,'Sprinkler':0, 'Wet_Grass':1}) + print(query) + print(query.df) + + # Lets try another inference + query = bn.inference.fit(model, variables=['Rain'], evidence={'Cloudy':1}) + print(query) + print(query.df) + +``` + +<hr> + +### References +* https://erdogant.github.io/bnlearn/ +* http://pgmpy.org +* https://programtalk.com/python-examples/pgmpy.factors.discrete.TabularCPD/ +* http://www.bnlearn.com/bnrepository/ + + +### Contributors +Setting up and maintaining bnlearn has been possible thanks to users and contributors. Thanks to: + +<p align="left"> + <a href="https://github.com/erdogant/bnlearn/graphs/contributors"> + <img src="https://contrib.rocks/image?repo=erdogant/bnlearn" /> + </a> +</p> + + +### Citation +Please cite ``bnlearn`` in your publications if this is useful for your research. See column right for citation information. + +### Maintainer +* Erdogan Taskesen, github: [erdogant](https://github.com/erdogant) +* Contributions are welcome. +* If you wish to buy me a <a href="https://erdogant.github.io/donate/?currency=USD&amount=5">Coffee</a> for this work, it is very appreciated :) + + + + + +%prep +%autosetup -n bnlearn-0.7.14 + +%build +%py3_build + +%install +%py3_install +install -d -m755 %{buildroot}/%{_pkgdocdir} +if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi +if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi +if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi +if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi +pushd %{buildroot} +if [ -d usr/lib ]; then + find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/lib64 ]; then + find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/bin ]; then + find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/sbin ]; then + find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst +fi +touch doclist.lst +if [ -d usr/share/man ]; then + find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst +fi +popd +mv %{buildroot}/filelist.lst . +mv %{buildroot}/doclist.lst . + +%files -n python3-bnlearn -f filelist.lst +%dir %{python3_sitelib}/* + +%files help -f doclist.lst +%{_docdir}/* + +%changelog +* Wed Apr 12 2023 Python_Bot <Python_Bot@openeuler.org> - 0.7.14-1 +- Package Spec generated |
