我有一个代表决策树的字典:
{'Outlook': {'Overcast': 'Yes', 'Rain': {'Wind': {'Strong': 'No', 'Weak': 'Yes'}}, 'Sunny': {'Temperature': {'Cool': 'Yes', 'Hot': 'No', 'Mild': 'No'}}}}
可视化,如下所示:
[这棵树是用一些训练数据和ID3算法制成的;我希望从测试数据中预测示例的决定:
Outlook Temperature Humidity Wind Decision
Sunny Mild Normal Strong Yes
Overcast Mild High Strong Yes
Overcast Hot Normal Weak Yes
Rain Mild High Strong No
使用第一个示例,检查商品的大致思路:
Current dict 'outlook'
Examine 'outlook', found 'sunny':
'sunny' is a dict, make current dict the 'sunny' subdict
Examine 'temperature', found 'mild':
'mild' is not a dict, return value 'no'
但是,我不确定如何遍历字典。我有一些代码开头:
def fun(d, t):
"""
d -- decision tree dictionary
t -- testing examples in form of pandas dataframe
"""
for _, e in t.iterrows():
predict(d, e)
def predict(d, e):
"""
d -- decision tree dictionary
e -- a testing example in form of pandas series
"""
# ?
在predict()
中,e
可以作为字典访问:
print(e.to_dict())
# {'Outlook': 'Rain', 'Temperature': 'Cool', 'Humidity': 'Normal', 'Wind': 'Weak', 'Decision': 'Yes'}
print(e['Outlook'])
# 'Rain'
print(e['Decision'])
# 'Yes'
# etc
我只是不确定如何遍历该字典。我需要遍历测试示例,使决策属性出现在决策树中。
import pandas as pd
dt = {'Outlook': {'Overcast': 'Yes', 'Rain': {'Wind': {'Strong': 'No', 'Weak': 'Yes'}}, 'Sunny': {'Temperature': {'Cool': 'Yes', 'Hot': 'No', 'Mild': 'No'}}}}
df = pd.DataFrame(data=[['Sunny', 'Mild', 'Normal', 'Strong', 'Yes']],columns=['Outlook', 'Temperature', 'Humidity', 'Wind', 'Decision'])
def fun(d, t):
"""
d -- decision tree dictionary
t -- testing examples in form of pandas dataframe
"""
for _, e in t.iterrows():
predict(d, e)
def predict(d, e):
"""
d -- decision tree dictionary
e -- a testing example in form of pandas series
"""
current_node = list(d.keys())[0]
current_branch = d[current_node][e[current_node].item()]
# if leaf node value is string then its a decision
if isinstance(current_branch, str):
return current_branch
# else use that node as new searching subtree
else:
return predict(current_branch, e)
predict(dt, df)
输出:
否