簡體   English   中英

在字典中查找最小值的鍵

[英]find key of smallest value in a dictionary

我寫了這個方法,它假設找到具有最小值的值(做一個 Kaggle 練習)

candidate_max_leaf_nodes = [5, 25, 50, 100, 250, 500]
# Write loop to find the ideal tree size from candidate_max_leaf_nodes
mae_results_dict_val = {}
for max_leaf_nodes in candidate_max_leaf_nodes:
    mae_result = get_mae(max_leaf_nodes,train_X,val_X,train_y,val_y)
    print('mae for %d is %d'%(max_leaf_nodes,mae_result))
    mae_results_dict_val[max_leaf_nodes]=mae_result

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

best_tree_size2 = min(mae_results_dict_val,key=mae_results_dict_val.get) #works but I copied this code

min(mae_results_dict_val,key=mae_results_dict_val.get)是做什么的?

min()返回排序后的第一個值中的值。 鍵指定對值進行排序的方式。 key=mae_results_dict_val.get表示列表將按字典的值排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM