我有一个树状结构的字典。 我希望将其转换为我的构造函数定义的属性 - 子关系。
这就是我班上的样子 -
class DecisionNode:
# A DecisionNode contains an attribute and a dictionary of children.
# The attribute is either the attribute being split on, or the predicted label if the node has no children.
def __init__(self, attribute):
self.attribute = attribute
self.children = {}
您可以在此处找到我的代码 - https://raw.githubusercontent.com/karthikkolathumani/Machine-Learning-Models/master/id3.py
这是一种虚拟方法,模仿我实际需要对字典对象进行的操作 -
def funTree():
myLeftTree = DecisionNode('humidity')
myLeftTree.children['normal'] = DecisionNode('no')
myLeftTree.children['high'] = DecisionNode('yes')
myTree = DecisionNode('wind')
myTree.children['weak'] = myLeftTree
myTree.children['strong'] = DecisionNode('no')
return myTree
示例输出类似于(示例) -
wind = weak
humidity = normal: no
humidity = high: yes
wind = strong: no
Dict对象样本 -
{'outlook': {'sunny': {'temperature': {'hot': 'no', 'mild': {'humidity': {'high': 'no', 'normal': 'yes'}}, 'cool': 'yes'}}, 'overcast': 'yes', 'rainy': {'temperature': {'mild': {'humidity': {'high': {'wind': {'weak': 'yes', 'strong': 'no'}}, 'normal': 'yes'}}, 'cool': {'humidity': {'normal': {'wind': {'weak': 'yes', 'strong': 'no'}}}}}}}}
我目前的结果如下所示,我使用 selfattr
outlook =
sunny =
temperature =
hot = no
mild =
humidity =
high = no
normal = yes
cool = yes
overcast = yes
rainy =
temperature =
mild =
humidity =
high =
wind =
weak = yes
strong = no
normal = yes
cool =
humidity =
normal =
wind =
weak = yes
strong = no