如何为我的dict属性泛化这个函数

2024-05-19 22:11:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有dict属性来建立一个树状结构。 我有一个预测函数,它根据属性子关系预测决策树的准确性

我想推广这个函数,这样我就可以根据dict属性预测我的精确度

def __init__(self, _d):
         self.__dict__ = {a:DecisionNode(b) if isinstance(b, dict) else b for a, b in _d.items()}



def predicts(self, x):
            if self.children == {}: # reached leaf level
                return self.attribute
            value = x[self.attribute]
            subtree = self.children[value]
            return subtree.predicts(x)


for i in range(0,len(test)):
        if str(class_tree.predicts(test.loc[i])) == str(test.loc[1,target]):
            correct += 1
    print("\nThe accuracy is: ", correct/len(test))

这就是我的dict属性-

class_tree.outlook.sunny.temperature.hot
Out[85]: 'no'

Tags: 函数intestselfforreturnif属性