Artificial Intelligence – (Solution)

$ 24.99

Description

E09 Bayesian Network

17341015 Hongzheng Chen
Contents
1 Pomegranate Installation 2
2 Building Bayesian Network 2
3 Tasks 3
3.1 Burglary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Diagnosing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4 Codes and Results 6
1 Pomegranate Installation
Under Linux:
1. Install python first (python 2, not python 3).
2. Run sudo apt-get install python-pip to install pip.
3. Run sudo pip install pomegranate to install pomegranate.

Under Windows
You can also run pip install pomegranate if you have installed pip. If you donโ€™t know how to install pip, please click https://jingyan.baidu.com/article/e73e26c0d94e0524adb6a7ff.html.
For more, please click the homepage of Pomegranate – https://github.com/jmschrei/pomegranate for help.

2 Building Bayesian Network
Please refer to Tutorial 4 Bayesian Networks.pdf. I will explain it in class.

3 Tasks
3.1 Burglary
Please code to calculate:
1.
2.
3.
4.
5.
6.

3.2 Diagnosing
Variables and their domais
(1)PatientAge:[โ€™0-30โ€™,โ€™31-65โ€™,โ€™65+โ€™]
(2)CTScanResult:[โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™]
(3)MRIScanResult: [โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™]
(4)StrokeType: [โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™, โ€™Stroke Mimicโ€™]
(5)Anticoagulants: [โ€™Usedโ€™,โ€™Not usedโ€™]
(6)Mortality:[โ€™Trueโ€™, โ€™Falseโ€™]
(7)Disability: [โ€™Negligibleโ€™, โ€™Moderateโ€™, โ€™Severeโ€™]
CPTs
Note: [CTScanResult, MRIScanResult,StrokeType] means:
P(StrokeType=โ€™…โ€™ | CTScanResult=โ€™…โ€™ โˆง MRIScanResult=โ€™…โ€™)
(1)
[PatientAge]
[โ€™0-30โ€™, 0.10],
[โ€™31-65โ€™, 0.30],
[โ€™65+โ€™, 0.60]
(2)
[CTScanResult]
[โ€™Ischemic Strokeโ€™,0.7],
[ โ€™Hemmorraghic Strokeโ€™,0.3]
(3)
[MRIScanResult]
[โ€™Ischemic Strokeโ€™,0.7],
[ โ€™Hemmorraghic Strokeโ€™,0.3]
(4)
[Anticoagulants]
[โ€™Usedโ€™,0.5],
[โ€™Not usedโ€™,0.5]
(5)
[CTScanResult, MRIScanResult,StrokeType]
[โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,0.8],
[โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,0.5],
[ โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,0.5],
[ โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,0],
[โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,0],
[โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,0.4],
[ โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,0.4],
[ โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,0.9],
[โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Stroke Mimicโ€™,0.2],
[โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Stroke Mimicโ€™,0.1],
[ โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Stroke Mimicโ€™,0.1],
[ โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Stroke Mimicโ€™,0.1],
(6)
[StrokeType, Anticoagulants, Mortality]
[โ€™Ischemic Strokeโ€™, โ€™Usedโ€™, โ€™Falseโ€™,0.28],
[โ€™Hemmorraghic Strokeโ€™, โ€™Usedโ€™, โ€™Falseโ€™,0.99],
[โ€™Stroke Mimicโ€™, โ€™Usedโ€™, โ€™Falseโ€™,0.1],
[โ€™Ischemic Strokeโ€™,โ€™Not usedโ€™, โ€™Falseโ€™,0.56],
[โ€™Hemmorraghic Strokeโ€™, โ€™Not usedโ€™, โ€™Falseโ€™,0.58],
[โ€™Stroke Mimicโ€™, โ€™Not usedโ€™, โ€™Falseโ€™,0.05],
[โ€™Ischemic Strokeโ€™, โ€™Usedโ€™ ,โ€™Trueโ€™,0.72],
[โ€™Hemmorraghic Strokeโ€™, โ€™Usedโ€™, โ€™Trueโ€™,0.01],
[โ€™Stroke Mimicโ€™, โ€™Usedโ€™, โ€™Trueโ€™,0.9],
[โ€™Ischemic Strokeโ€™, โ€™Not usedโ€™ ,โ€™Trueโ€™,0.44],
[โ€™Hemmorraghic Strokeโ€™, โ€™Not usedโ€™, โ€™Trueโ€™,0.42 ],
[โ€™Stroke Mimicโ€™, โ€™Not usedโ€™, โ€™Trueโ€™,0.95]
(7)
[StrokeType, PatientAge, Disability]
[โ€™Ischemic Strokeโ€™, โ€™0-30โ€™,โ€™Negligibleโ€™, 0.80],
[โ€™Hemmorraghic Strokeโ€™, โ€™0-30โ€™,โ€™Negligibleโ€™, 0.70],
[โ€™Stroke Mimicโ€™, โ€™0-30โ€™, โ€™Negligibleโ€™,0.9],
[โ€™Ischemic Strokeโ€™, โ€™31-65โ€™,โ€™Negligibleโ€™, 0.60],
[โ€™Hemmorraghic Strokeโ€™, โ€™31-65โ€™,โ€™Negligibleโ€™, 0.50],
[โ€™Stroke Mimicโ€™, โ€™31-65โ€™, โ€™Negligibleโ€™,0.4],
[โ€™Ischemic Strokeโ€™, โ€™65+โ€™ , โ€™Negligibleโ€™,0.30],
[โ€™Hemmorraghic Strokeโ€™, โ€™65+โ€™ , โ€™Negligibleโ€™,0.20], [โ€™Stroke Mimicโ€™, โ€™65+โ€™ , โ€™Negligibleโ€™,0.1],
[โ€™Ischemic Strokeโ€™, โ€™0-30โ€™ ,โ€™Moderateโ€™,0.1],
[โ€™Hemmorraghic Strokeโ€™, โ€™0-30โ€™ ,โ€™Moderateโ€™,0.2],
[โ€™Stroke Mimicโ€™, โ€™0-30โ€™ ,โ€™Moderateโ€™,0.05],
[โ€™Ischemic Strokeโ€™, โ€™31-65โ€™,โ€™Moderateโ€™,0.3],
[โ€™Hemmorraghic Strokeโ€™, โ€™31-65โ€™,โ€™Moderateโ€™,0.4],
[โ€™Stroke Mimicโ€™, โ€™31-65โ€™,โ€™Moderateโ€™,0.3],
[โ€™Ischemic Strokeโ€™, โ€™65+โ€™ ,โ€™Moderateโ€™,0.4],
[โ€™Hemmorraghic Strokeโ€™, โ€™65+โ€™ ,โ€™Moderateโ€™,0.2], [โ€™Stroke Mimicโ€™, โ€™65+โ€™ ,โ€™Moderateโ€™,0.1],
[โ€™Ischemic Strokeโ€™, โ€™0-30โ€™ ,โ€™Severeโ€™,0.1],
[โ€™Hemmorraghic Strokeโ€™, โ€™0-30โ€™ ,โ€™Severeโ€™,0.1],
[โ€™Stroke Mimicโ€™, โ€™0-30โ€™ ,โ€™Severeโ€™,0.05],
[โ€™Ischemic Strokeโ€™, โ€™31-65โ€™,โ€™Severeโ€™,0.1],
[โ€™Hemmorraghic Strokeโ€™, โ€™31-65โ€™,โ€™Severeโ€™,0.1],
[โ€™Stroke Mimicโ€™, โ€™31-65โ€™,โ€™Severeโ€™,0.3],
[โ€™Ischemic Strokeโ€™, โ€™65+โ€™ ,โ€™Severeโ€™,0.3],
[โ€™Hemmorraghic Strokeโ€™, โ€™65+โ€™ ,โ€™Severeโ€™,0.6], [โ€™Stroke Mimicโ€™, โ€™65+โ€™ ,โ€™Severeโ€™,0.8]
Calculation
Please code to calculate the following probability value:
p1 = P(Mortality=โ€™Trueโ€™ | PatientAge=โ€™31-65โ€™ โˆง CTScanResult=โ€™Ischemic Strokeโ€™) p2 = P(Disability=โ€™Moderateโ€™ | PatientAge=โ€™65+โ€™ โˆง MRIScanResult=โ€™Hemmorraghic Strokeโ€™) p3 = P(StrokeType=โ€™Stroke Mimicโ€™ | PatientAge=โ€™65+โ€™ โˆง CTScanResult=โ€™Hemmorraghic Strokeโ€™ โˆง MRIScanResult=โ€™Ischemic Strokeโ€™)
p4 = P(Anticoagulants=โ€™Not usedโ€™ | PatientAge=โ€™0-30โ€™)

