MCPcopy Index your code
hub / github.com/python/cpython / mode

Function mode

Lib/statistics.py:467–494  ·  view source on GitHub ↗

Return the most common data point from discrete or nominal data. ``mode`` assumes discrete data, and returns a single value. This is the standard treatment of the mode as commonly taught in schools: >>> mode([1, 1, 2, 3, 3, 3, 3, 4]) 3 This also works with nominal (non

(data)

Source from the content-addressed store, hash-verified

465
466
467def mode(data):
468 """Return the most common data point from discrete or nominal data.
469
470 ``mode`` assumes discrete data, and returns a single value. This is the
471 standard treatment of the mode as commonly taught in schools:
472
473 >>> mode([1, 1, 2, 3, 3, 3, 3, 4])
474 3
475
476 This also works with nominal (non-numeric) data:
477
478 >>> mode(["red", "blue", "blue", "red", "green", "red", "red"])
479 'red'
480
481 If there are multiple modes with same frequency, return the first one
482 encountered:
483
484 >>> mode(['red', 'red', 'green', 'blue', 'blue'])
485 'red'
486
487 If *data* is empty, ``mode``, raises StatisticsError.
488
489 """
490 pairs = Counter(iter(data)).most_common(1)
491 try:
492 return pairs[0][0]
493 except IndexError:
494 raise StatisticsError('no mode for empty data') from None
495
496
497def multimode(data):

Callers 3

mainFunction · 0.85
setupFunction · 0.85
clock.pyFile · 0.85

Calls 3

CounterClass · 0.90
StatisticsErrorClass · 0.85
most_commonMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…