python - 在字典中查找最小值 - TypeError int object not callable

标签 python

我的 ML 类(class)要求我找到决策树中误差值最小的叶子。叶子和错误值存储在字典中(输出下方)

{5:35044.51299744237、25:29016.41319191076、50:27405.930473214907、100:27282.50803885739、250:27893.822225701646、 500:29454.18598068598}

现在,这本字典中的最佳解决方案应该是 100,因为它的错误率最小。

代码片段如下:

candidate_max_leaf_nodes = [5, 25, 50, 100, 250, 500]

for i in candidate_max_leaf_nodes:
#stores key,value pair of leaf_nodes, and their error values
    mydict[i] = get_mae(i,train_X,val_X,train_y,val_y)
print(mydict)


#  Find the best value of max_leaf_nodes (it will be either 5, 25, 50, 100, 250 or 500)

我尝试获取最小值的代码如下:

tmp = min(mydict.values()) 
best_tree_size = [key for key in mydict if mydict[key] == tmp] 

但是,我不断收到TypeError:'int'对象不可调用。有人可以解释我哪里出错了吗?我能以更优化的方式找到最小值吗?

最佳答案

您必须定义一个名为 min 的整型变量。不要这样做(或者在调用 min() 函数之前使用 del min)。

关于python - 在字典中查找最小值 - TypeError int object not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60188755/

相关文章:

Python:在数组中填充多边形区域

python - 惰性模块变量——可以吗?

python - 使用 lambda 时索引超出范围

python - 在 ParaView 中通过 Python 脚本保存动画

python - 名称错误 : name is not defined error in class __init__() method

Matlab 区间数的 Python 模拟,如 1 :2:5

python - 如何在Python中分析列?

python - 按时间过滤 Pandas 数据框(不是日期)

python - 如何使用 openpyxl 和 python3 为 Excel 工作表中的一系列单元格(列和行)提供字体颜色?

python - 我需要只出现一次的元素数量