PGM---pgmpy学习
官網
http://pgmpy.org/
github
https://github.com/pgmpy/pgmpy#installation
三種方法安裝
Using conda: $ conda install -c ankurankan pgmpyUsing pip: $ pip install -r requirements.txt # or requirements-dev.txt if you want to run unittests $ pip install pgmpyOr for installing the latest codebase: $ git clone https://github.com/pgmpy/pgmpy $ cd pgmpy/ $ pip install -r requirements.txt $ python setup.py install文檔閱讀
例子01
###創建模型代碼 # coding: utf-8 # In[16]: # Starting with defining the network structure from pgmpy.models import BayesianModel cancer_model = BayesianModel([('Pollution', 'Cancer'), ('Smoker', 'Cancer'),('Cancer', 'Xray'),('Cancer', 'Dyspnoea')]) # In[17]: # Now defining the parameters. from pgmpy.factors.discrete import TabularCPD cpd_poll = TabularCPD(variable='Pollution', variable_card=2,values=[[0.9], [0.1]]) cpd_smoke = TabularCPD(variable='Smoker', variable_card=2,values=[[0.3], [0.7]]) cpd_cancer = TabularCPD(variable='Cancer', variable_card=2,values=[[0.03, 0.05, 0.001, 0.02],[0.97, 0.95, 0.999, 0.98]],evidence=['Smoker', 'Pollution'],evidence_card=[2, 2]) cpd_xray = TabularCPD(variable='Xray', variable_card=2,values=[[0.9, 0.2], [0.1, 0.8]],evidence=['Cancer'], evidence_card=[2]) cpd_dysp = TabularCPD(variable='Dyspnoea', variable_card=2,values=[[0.65, 0.3], [0.35, 0.7]],evidence=['Cancer'], evidence_card=[2]) # In[18]: # Associating the parameters with the model structure. cancer_model.add_cpds(cpd_poll, cpd_smoke, cpd_cancer, cpd_xray, cpd_dysp) # Checking if the cpds are valid for the model. cancer_model.check_model() # In[19]: cancer_model.get_independencies()例02
#Bayesian Estimator In [2]: >>> import pandas as pd >>> from pgmpy.models import BayesianModel >>> from pgmpy.estimators import BayesianEstimator >>> data = pd.DataFrame(data={'A': [0, 0, 1], 'B': [0, 1, 0], 'C': [1, 1, 0]})data.head()
Out[2]:
A B C
0 0 0 1
1 0 1 1
2 1 0 0
╒══════╤══════╤══════╤══════╤════════════════════╕
│ A │ A(0) │ A(0) │ A(1) │ A(1) │
├──────┼──────┼──────┼──────┼────────────────────┤
│ B │ B(0) │ B(1) │ B(0) │ B(1) │
├──────┼──────┼──────┼──────┼────────────────────┤
│ C(0) │ 0.25 │ 0.25 │ 0.5 │ 0.3333333333333333 │
├──────┼──────┼──────┼──────┼────────────────────┤
│ C(1) │ 0.75 │ 0.75 │ 0.5 │ 0.6666666666666666 │
╘══════╧══════╧══════╧══════╧════════════════════╛
錯誤記錄
在使用BeliefPropagation時,使用query過程中,出現以下的報錯,原因是networkx版本問題,這個可查看github的
requirements.txt: networkx==1.11 numpy==1.11.3 scipy==0.18.1 pandas==0.19.2 pyparsing==2.2 wrapt==1.10.8總結
以上是生活随笔為你收集整理的PGM---pgmpy学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LCD1602教程(一)
- 下一篇: 突破存储边界 赋能开放计算 | 西部数据