cpts.sort(lambda a,b: cmp(long(a), long(b))) TypeError: must use keyword argument for key function
时间: 2024-05-20 09:10:13 浏览: 15
这个错误是因为在 Python 3 中,sort() 方法不再支持 cmp 参数。相反,您需要通过 key 参数传递函数来定义您希望排序的标准。例如,如果您希望按数字大小对字符串列表进行排序,您可以使用以下代码:
cpts.sort(key=lambda s: int(s))
其中 lambda 函数将字符串转换为整数,并且 sort() 方法将使用这个整数值进行排序。希望这可以帮助您解决问题。
相关问题
cpts.sort(key=lambda (a,b): cmp(long(a), long(b))) ^ SyntaxError: invalid syntax
这是一个Python代码段,但是它可能有一些问题。这段代码使用lambda函数对一个包含元组的列表进行排序,但是lambda函数的语法可能有点错误。具体来说,lambda的参数列表应该像这样写:lambda a, b: cmp(long(a), long(b))。这个错误可能是由python版本不同导致的。
《机器学习》 周志华学习笔记第十四章 概率图模型(课后习题)python实现
由于题目较多,我这里只列出部分题目的代码实现,供您参考。
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]))
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)