CS179 Homework 4 Helpful Template & Code
In [ ]:
import pyGM as gm
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Loading the Data¶
In [ ]:
data = np.genfromtxt (‘RiskFactorData.csv’, delimiter=”,”,names=True)
print(data)
In [ ]:
data_int = np.array([list(xj) for xj in data], dtype=int)-1
print(data_int)
In [ ]:
nTrain = int(.75*len(data_int))
# normally you might permute the data, e.g.,
# data_perm = data_int[np.random.permutation(len(data_int)),:]
# but if you do you may get different answers than the HW expects
train = data_int[:nTrain]
valid = data_int[nTrain:]
In [ ]:
# define variables and domain sizes in order in file
Income = gm.Var(0,8)
Smoke = gm.Var(1,2)
Chol = gm.Var(2,2)
BMI = gm.Var(3,4)
Exercise = gm.Var(4,2)
Attack = gm.Var(5,2)
BP = gm.Var(6,4)
Angina = gm.Var(7,2)
Stroke = gm.Var(8,2)
Diab = gm.Var(9,4)
# and all variables together:
X = [Income,Smoke,Chol,BMI,Exercise,Attack,BP,Angina,Stroke,Diab]
names = {Income:’Income’, Smoke:’Smoke’, Chol:’Chol’, BMI:’BMI’, Exercise:’Exercise’,
Attack:’Attack’,BP:’BP’, Angina:’Angina’, Stroke:’Stroke’, Diab:’Diab’}
n = len(X) # number of variables
m = len(train) # number of training data points
Maximum likelihood for Bayesian Networks¶
In [ ]:
# …
Learning Graph Structure¶
In [ ]:
# …