Please solve the 2 tasks and hand in a file named E09 YourNumber.pdf, and send it to ai 201901@foxmail.com
4 Codes and Results
The codes use Python 3!

burglary.py
from pomegranate import *
burglary = DiscreteDistribution( {โ€™Tโ€™:0.001, โ€™Fโ€™:0.999} ) earthquake = DiscreteDistribution( {โ€™Tโ€™:0.002, โ€™Fโ€™:0.998} ) alarm = ConditionalProbabilityTable(
[[โ€™Tโ€™,โ€™Tโ€™,โ€™Tโ€™,0.95],
[โ€™Tโ€™,โ€™Fโ€™,โ€™Tโ€™,0.94],
[โ€™Fโ€™,โ€™Tโ€™,โ€™Tโ€™,0.29],
[โ€™Fโ€™,โ€™Fโ€™,โ€™Tโ€™,0.001],
[โ€™Tโ€™,โ€™Tโ€™,โ€™Fโ€™,0.05],
[โ€™Tโ€™,โ€™Fโ€™,โ€™Fโ€™,0.06],
[โ€™Fโ€™,โ€™Tโ€™,โ€™Fโ€™,0.71],
[โ€™Fโ€™,โ€™Fโ€™,โ€™Fโ€™,0.999]], [burglary, earthquake]) johncalls = ConditionalProbabilityTable(
[[โ€™Tโ€™,โ€™Tโ€™,0.90],
[โ€™Fโ€™,โ€™Tโ€™,0.05],
[โ€™Tโ€™,โ€™Fโ€™,0.10],
[โ€™Fโ€™,โ€™Fโ€™,0.95]], [alarm]) marycalls = ConditionalProbabilityTable(
[[โ€™Tโ€™,โ€™Tโ€™,0.70],
[โ€™Fโ€™,โ€™Tโ€™,0.01],
[โ€™Tโ€™,โ€™Fโ€™,0.30],
[โ€™Fโ€™,โ€™Fโ€™,0.99]], [alarm])
s1 = State(burglary, name=”burglary”) s2 = State(earthquake, name=”earthquake”) s3 = State(alarm, name=”alarm”) s4 = State(johncalls, name=”johncalls”) s5 = State(marycalls, name=”marycalls”)
model = BayesianNetwork(“Burglary”) model.add_states(s1,s2,s3,s4,s5)
model.add_transition(s1,s3) model.add_transition(s2,s3) model.add_transition(s3,s4) model.add_transition(s3,s5) model.bake()
marginals = model.predict_proba({})
# P(A) print(“P(A) = {}”.format(marginals[2].parameters[0][“T”]))
# P(J&&~M) = P(J|~M)P(~M) j_nm = model.predict_proba({โ€™marycallsโ€™:โ€™Fโ€™})[3].parameters[0][“T”] * marginals[4].
,โ†’ parameters[0][“F”] print(“P(J && ~M) = {}”.format(j_nm))
# P(A|J&&~M) print(“P(A | J && ~M) = {}”.format(model.predict_proba({โ€™johncallsโ€™:โ€™Tโ€™,โ€™marycallsโ€™:โ€™Fโ€™}) ,โ†’ [2].parameters[0][“T”]))
# P(B|A) print(“P(B | A) = {}”.format(model.predict_proba({โ€™alarmโ€™:โ€™Tโ€™})[0].parameters[0][“T”]))
# P(B|J&&~M) b_c_j_nm = model.predict_proba({โ€™johncallsโ€™:โ€™Tโ€™,โ€™marycallsโ€™:โ€™Fโ€™})[0].parameters[0][“T”] print(“P(B | J && ~M) = {}”.format(b_c_j_nm)) # P(J&&~M|~B) = P(~B && J && ~M) / P(~B)
# = P(~B | J && ~M) P(J && ~M) / P(~B)
# = (1- P(B | J && ~M)) P(J && ~M) / P(~B)
print(“P(J && ~M | ~B) = {}”.format((1-b_c_j_nm) * j_nm / marginals[0].parameters[0][“F”]))

