【问题标题】:Minimum in Python DictionaryPython 字典中的最小值
【发布时间】:2020-06-06 13:21:59
【问题描述】:

当我使用 min() 时,我有 python Dict。查看我的代码

scores = {leaf_size: round(get_mae(leaf_size, train_X, val_X, train_y, val_y)) for leaf_size in candidate_max_leaf_nodes}
best_tree_size = min(scores, key=scores.get)

get_mae() 只是一个计算平均绝对误差的函数。

但它显示以下错误

TypeError                                 Traceback (most recent call last)
<ipython-input-48-aa5b1fc6e492> in <module>
      8 # print(dict)
      9 scores = {leaf_size: round(get_mae(leaf_size, train_X, val_X, train_y, val_y)) for leaf_size in candidate_max_leaf_nodes}
---> 10 best_tree_size = min(scores, key=scores.get)
     11 # list = scores.items()
     TypeError: 'int' object is not callable

【问题讨论】:

    标签: python sorting dictionary min


    【解决方案1】:

    您可能使用了 min 作为变量并隐藏了内置函数。

    试试这个:

    best_tree_size = __builtins__.min(scores, key=scores.get)
    

    这将使用实际的 min() 函数,而不是引用 int 值的 min 变量。

    【讨论】:

      猜你喜欢
      • 2016-05-19
      • 2012-07-28
      • 2020-07-26
      • 2021-07-30
      • 2012-07-28
      • 2015-07-18
      • 2019-02-09
      • 1970-01-01
      • 2014-06-25
      相关资源
      最近更新 更多