【问题标题】:Finding two maximum valued Object in python在python中找到两个最大值的对象
【发布时间】:2021-03-13 04:06:44
【问题描述】:

我写了一个类来保存三个变量 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

【问题讨论】:

    标签: python pandas algorithm class decision-tree


    【解决方案1】:

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

    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
        ]  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      相关资源
      最近更新 更多