diagnosing.py
from pomegranate import *
PatientAge = DiscreteDistribution({โ€™0-30โ€™:0.10, โ€™31-65โ€™:0.30, โ€™65+โ€™:0.60})
CTScanResult = DiscreteDistribution({โ€™Ischemic Strokeโ€™:0.7, โ€™Hemmorraghic Strokeโ€™:0.3})
MRIScanResult = DiscreteDistribution({โ€™Ischemic Strokeโ€™:0.7, โ€™Hemmorraghic Strokeโ€™:0.3})
Anticoagulants = DiscreteDistribution({โ€™Usedโ€™:0.5 ,โ€™Not usedโ€™:0.5})
StrokeType = ConditionalProbabilityTable(
[[โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,0.8],
[โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,0.5],
[โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,0.5],
[โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,0],
[โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,0],
[โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,0.4],
[โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,0.4],
[โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,0.9],
[โ€™Ischemic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Stroke Mimicโ€™,0.2],
[โ€™Ischemic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Stroke Mimicโ€™,0.1],
[โ€™Hemmorraghic Strokeโ€™,โ€™Ischemic Strokeโ€™,โ€™Stroke Mimicโ€™,0.1],
[โ€™Hemmorraghic Strokeโ€™,โ€™Hemmorraghic Strokeโ€™,โ€™Stroke Mimicโ€™,0.1]],[CTScanResult, ,โ†’ MRIScanResult])
Mortality = ConditionalProbabilityTable(
[[โ€™Ischemic Strokeโ€™,โ€™Usedโ€™,โ€™Falseโ€™,0.28],
[โ€™Hemmorraghic Strokeโ€™,โ€™Usedโ€™,โ€™Falseโ€™,0.99], [โ€™Stroke Mimicโ€™,โ€™Usedโ€™,โ€™Falseโ€™,0.1],
[โ€™Ischemic Strokeโ€™,โ€™Not usedโ€™,โ€™Falseโ€™,0.56],
[โ€™Hemmorraghic Strokeโ€™,โ€™Not usedโ€™,โ€™Falseโ€™,0.58],
[โ€™Stroke Mimicโ€™,โ€™Not usedโ€™,โ€™Falseโ€™,0.05],
[โ€™Ischemic Strokeโ€™,โ€™Usedโ€™,โ€™Trueโ€™,0.72],
[โ€™Hemmorraghic Strokeโ€™,โ€™Usedโ€™,โ€™Trueโ€™,0.01], [โ€™Stroke Mimicโ€™,โ€™Usedโ€™,โ€™Trueโ€™,0.9],
[โ€™Ischemic Strokeโ€™,โ€™Not usedโ€™,โ€™Trueโ€™,0.44],
[โ€™Hemmorraghic Strokeโ€™,โ€™Not usedโ€™,โ€™Trueโ€™,0.42],
[โ€™Stroke Mimicโ€™,โ€™Not usedโ€™,โ€™Trueโ€™,0.95]],[StrokeType, Anticoagulants]) Disability = ConditionalProbabilityTable(
[[โ€™Ischemic Strokeโ€™,โ€™0-30โ€™,โ€™Negligibleโ€™,0.80],
[โ€™Hemmorraghic Strokeโ€™,โ€™0-30โ€™,โ€™Negligibleโ€™,0.70],
[โ€™Stroke Mimicโ€™,โ€™0-30โ€™,โ€™Negligibleโ€™,0.9],
[โ€™Ischemic Strokeโ€™,โ€™31-65โ€™,โ€™Negligibleโ€™,0.60],
[โ€™Hemmorraghic Strokeโ€™,โ€™31-65โ€™,โ€™Negligibleโ€™,0.50],
[โ€™Stroke Mimicโ€™,โ€™31-65โ€™,โ€™Negligibleโ€™,0.4],
[โ€™Ischemic Strokeโ€™,โ€™65+โ€™,โ€™Negligibleโ€™,0.30],
[โ€™Hemmorraghic Strokeโ€™,โ€™65+โ€™,โ€™Negligibleโ€™,0.20],
[โ€™Stroke Mimicโ€™,โ€™65+โ€™,โ€™Negligibleโ€™,0.1],
[โ€™Ischemic Strokeโ€™,โ€™0-30โ€™,โ€™Moderateโ€™,0.1],
[โ€™Hemmorraghic Strokeโ€™,โ€™0-30โ€™,โ€™Moderateโ€™,0.2],
[โ€™Stroke Mimicโ€™,โ€™0-30โ€™,โ€™Moderateโ€™,0.05],
[โ€™Ischemic Strokeโ€™,โ€™31-65โ€™,โ€™Moderateโ€™,0.3],
[โ€™Hemmorraghic Strokeโ€™,โ€™31-65โ€™,โ€™Moderateโ€™,0.4],
[โ€™Stroke Mimicโ€™,โ€™31-65โ€™,โ€™Moderateโ€™,0.3],
[โ€™Ischemic Strokeโ€™,โ€™65+โ€™,โ€™Moderateโ€™,0.4],
[โ€™Hemmorraghic Strokeโ€™,โ€™65+โ€™,โ€™Moderateโ€™,0.2],
[โ€™Stroke Mimicโ€™,โ€™65+โ€™,โ€™Moderateโ€™,0.1],
[โ€™Ischemic Strokeโ€™,โ€™0-30โ€™,โ€™Severeโ€™,0.1],
[โ€™Hemmorraghic Strokeโ€™,โ€™0-30โ€™,โ€™Severeโ€™,0.1],
[โ€™Stroke Mimicโ€™,โ€™0-30โ€™,โ€™Severeโ€™,0.05],
[โ€™Ischemic Strokeโ€™,โ€™31-65โ€™,โ€™Severeโ€™,0.1],
[โ€™Hemmorraghic Strokeโ€™,โ€™31-65โ€™,โ€™Severeโ€™,0.1],
[โ€™Stroke Mimicโ€™,โ€™31-65โ€™,โ€™Severeโ€™,0.3],
[โ€™Ischemic Strokeโ€™,โ€™65+โ€™,โ€™Severeโ€™,0.3],
[โ€™Hemmorraghic Strokeโ€™,โ€™65+โ€™,โ€™Severeโ€™,0.6],
[โ€™Stroke Mimicโ€™,โ€™65+โ€™,โ€™Severeโ€™,0.8]],[StrokeType,PatientAge])
s1 = State(PatientAge, name=”PatientAge”) s2 = State(CTScanResult, name=”CTScanResult”) s3 = State(MRIScanResult, name=”MRIScanResult”) s4 = State(StrokeType, name=”StrokeType”) s5 = State(Anticoagulants, name=”Anticoagulants”) s6 = State(Mortality, name=”Mortality”) s7 = State(Disability, name=”Disability”) model = BayesianNetwork(“Diagnosing”)

