
我似乎搞不懂为什么会有错误。我正在学习决策树算法。
def gini(rows):
"""calculate the gini impurity for a list of rows."""
counts = class_counts(rows)
impurity = 1
for lbl in counts:
prob_of_lbl = counts[lbl] / float(len(rows))
impurity -= prob_of_lbl**2
return impurity
def info_gain(left, right, current_uncertainty):
"""Information Gain"""
#The uncertainty of the starting node, minus the weighted
#impurity of two child nodes.
p = float(len(left)) / (len(left) + len(right))
return current_uncertainty - p * gini(left) - (1 - p) * gini(right)发布于 2022-05-21 08:10:49
您在def gini ...之前有缩进吗?如果您的gini函数位于类中,则应该使用ClassName.gini。
例如:
class ClassName:
def gini(rows):
...
def info_gain(left, right, current_uncertainty):
...
ClassName.gini(rows)
...https://stackoverflow.com/questions/72327637
复制相似问题