Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在给定特定输入的情况下,如何实现贝叶斯网络的枚举算法?_Python_Algorithm_Bayesian Networks - Fatal编程技术网

Python 在给定特定输入的情况下,如何实现贝叶斯网络的枚举算法?

Python 在给定特定输入的情况下,如何实现贝叶斯网络的枚举算法?,python,algorithm,bayesian-networks,Python,Algorithm,Bayesian Networks,我刚刚开始学习Bayes网络,并且一直在尝试用python实现它 其思想是,从包含网络节点和每个节点的概率分布表的文件中给定特定的输入,以执行作为字符串给定的查询,应用枚举算法并输出该查询的结果。格式如下: [Nodes] Burglary, Earthquake, Alarm, JohnCalls, MaryCalls [Probabilities] +Burglary = 0.001 +Earthquake = 0.002 +Alarm|+Earthquake,+Burglary = 0.

我刚刚开始学习Bayes网络,并且一直在尝试用python实现它

其思想是,从包含网络节点和每个节点的概率分布表的文件中给定特定的输入,以执行作为字符串给定的查询,应用枚举算法并输出该查询的结果。格式如下:

[Nodes]
Burglary, Earthquake, Alarm, JohnCalls, MaryCalls

[Probabilities]
+Burglary = 0.001
+Earthquake = 0.002
+Alarm|+Earthquake,+Burglary = 0.95
+Alarm|-Earthquake, +Burglary = 0.94
+Alarm|+Earthquake, -Burglary = 0.29
+Alarm|-Earthquake, -Burglary = 0.001
+JohnCalls|+Alarm = 0.9
+JohnCalls|-Alarm = 0.05
+MaryCalls|+Alarm = 0.7
+MaryCalls|-Alarm = 0.01

[Queries]
+Burglary|+Earthquake, +JohnCalls
+Earthquake
-MaryCalls|+Earthquake, +Alarm
+JohnCalls|-Earthquake, -MaryCalls, +Burglary
-Alarm|-Earthquake, -MaryCalls, +Burglary
-Alarm, +JohnCalls|-Earthquake, -MaryCalls, +Burglary
到目前为止,我已经能够解析此文本文件并构建:

  • 称为网络的所有节点的列表
  • 将所有查询作为字符串的列表
  • 节点类,包括标题、父节点列表和概率分布表的字典
问题是,我不知道如何从这里继续下去。我知道算法是如何运行的,但我在将其映射到代码时遇到了很多困难

该算法的工作方式如下:

这个算法在python中可能实现什么?在哪里可以找到此算法的一个好python示例?我在谷歌上搜索过,但我发现对于这个话题的新手来说,大多数例子和算法建议都过于复杂

以下是我目前掌握的代码:

import copy
import fileinput

network = [] #Network is a list of all the nodes
queries = [] #List of all the required queries
allProbs = {} #List with all the propabilities, given or calculated

class Node:
    def __init__(self, title, parents, pdt):
        self.title = title #String that identifies the node
        self.parents = parents #List of nodes that are parents of this node
        self.pdt = pdt #Dictionary where { '+title': value, '-title': value }

def readInput():
    input = fileinput.input()
    operation = 0
    for line in input:
        line = line.rstrip('\n').rstrip('\r')
        #Parsing the line that contains the nodes
        if operation is 1 and "[Probabilities]" not in line and "[Queries]" not in line and line is not "" and line is not "\n":
            nodes = line.split(',')
            for element in nodes:
                node = Node(element.replace(" ", ""), [], {})
                network.append(node);
        #Parsing the lines that contain the propabilities
        if operation is 2 and "[Probabilties]" not in line and "[Queries]" not in line and line is not "" and line is not "\n":
            lineAux = line.replace(" ", "").split("=")
            nodes = lineAux[0].split("|")
            queryNode = nodes[0].strip("+")
            #Finding and assigning parent nodes to each node, based on the evidence nodes of the probabilities
            if len(nodes) > 1:
                evidenceNodes = nodes[1].split(",")
                for element in evidenceNodes:
                    rawElement = element.strip("+").strip("-")
                    for node in network:
                        if node.title == queryNode:
                            alreadyInList = False
                            if len(node.parents) > 0:
                                for parent in node.parents:
                                    if parent.title == rawElement:
                                        alreadyInList = True
                                if not alreadyInList:
                                    for parentNode in network:
                                        if parentNode.title == rawElement:
                                            node.parents.append(parentNode)
                            else:
                                for parentNode in network:
                                    if parentNode.title == rawElement:
                                        node.parents.append(parentNode)
            #Assigning the Probability to the correct node
            for node in network:
                if node.title == queryNode:
                    node.pdt[lineAux[0]] = float(lineAux[1])
                    node.pdt["-" + lineAux[0].strip("+")] = round(1.0 - float(lineAux[1]), 7)
        #Handling what to do based on the value of the line
        if "[Nodes]" in line:
            operation = 1
        if "[Probabilities]" in line:
            operation = 2
        if "[Queries]" in line:
            operation = 3

readInput()
for node in network:
    print("------------------------")
    print(node.title)
    print(node.pdt)
我承认这可能不是最有效的代码,但现在,我只想找到一个可行的解决方案,然后在优化方面遇到麻烦

提前谢谢