在 python 中找到两个最大值对象

Finding two maximum valued Object in python

我写了一个 class 来保存三个变量 column-name, cut-value 和 info-gain [1]。我在 for 循环中提供它,如下所示:

def best_split(X, y):
 best_gain=0
 Leaf_collection = []

 for column in X:
  ....
  info_gain = f(column, y)
  if info_gain >= best_gain:
    best_gain= info_gain
    Leaf_colletion.append(Leaf(column, value, info_gain))

 return Leaf_collection

现在,目的是过滤掉 iris 数据集中的这两种特殊情况,其中 info_gain 对于两个列值相同:

column ; value ;  info-gain
petal width (cm) ; 0.6 ; 0.5893309047577491
petal length (cm); 1.9 ; 0.5893309047577491

关于如何在同一个 for 循环中或之后如何做到这一点的任何建议,找到一些最大值。我尝试了一种找到最大值的方法,但不幸的是,这只过滤掉了两个案例中的一个。 :(

非常感谢,

[1]

class Leaf:

def __init__(self, column, value, info_gain):
    self.column = column //string
    self.value = value // float 
    self.info_gain = info_gain //float

跟踪所有叶子,然后在最后过滤:

def best_split(X, y):
    best_gain = 0
    Leaf_collection = []

    for column in X:
        # ....
        info_gain = f(column, y)
        Leaf_collection.append(Leaf(column, value, info_gain))
        best_gain = max(best_gain, info_gain)

    # return all leaves with best gain
    return [
        leaf for leaf in Leaf_collection if leaf.info_gain >= best_gain
    ]