首页 » Python代做 » Python代写 | CMPSC 448: Machine Learning. Homework 7.

Python代写 | CMPSC 448: Machine Learning. Homework 7.

这个作业是用Python完成贝叶斯网络相关的程序
CMPSC 448: Machine Learning. Homework 7.
Due: April 27
1. Instructions
• You cannot look at anyone else’s code.
• Fill in and upload hw7.py to gradescope.
• All code (except import statements) in hw7.py should be inside functions (importing homework1.py
should not cause code to execute).
• Code must have comments and any constants should be stored in a variable defined near the top of
your file.
• Read the instructions below carefully
• For most of the questions, we will not provide testing code. It is your responsibility to test your own
functions and make sure they run without throwing exceptions.
• Another reason for you to develop testing code is a very similar assignment will be given as the
take-home final.
2. Bayesian Networks
2.1. Numeric Stability. In this assignment, to avoid rounding issues, you will need to use Fraction
datatypes in python. Here is an example of their usage:
1 from fractions import Fraction as frac
2 half = frac (1 ,2)
If you choose not to use fraction datatypes, you might get a correct answer marked as incorrect due to
rounding issues.
1
2
2.2. Bayesian Network Parameters. The parameters of a Bayesian network will be passed to your code
through a parameter bn. It will be a class and its usage is described as follows: suppose the Bayesian network
has directed edges (a, b),(a, c),(b, d),(c, d). The parameters of this network are the conditional probabilities
P(a), P(b|a), P(c|a), P(d|b, c). The corresponding bn variable will be defined something like this:
1 import numpy as np
2 from fractions import Fraction as frac
3
4 class BayesNet1 :
5 def __init__ ( self , seed , k =10) :
6 prng = np . random . RandomState ( seed )
7 prob_a = frac ( prng . randint (1 , 2** k) , 2** k) # P(a =1)
8 prob_b = {(1 ,) : frac ( prng . randint (1 , 2** k) , 2** k) , # P(b =1 | a =1)
9 (0 ,) : frac ( prng . randint (1 , 2** k) , 2** k) # P(b =1 | a =0)
10 }
11 prob_c = {(1 ,) : frac ( prng . randint (1 , 2** k) , 2** k) , # P(c =1 | a =1)
12 (0 ,) : frac ( prng . randint (1 , 2** k) , 2** k) # P(c =1 | a =0)
13 }
14 prob_d = {
15 (0 , 0) : : frac ( prng . randint (1 , 2** k) , 2** k) , # P(d =1 | b =0 , c =0)
16 (0 , 1) : : frac ( prng . randint (1 , 2** k) , 2** k) , # P(d =1 | b =0 , c =1)
17 (1 , 0) : : frac ( prng . randint (1 , 2** k) , 2** k) , # P(d =1 | b =1 , c =0)
18 (1 , 1) : : frac ( prng . randint (1 , 2** k) , 2** k) , # P(d =1 | b =1 , c =1)
19 }
20
21 def a( value ): # returns P (a= value )
22 if value == 1:
23 return prob_a
24 else :
25 return 1- prob_a
26
27 def b( value , a): # returns P( b= value | a )
28 tmp = prob_b [(a ,) ]
29 if value == 1:
30 return tmp
31 else :
32 return 1- tmp
33
34 def c( value , a): # returns P( c= value | a )
35 tmp = prob_c [(a ,) ]
36 if value == 1:
37 return tmp
38 else :
39 return 1- tmp
40
41 def d( value , b ,c ): # returns P(d= value | b , c)
42 tmp = prob_d [(b ,c )]
43 if value == 1:
44 return tmp
45 else :
46 return 1- tmp
47
48
49 # example usage
50 bn = BayesNet1 ()
51 # get parameter p (a =1)
52 bn .a( value =1) # must call with arg names , bn .a (1) is incorrect
53 bn .a( value =0) # get parameter p(a =0)
54
3
55 bn .d( value =0 , b =1 , c =0) # get parameter P(d =0 | b =1 , c =0)
56
57 bd .a( value =1 , d =2) # throws error because P(a|d ) is not a parameter
If a parameter is not needed for a particular problem, using it may throw an exception. For example, for
the Bayesian network in the code above, P(a, b) can be computed without using the parameter P(d | b, c).
That is, after you write P(a, b) in terms of the network parameters and simplify, you will notice that P(d|b, c)
is not used at all. Thus our implementation of bn might not define the function bn.d(value, b, c). This
is used to test that you simplified the expression correctly.
2.3. Types of Questions. There will be two types of questions:
(1) Probability calculations: given network parameters, compute probabilities such as P(A = 1 | B =
0, C = 1).
(2) D-separation. The questions might ask you if A is conditionally independent of D given E. You will
write a function that returns result, pathverdict where result is the answer (are they conditionally
independent?) and pathverdict looks like:
[
((’a’, ’b’, ’d’), False)
((’a’, ’e’, ’d’), False)
]
each element in the list is a tuple. The first part of the tuple describes the path (e.g., a,e,d is an
undirected path from a to d) and the second part of the tuple tells us if that path is blocked or
not. Make sure pathverdict contains all of the appropriate undirected paths. A path cannot repeat
nodes (so a e a e d is not a path). You have to hard-code the appropriate paths, and results. In
other words, for d-separations questions, your functions should look like:
def question0():
parthverdict = [
((’a’, ’b’, ’d’), False)
((’a’, ’e’, ’d’), False)
]
result = False
return result, pathverdict
In this case, this answer indicates that the first path is not blocked and the second path is not
blocked.
4
Figure 1. Bayesian Network 1
Question 1 (5 pts). Fill in the function def question1Part1() that tells us if, in Figure 1, A ⊥⊥ D | G, B?
Your function should return result, pathverdict (see instructions on the previous page), where result is
the True/False answer to the question, and pathverdict is the set of all paths between A and D and their
status (blocked or not). This question will have a test that you can see.
Question 2 (5 pts). Fill in the function def question1Part2() that tells us if, in Figure 1, A ⊥⊥ G | D, F?
Your function should return result, pathverdict (see instructions on the previous page), where result is
the True/False answer to the question, and pathverdict is the set of all paths between A and G and their
status (blocked or not).
Question 3 (5 pts). Fill in the function def question1Part3() that tells us if, in Figure 1, A ⊥⊥ G | F?
Your function should return result, pathverdict (see instructions on the previous page), where result is
the True/False answer to the question, and pathverdict is the set of all paths between A and G and their
status (blocked or not).
Question 4 (5 pts). Fill in the function def question1Part4() that tells us if, in Figure 1, A ⊥⊥ G | B?
Your function should return result, pathverdict (see instructions on the previous page), where result is
the True/False answer to the question, and pathverdict is the set of all paths between A and G and their
status (blocked or not).
5
Figure 2. Bayesian Network 2
Question 5 (5 pts). Fill in the function def question2Part1() that tells us if, in Figure 2, A ⊥⊥ D | G?
Your function should return result, pathverdict (see instructions), where result is the True/False answer
to the question, and pathverdict is the set of all paths between A and D and their status (blocked or not).
Question 6 (5 pts). Fill in the function def question2Part2() that tells us if, in Figure 2, A ⊥⊥ D | F?
Your function should return result, pathverdict (see instructions), where result is the True/False answer
to the question, and pathverdict is the set of all paths between A and D and their status (blocked or not).
Question 7 (5 pts). Fill in the function def question2Part3() that tells us if, in Figure 2, B ⊥⊥ G | E, D?
Your function should return result, pathverdict (see instructions), where result is the True/False answer
to the question, and pathverdict is the set of all paths between B and G and their status (blocked or not).
6
Figure 3. Bayesian Network 3
Question 8 (4 pts). This question uses the Bayesian Network in Figure 3.
Fill in the function def question3Part1(a,b,c,d, bn) that returns P(a, b, c, d) in this network. The
parameter bn gives you the network parameters (see instructions). For this question, we will give 2 test
cases. In 1 test case, bn will provide all of the network parameters and in 1 test case, bn will only provide
the parameters that are absolutely needed to compute this answer.
Question 9 (4 pts). This question uses the Bayesian Network in Figure 3.
Fill in the function def question3Part2(a,b,c, bn) that returns P(a, b, c) in this network. The parameter
bn gives you the network parameters (see instructions).
Question 10 (4 pts). This question uses the Bayesian Network in Figure 3.
Fill in the function def question3Part3(d, bn) that returns P(d) in this network. The parameter bn gives
you the network parameters (see instructions).
Question 11 (4 pts). This question uses the Bayesian Network in Figure 3.
Fill in the function def question3Part4(a,b,c, bn) that returns P(a, b|c) in this network. The parameter
bn gives you the network parameters (see instructions). For this question, we will give 2 test cases. In 1 test
case, bn will provide all of the network parameters and in 1 test case, bn will only provide the parameters
that are absolutely needed to compute this answer.
Question 12 (4 pts). This question uses the Bayesian Network in Figure 3.
Fill in the function def question3Part4(c, d, bn) that returns P(c|d) in this network. The parameter
bn gives you the network parameters (see instructions).
7
A
B C
D
E
Figure 4. Bayesian Network 4
Question 13 (4 pts). This question uses the Bayesian Network in Figure 4.
Fill in the function def question4Part1(b,c,d, bn) that returns P(b, c, d) in this network. The parameter
bn gives you the network parameters (see instructions).
Question 14 (4 pts). This question uses the Bayesian Network in Figure 4.
Fill in the function def question4Part2(c, d, bn) that returns P(d|c) in this network. The parameter
bn gives you the network parameters (see instructions). note that the variables given to your function are in
alphabetical order (c first then d) but the answer should return P(d|c)
Question 15 (4 pts). This question uses the Bayesian Network in Figure 4.
Fill in the function def question4Part3(d, e, bn) that returns P(d | e) in this network. The parameter
bn gives you the network parameters (see instructions).


程序辅导定制C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB


本网站支持 Alipay WeChatPay PayPal等支付方式

E-mail: vipdue@outlook.com  微信号:vipnxx


如果您使用手机请先保存二维码,微信识别。如果用电脑,直接掏出手机果断扫描。

blank

发表评论

您的电子邮箱地址不会被公开。