model.add_states(s1,s2,s3,s4,s5,s6,s7)
model.add_transition(s2,s4) model.add_transition(s3,s4)
model.add_transition(s4,s6) model.add_transition(s5,s6)
model.add_transition(s1,s7) model.add_transition(s4,s7) model.bake() marginals = model.predict_proba({})
p1 = model.predict_proba({โ€™PatientAgeโ€™:โ€™31-65โ€™,โ€™CTScanResultโ€™:โ€™Ischemic Strokeโ€™})[5].
,โ†’ parameters[0][“True”] p2 = model.predict_proba({โ€™PatientAgeโ€™:โ€™65+โ€™,โ€™MRIScanResultโ€™:โ€™Hemmorraghic Strokeโ€™})[6].
,โ†’ parameters[0][“Moderate”] p3 = model.predict_proba({โ€™PatientAgeโ€™:โ€™65+โ€™,โ€™CTScanResultโ€™:โ€™Hemmorraghic Strokeโ€™,โ€™
,โ†’ MRIScanResultโ€™:โ€™Ischemic Strokeโ€™})[3].parameters[0][“Stroke Mimic”] p4 = model.predict_proba({โ€™PatientAgeโ€™:โ€™0-30โ€™})[4].parameters[0][“Not used”] print(p1) print(p2) print(p3) print(p4)

Reviews

There are no reviews yet.

Be the first to review “Artificial Intelligence – (Solution)”

Your email address will not be published. Required fields are marked *