Гранатовая байесовская ошибка ключа сети для вероятности - PullRequest
0 голосов
/ 22 апреля 2020

** Я получаю ошибку ключа (0,0), может кто-нибудь, пожалуйста, отладьте это и скажите мне, где я ошибся, я потратил слишком много времени на это. Все работает, если бы я был model.predict_proba (доказательства), но я хочу найти точную вероятность того, когда модель предоставила доказательства. **

из импорта граната *

def joint_probability (people, one_gene, two_genes, have_trait): "" "Вычислить и вернуть совместную вероятность.

The probability returned should be the probability that
    * everyone in set `one_gene` has one copy of the gene, and
    * everyone in set `two_genes` has two copies of the gene, and
    * everyone not in `one_gene` or `two_gene` does not have the gene, and
    * everyone in set `have_trait` has the trait, and
    * everyone not in set` have_trait` does not have the trait.
"""

print(one_gene)
print(two_genes)
print(have_trait)
# construct bayesian network from dictionary
# construct bayesian network from dictionary
# Now for parents make discrete distribution nodes
left_parent = Node(DiscreteDistribution({
    "2": 0.01,
    "1": 0.03,
    "0": 0.96,
}), "left_parent")

right_parent = Node(DiscreteDistribution({
    "2": 0.01,
    "1": 0.03,
    "0": 0.96,
}), "right_parent")

# Make Has traits for left_parent and right_parent
left_trait = Node(ConditionalProbabilityTable([

    # Probability of trait given copies of gene

    ["0", "True", 0.01],
    ["0", "False", 0.99],

    ["1", "True", 0.56],
    ["1", "False", 0.44],

    ["2", "True", 0.65],
    ["2", "False", 0.35],

], [left_parent.distribution]), "left_trait")

right_trait = Node(ConditionalProbabilityTable([

    ["0", "True", 0.01],
    ["0", "False", 0.99],

    ["1", "True", 0.56],
    ["1", "False", 0.44],

    ["2", "True", 0.65],
    ["2", "False", 0.35]

], [right_parent.distribution]), "right_trait")

# make childGene node which is conditional on MotherGene and FatherGene
child_gene = Node(ConditionalProbabilityTable([

    # probability both parents give neither gene
    ['0', '0', '0', 0.99],
    ["0", "0", "1", 0.005],
    ["0", "0", "2", 0.005],

    ["0", "1", "0", 0.495],
    ["0", "1", "1", 0.495],
    ["0", "1", "2", 0.01],

    ["0", "2", "0", 0.495],
    ["0", "2", "1", 0.01],
    ["0", "2", "2", 0.495],

    ["1", "0", "0", 0.495],
    ["1", "0", "1", 0.495],
    ["1", "0", "2", 0.01],

    ["1", "1", "0", 0.005],
    ['1', '1', '1', 0.495],
    ["1", "1", "2", 0.005],

    ["1", "2", "0", 0.01],
    ["1", "2", "1", 0.495],
    ["1", "2", "2", 0.495],

    ["2", "0", "0", 0.495],
    ["2", "0", "1", 0.01],
    ["2", "0", "2", 0.495],

    ["2", "1", "0", 0.01],
    ["2", "1", "1", 0.495],
    ["2", "1", "2", 0.495],

    ['2', '2', "0", 0.005],
    ["2", "2", "1", 0.005],
    ["2", "2", "2", 0.99],

], [left_parent.distribution, right_parent.distribution]),"child_gene")

# make child has trait
child_trait = Node(ConditionalProbabilityTable([
    ['0','0',0],
    ['1','0',0],
    ['2','0',0],
    ["0", "True", 0.01],
    ["0", "False", 0.99],

    ["1", "True", 0.56],
    ["1", "False", 0.44],

    ["2", "True", 0.65],
    ["2", "False", 0.35],

], [child_gene.distribution]),"child_trait")

model = BayesianNetwork()
model.add_states(left_parent , right_parent, left_trait, right_trait, child_gene, child_trait)
model.add_edge(left_parent, left_trait)
model.add_edge(right_parent, right_trait)
model.add_edge(left_parent, child_gene)
model.add_edge(right_parent, child_gene)
model.add_edge(child_gene, child_trait)
model.bake()

final = 1
# separate children in people
children_list = []
for n in people:
    # print(people[n]["mother"])
    if people[n]["mother"] != None and people[n]["father"] != None:
        children_list.append(people[n])

# loop through children list
for child in children_list:
    # check if it has_trait and set trait,
    if child["name"] in have_trait:
        child_tra = "True"
    else:
        child_tra = "False"

    # check if in one_gene or two_gene , else gene is 0
    if child["name"] in one_gene:
        child_g = "1"
    elif child["name"] in two_genes:
        child_g = "2"
    else:
        child_g = "0"

# for every parent of that child check , check if in has_trait and set trait, check if in one_gene or two_gene , else gene is 0
    if child["mother"] in have_trait:
        left_tra = "True"
    else:
        left_tra = "False"

        # check if in one_gene or two_gene , else gene is 0
    if child["mother"] in one_gene and child["mother"] is not None:
        left_par = "1"
    elif child["mother"] in two_genes and child["mother"] is not None:
        left_par = "2"
    else:
        left_par = "0"

    if child["father"] in have_trait:
        right_tra = "True"
    else:
        right_tra = "False"

    # check if in one_gene or two_gene , else gene is 0

    if child["father"] in one_gene:
        right_par = "1"
    elif child["father"] in two_genes:
        right_par = "2"
    else:
        right_par = "0"

#find probability by child

    evidence = {
             "left_parent": left_par,
             "right_parent": right_par,
             "left_trait": left_tra,
             "right_trait": right_tra,
             "child_gene": child_g,
             "child_trait": child_tra
         }
    print(evidence)

    print(model)

    pr = model.probability(evidence)

    final = final * pr

return final

, если name == ' main ':

model = Model()
people = {'Harry': {'name': 'Harry', 'mother': 'Lily', 'father': 'James', 'trait': None},
 'James': {'name': 'James', 'mother': None, 'father': None, 'trait': True},
 'Lily': {'name': 'Lily', 'mother': None, 'father': None, 'trait': False}}
one_gene=set()
two_genes=set()
have_trait={'James'}
print(joint_probability(people, one_gene, two_genes, have_trait))
...