我的 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/