我正在使用ID3算法使用决策树的python实现,并且我想知道如何在预定义数量的节点中训练树时如何测量准确性(明确说明:每插入10个节点进入树使我能够测量精度,因此我可以构建一个图表来显示训练时精度的演变),但是我使用的是递归方法,并且我不能使用节点计数器来进行测量,因为子树节点作为方法中的参数,这将使我对子树的准确性。我有点主意。 感谢您的关注!
这是该方法的代码(我尝试翻译代码,因此可能会有错误)
def decisionTree(Data, Target, Atribute,default):
Root = Node()
index = calc.getindexAttribute(Data,Target)
bigger = ">50K"
smaller = "<=50K"
ibigger = 0
ismaller = 0
a = ""
for i in range (1,len(Data)-1):
if ibigger > 0 and ismaller > 0:
break
elif Data[i][index] == bigger:
ibigger = ibigger + 1
elif Data[i][index] == smaller:
ismaller = ismaller + 1
if ismaller == len(Data) -1 :
return smaller
elif ibigger == len(Data) -1:
return bigger
if len(Atribute) == 1:
return mostCommonValue(Data,Target,Atribute)
else:
a = BestAttribute(Data,Atribute)
Root.attribute = a
Root.mostCommonClass = mostCommonValue(Data,Target, Atribute)
childs = {}
Values_A = calc.getAtributeValues(Data,a)
for y in range (0,len(Valores_A)):
SubData=[]
SubData = calc.makeSubData(Data,a,Valores_A[y])
if len(SubData) == 1:
return Root
else:
childs[Valores_A[y]] = decisionTree(SubData,Target,Delete_matrix(Atribute,a),Root)
Root.childs = childs
return Root