《机器学习》 周志华学习笔记第十四章 概率图模型(课后习题)python实现
时间: 2023-06-20 15:09:21 浏览: 99
最小描述长度准则-机器学习算法汇总大全
由于题目较多,我这里只列出部分题目的代码实现,供您参考。
14.2 贝叶斯网络
14.2.1 习题1
```python
import numpy as np
# 构建贝叶斯网络的结构,定义每个节点的父节点
parents = {
'D': [],
'I': [],
'G': ['I', 'D'],
'S': ['I'],
'L': ['G']
}
# 定义概率表
prob_tables = {
'D': np.array([0.6, 0.4]),
'I': np.array([0.7, 0.3]),
'G': np.array([
[0.3, 0.4, 0.3, 0.05, 0.95, 0.9],
[0.7, 0.6, 0.7, 0.95, 0.05, 0.1]
]),
'S': np.array([
[0.95, 0.2],
[0.05, 0.8]
]),
'L': np.array([
[0.1, 0.4],
[0.9, 0.6]
])
}
def get_prob(node, values, evidences):
"""
计算节点的概率
:param node: 节点名称
:param values: 取值
:param evidences: 证据
"""
if len(parents[node]) == 0: # 如果节点没有父节点
return prob_tables[node][values]
else:
parent_values = [evidences[parent] for parent in parents[node]]
return prob_tables[node][tuple(parent_values)][values]
def inference(query, evidences):
"""
推断函数
:param query: 查询节点
:param evidences: 证据
"""
values = [0, 1] # 取值为0或1
q = np.zeros((2,))
for value in values:
evidences[query] = value # 将查询节点的取值设置为当前值
p = 1
for node in prob_tables.keys():
p *= get_prob(node, evidences[node], evidences)
q[value] = p
q /= np.sum(q) # 归一化
return q
query = 'G'
evidences = {'D': 0, 'I': 1, 'S': 1, 'L': 1}
q = inference(query, evidences)
print('P(G=0|D=0,I=1,S=1,L=1)={:.4f}'.format(q[0]))
print('P(G=1|D=0,I=1,S=1,L=1)={:.4f}'.format(q[1]))
```
14.3 马尔可夫网络
14.3.1 习题1
```python
import numpy as np
# 构建马尔可夫网络的结构
structure = {
'A': ['C', 'D'],
'B': ['D'],
'C': ['E'],
'D': ['E'],
'E': []
}
# 定义概率表
prob_tables = {
'A': np.array([
[0.4, 0.6],
[0.2, 0.8]
]),
'B': np.array([0.2, 0.8]),
'C': np.array([
[0.5, 0.5],
[0.3, 0.7]
]),
'D': np.array([
[0.6, 0.4],
[0.1, 0.9]
]),
'E': np.array([
[0.3, 0.7],
[0.8, 0.2]
])
}
def inference(query, evidences):
"""
推断函数
:param query: 查询节点
:param evidences: 证据
"""
values = [0, 1] # 取值为0或1
q = np.zeros((2,))
for value in values:
# 构建条件概率表
cpts = {}
for node in structure.keys():
parents = structure[node]
if len(parents) == 0: # 如果节点没有父节点
cpts[node] = prob_tables[node][value]
else:
parent_values = tuple([evidences[parent] for parent in parents])
cpts[node] = prob_tables[node][parent_values][value]
# 计算联合概率
p = 1
for node in structure.keys():
parents = structure[node]
if len(parents) == 0: # 如果节点没有父节点
p *= cpts[node]
else:
parent_values = tuple([evidences[parent] for parent in parents])
p *= cpts[node]
q[value] = p
q /= np.sum(q) # 归一化
return q
query = 'E'
evidences = {'A': 1, 'B': 0}
q = inference(query, evidences)
print('P(E=0|A=1,B=0)={:.4f}'.format(q[0]))
print('P(E=1|A=1,B=0)={:.4f}'.format(q[1]))
```
阅读全文