By Khalid Ibrahim


2019-12-02 22:54:52 8 Comments

I have records dictionary as:

{0.001: {'num_iter': 8112, 'accuracy': 98.41827768014059}, 
 0.002: {'num_iter': 4255, 'accuracy': 98.41827768014059}, 
 0.003: {'num_iter': 2837, 'accuracy': 98.41827768014059}, 
 0.004: {'num_iter': 2128, 'accuracy': 98.41827768014059}, 
 0.005: {'num_iter': 1702, 'accuracy': 98.41827768014059}, 
 0.006: {'num_iter': 1417, 'accuracy': 98.41827768014059}, 
 0.007: {'num_iter': 4, 'accuracy': 63.620386643233736}, 
 0.008: {'num_iter': 4, 'accuracy': 62.741652021089635}, 
 0.009: {'num_iter': 3, 'accuracy': 52.196836555360285}}

All I want is to get the key with the highest accuracy and if there is more than one, then consider the one with the lowest num_iter. In this case it will be 0.006.

Any help?

3 comments

@Andrej Kesely 2019-12-02 22:59:21

You can use sorted() with custom key= parameter:

d = {0.001: {'num_iter': 8112, 'accuracy': 98.41827768014059},
 0.002: {'num_iter': 4255, 'accuracy': 98.41827768014059},
 0.003: {'num_iter': 2837, 'accuracy': 98.41827768014059},
 0.004: {'num_iter': 2128, 'accuracy': 98.41827768014059},
 0.005: {'num_iter': 1702, 'accuracy': 98.41827768014059},
 0.006: {'num_iter': 1417, 'accuracy': 98.41827768014059},
 0.007: {'num_iter': 4, 'accuracy': 63.620386643233736},
 0.008: {'num_iter': 4, 'accuracy': 62.741652021089635},
 0.009: {'num_iter': 3, 'accuracy': 52.196836555360285}}

print(sorted(d.items(), key=lambda k: (-k[1]['accuracy'], k[1]['num_iter']))[0][0])

Prints:

0.006

Note: If you want just one value - use max() function - complexity O(n) instead of O(nlogn) of sorted()

@alex067 2019-12-02 23:01:03

What does -k do?

@Andrej Kesely 2019-12-02 23:02:40

@alex067 -k[1]['accuracy'] negates the 'accuracy' value - that way we are sorting in reversed order - from highest accuracy to lowest.

@RaySteam 2019-12-02 23:04:05

This has a time complexity of O(n*logn)

@kaya3 2019-12-02 23:04:10

If you just want a single maximum element, use max. It accepts a key parameter just like sorted, but runs in O(n) time instead of O(n log n), meaning it is more efficient.

>>> max(d, key=lambda k: (d[k]['accuracy'], -d[k]['num_iter']))
0.006

Here, the keys are compared by a tuple of (accuracy, -num_iter) meaning it selects the key with the maximum accuracy, using the minimum num_iter as a tie-breaker.

@Khalid Ibrahim 2019-12-02 23:10:34

Thank you for the explanation :) I accepted it.

@RaySteam 2019-12-02 23:02:59

You could use the key parameter of max:

data = {0.001: {'num_iter': 8112, 'accuracy': 98.41827768014059},
        0.002: {'num_iter': 4255, 'accuracy': 98.41827768014059},
        0.003: {'num_iter': 2837, 'accuracy': 98.41827768014059},
        0.004: {'num_iter': 2128, 'accuracy': 98.41827768014059},
        0.005: {'num_iter': 1702, 'accuracy': 98.41827768014059},
        0.006: {'num_iter': 1417, 'accuracy': 98.41827768014059},
        0.007: {'num_iter': 4, 'accuracy': 63.620386643233736},
        0.008: {'num_iter': 4, 'accuracy': 62.741652021089635},
        0.009: {'num_iter': 3, 'accuracy': 52.196836555360285}}

result = max(data, key=lambda x : (data[x]['accuracy'], -1 * data[x]['num_iter']))
print(result)

Output

0.006

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

34 Answered Questions

[SOLVED] Get key by value in dictionary

  • 2011-11-05 21:09:18
  • user998316
  • 1279018 View
  • 580 Score
  • 34 Answer
  • Tags:   python dictionary

36 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3093568 View
  • 2644 Score
  • 36 Answer
  • Tags:   python datetime time

8 Answered Questions

[SOLVED] How to return dictionary keys as a list in Python?

15 Answered Questions

[SOLVED] Add new keys to a dictionary?

11 Answered Questions

[SOLVED] How to remove a key from a Python dictionary?

20 Answered Questions

[SOLVED] Getting key with maximum value in dictionary?

11 Answered Questions

[SOLVED] How do I get a substring of a string in Python?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2739426 View
  • 2010 Score
  • 11 Answer
  • Tags:   python string

25 Answered Questions

[SOLVED] How can I sort a dictionary by key?

16 Answered Questions

[SOLVED] Check if a given key already exists in a dictionary

  • 2009-10-21 19:05:09
  • Mohan Gulati
  • 3235061 View
  • 2683 Score
  • 16 Answer
  • Tags:   python dictionary

Sponsored Content