[英]Recursively Incrementing Over Nested Dictionary Python
I am designing a simple Machine Learning program.我正在设计一个简单的机器学习程序。 I would like to calculate the total number of positive examples for a nested decision tree table.我想计算嵌套决策树表的正例总数。 I'm running into an issue with incrementing the variable p
and n
in my entropy
function我遇到了在entropy
function 中递增变量p
和n
的问题
import json
decisionTreeTable = {
"example1": {
"attributes": {
"alt": True,
"Bar": False,
"Fri": True,
"Hun": True,
"Pat": "SOME",
"Price": "$$$",
"Rain": False,
"Res": True,
"Type": "FRENCH",
"Est": "1-10",
"WillWait": True
}
},
"example2": {
"attributes": {
"alt": True,
"Bar": False,
"Fri": True,
"Hun": True,
"Pat": "FULL",
"Price": "$",
"Rain": False,
"Res": False,
"Type": "THAI",
"Est": "30-60",
"WillWait": False
}
},
"example3": {
"attributes": {
"alt": False,
"Bar": True,
"Fri": False,
"Hun": False,
"Pat": "SOME",
"Price": "$",
"Rain": False,
"Res": False,
"Type": "BURGER",
"Est": "0-10",
"WillWait": True
}
}
}
def recursivelyCalculatePositiveAndNegativeExamples(p,n,decisionTreeTable):
for key, value in decisionTreeTable.items():
if type(value) is dict:
recursivelyCalculatePositiveAndNegativeExamples(p,n,value)
else:
if key == "WillWait" and value:
print(key,":",value)
p += 1
elif key == "WillWait":
print(key,":",value)
n += 1
def entropy(decisionTreeTable):
print("Calculating entropy for example set")
p = 0
n = 0
recursivelyCalculatePositiveAndNegativeExamples(p,n,decisionTreeTable)
print("Total number of positive examples p", ":", p)
print("Total number of negative examples n", ":", n)
entropy(decisionTreeTable)
When I run this program they always come back as 0. I believe this is a simple issue.当我运行这个程序时,它们总是返回 0。我相信这是一个简单的问题。 If anyone can help me I would greatly appreciate it.如果有人可以帮助我,我将不胜感激。
Instead of relying on modifying global values (or arguments), make the function return p
and n
:不要依赖于修改全局值(或参数),而是让 function 返回p
和n
:
def recursively_calculate_positive_and_negative_examples(dt):
p, n = 0, 0
for key, value in dt.items():
if isinstance(value, dict):
pi, ni = recursively_calculate_positive_and_negative_examples(value)
p += pi
n += ni
elif key == "WillWait":
p += int(value)
n += int(not value)
return p, n
result = recursively_calculate_positive_and_negative_examples(decisionTreeTable)
print(result)
Output Output
(2, 1)
You need to return the values to keep track of them in Python.您需要返回值以在 Python 中跟踪它们。
Having made the minor fix, now your code works:进行了较小的修复后,现在您的代码可以工作了:
import json
decisionTreeTable = {
"example1": {
"attributes": {
"alt": True,
"Bar": False,
"Fri": True,
"Hun": True,
"Pat": "SOME",
"Price": "$$$",
"Rain": False,
"Res": True,
"Type": "FRENCH",
"Est": "1-10",
"WillWait": True
}
},
"example2": {
"attributes": {
"alt": True,
"Bar": False,
"Fri": True,
"Hun": True,
"Pat": "FULL",
"Price": "$",
"Rain": False,
"Res": False,
"Type": "THAI",
"Est": "30-60",
"WillWait": False
}
},
"example3": {
"attributes": {
"alt": False,
"Bar": True,
"Fri": False,
"Hun": False,
"Pat": "SOME",
"Price": "$",
"Rain": False,
"Res": False,
"Type": "BURGER",
"Est": "0-10",
"WillWait": True
}
}
}
def recursivelyCalculatePositiveAndNegativeExamples(p,n,decisionTreeTable):
print(p, n)
for key, value in decisionTreeTable.items():
if type(value) is dict:
_p, _n = recursivelyCalculatePositiveAndNegativeExamples(p,n,value)
p += _p
n += _n
else:
if key == "WillWait" and value:
print(key,":",value)
p += 1
elif key == "WillWait":
print(key,":",value)
n += 1
return p, n
def entropy(decisionTreeTable):
print("Calculating entropy for example set")
p = 0
n = 0
p, n = recursivelyCalculatePositiveAndNegativeExamples(p,n,decisionTreeTable)
print("Total number of positive examples p", ":", p)
print("Total number of negative examples n", ":", n)
entropy(decisionTreeTable)
Output: Output:
Calculating entropy for example set
0 0
0 0
0 0
WillWait : True
1 0
1 0
WillWait : False
3 1
3 1
WillWait : True
Total number of positive examples p : 10
Total number of negative examples n : 3
The problem is that there are no return values in your function.问题是你的 function 中没有返回值。
Try these changes:试试这些改变:
def recursivelyCalculatePositiveAndNegativeExamples(p,n,decisionTreeTable):
for key, value in decisionTreeTable.items():
if type(value) is dict:
p,n=recursivelyCalculatePositiveAndNegativeExamples(p,n,value)
else:
if key == "WillWait" and value:
print(key,":",value)
p += 1
elif key == "WillWait":
print(key,":",value)
n += 1
return p,n
def entropy(decisionTreeTable):
print("Calculating entropy for example set")
p = 0
n = 0
p,n=recursivelyCalculatePositiveAndNegativeExamples(p,n,decisionTreeTable)
print("Total number of positive examples p", ":", p)
print("Total number of negative examples n", ":", n)
Now entropy(decisionTreeTable)
gives:现在entropy(decisionTreeTable)
给出:
Calculating entropy for example set
WillWait : True
WillWait : False
WillWait : True
Total number of positive examples p : 2
Total number of negative examples n : 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.