日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PGM---pgmpy学习

發布時間:2024/1/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

In [3]: >>> model = BayesianModel([('A', 'C'), ('B', 'C')]) >>> estimator = BayesianEstimator(model, data) >>> cpd_C = estimator.estimate_cpd('C', prior_type="dirichlet", pseudo_counts=[1, 2]) >>> print(cpd_C)

╒══════╤══════╤══════╤══════╤════════════════════╕
│ 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 │
╘══════╧══════╧══════╧══════╧════════════════════╛

In [4]: print(estimator.get_parameters(prior_type='BDeu', equivalent_sample_size=5)) [<TabularCPD representing P(A:2) at 0x7f86a42021d0>, <TabularCPD representing P(C:2 | A:2, B:2) at 0x7f86a4202940>, <TabularCPD representing P(B:2) at 0x7f86a42026d8>]

錯誤記錄

在使用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学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。