pythonBayesianModel

本文通过使用Python的pgmpy库构建了一个关于餐厅选择的贝叶斯网络模型。该模型考虑了位置(location)、质量(quality)、成本(cost)及顾客数量(no_of_people)等因素之间的相互依赖关系,并定义了条件概率分布(CPD)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type "copyright", "credits" or "license" for more information.

IPython 5.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from pgmpy.models import BayesianModel
   ...: from pgmpy.inference import VariableElimination
   ...: from pgmpy.factors import TabularCPD
   ...:

In [2]: restaurant = BayesianModel([('location', 'cost'),
   ...:                             ('quality', 'cost'),
   ...:                             ('cost', 'no_of_people'),
   ...:                             ('location', 'no_of_people')])
   ...:

In [3]: cpd_location = TabularCPD('location', 2, [[0.6, 0.4]])

In [4]: cpd_quality = TabularCPD('quality', 3, [[0.3, 0.5, 0.2]])

In [5]: cpd_cost = TabularCPD('cost', 2,
   ...:                       [[0.8, 0.6, 0.1, 0.6, 0.6, 0.05],
   ...:                        [0.2, 0.1, 0.9, 0.4, 0.4, 0.95]],
   ...:                       ['location', 'quality'], [2, 3])

In [6]: cpd_no_of_people = TabularCPD('no_of_people', 2,
   ...:                               [[0.6, 0.8, 0.1, 0.6],
   ...:                                [0.4, 0.2, 0.9, 0.4]],
   ...:                               ['cost', 'location'], [2, 2])

In [7]: restaurant.add_cpds(cpd_location, cpd_quality,
   ...:                     cpd_cost, cpd_no_of_people)

In [8]: restaurant_inference = VariableElimination(restaurant)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-8-989252db6069> in <module>()
----> 1 restaurant_inference = VariableElimination(restaurant)

~/Research/pythonStudio/pgmpy/pgmpy/inference/base.py in __init__(self, model)
     55
     56     def __init__(self, model):
---> 57         if not model.check_model():
     58             raise ModelError("Model of type {!r} not valid".format(type(model).__name__))
     59

~/Research/pythonStudio/pgmpy/pgmpy/models/BayesianModel.py in check_model(self)
    241                     raise ValueError("CPD associated with %s doesn't have "
    242                                      "proper parents associated with it." % node)
--> 243                 if not np.allclose(cpd.marginalize(node, inplace=False).values,
    244                                    np.ones(np.product(cpd.evidence_card)),
    245                                    atol=0.01):

~//Research/pythonStudio/pgmpy/pgmpy/factors/CPD.py in marginalize(self, variables, inplace)
    263                                      self.evidence_card)
    264
--> 265         super(TabularCPD, tabular_cpd).marginalize(variables)
    266         tabular_cpd.normalize()
    267

~/Research/pythonStudio/pgmpy/pgmpy/factors/Factor.py in marginalize(self, variables, inplace)
    207         for i in product(*[range(index) for index in assign[assign != -1]]):
    208             assign[assign != -1] = i
--> 209             marginalized_values.append(np.sum(factor.values[factor._index_for_assignment(assign)]))
    210         factor.values = np.array(marginalized_values)
    211         for variable in variables:

~/Research/pythonStudio/pgmpy/pgmpy/factors/Factor.py in _index_for_assignment(self, assignment)
    414         """
    415         assignment = np.array(assignment)
--> 416         card_cumprod = np.delete(np.concatenate((np.array([1]), np.cumprod(self.cardinality[::-1])), axis=1)[::-1], 0)
    417         if -1 in assignment:
    418             indexes = np.where(assignment == -1)[0]

IndexError: axis 1 out of bounds [0, 1)

In [9]:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值