Beispiel #1
0
network = BayesianNetwork("Monty hall problem")
network.add_states(*states.values())
network.add_edge(states["Peer_Pressure"],states["Smoking"])
network.add_edge(states["Anxiety"],states["Smoking"])
network.add_edge(states["Smoking"],states["Yellow_Fingers"])
network.add_edge(states["Genetics"],states["Lung_cancer"])
network.add_edge(states["Smoking"],states["Lung_cancer"])
network.add_edge(states["Genetics"],states["Attention_Disorder"])
network.add_edge(states['Lung_cancer'], states["Coughing"])
network.add_edge(states['Allergy'], states["Coughing"])
network.add_edge(states['Coughing'], states["Fatigue"])
network.add_edge(states['Lung_cancer'], states["Fatigue"])
network.add_edge(states["Fatigue"], states["Car_Accident"])
network.add_edge(states["Attention_Disorder"], states["Car_Accident"])
import ast
network.bake()
beliefs = network.predict_proba({"Genetics":"T"},max_iterations=100000)
# print(beliefs)
# beliefs = map(str, beliefs)
# for state, belief in zip(network.states, beliefs):
#     if hasattr(belief,"parameters"):
#         print(state.name,belief.parameters)
# exit()
# network.add_edge(s1, s3)
# network.add_edge(s2, s3)
#
# beliefs = network.predict_proba({"guest": "A", "monty": "B"})
# beliefs = map(str, beliefs)
# for state, belief in zip(network.states, beliefs):
#     print(state.name, belief)
    sCarAccident,
)
model.add_edge(sAnxiety, sSmoking)
model.add_edge(sPeerPressure, sSmoking)
model.add_edge(sSmoking, sLungCancer)
model.add_edge(sGenetics, sLungCancer)
model.add_edge(sGenetics, sAttentionDisorder)
model.add_edge(sAllergy, sCoughing)
model.add_edge(sLungCancer, sCoughing)
model.add_edge(sCoughing, sFatigue)
model.add_edge(sLungCancer, sFatigue)
model.add_edge(sFatigue, sCarAccident)
model.add_edge(sAttentionDisorder, sCarAccident)
model.bake()

prediction = model.predict_proba({})
states = [
    "Anxiety",
    "Peer_pressure",
    "Smoking",
    "Genetics",
    "Lung_cancer",
    "Allergy",
    "Coughing",
    "Fatigue",
    "Attention_Disorder",
    "Car_Accident",
]

print(prediction)
# print([(s, p) for (s, p) in list(zip(states, prediction))])
Beispiel #3
0
ashwin = ConditionalProbabilityTable(
    returnConditionalProbability(df, 'Location', 'Ashwin'), [location])
batting = ConditionalProbabilityTable(
    returnConditionalProbability(df, 'Toss', 'Bat'), [toss])
result = ConditionalProbabilityTable(
    returnConditionalProbability(df, 'Bat', 'Result'), [batting])

sLocation = State(location, name="Location")
sToss = State(toss, name="Toss")
sBatting = State(batting, name="Batting")
sAshwin = State(ashwin, name="Ashwin")
sResult = State(result, name="Result")

# Create the Bayesian network object with a useful name
model = BayesianNetwork("Ashwin Playing Problem")

# Add the three states to the network
model.add_states(sLocation, sToss, sBatting, sAshwin, sResult)
model.add_edge(sLocation, sAshwin)
model.add_edge(sToss, sBatting)
model.add_edge(sBatting, sResult)
model.bake()

model.predict_proba([None, None, '2nd', 'Y', 'won'])[1]
model.predict_proba([None, None, '2nd', 'N', 'won'])[0]

model.predict_proba([None, None, '2nd', 'Y', 'lost'])[1]

model.predict_proba([None, None, '2nd', 'N', 'lost'])[0]