user8838577
user8838577

Reputation:

Minimum in Python Dictionary

I have python Dict, when I am using min(). See my code

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() is just a function for calculating mean absolute error.

but it shows following error

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

Upvotes: 4

Views: 326

Answers (1)

Raymond Hettinger
Raymond Hettinger

Reputation: 226396

It is possible that you've used min as a variable and have shadowed the built-function.

Try this:

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

That will use the actual min() function rather than the min variable that references the int value.

Upvotes: 6

Related Questions