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)
| 465 | |
| 466 | |
| 467 | def 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 | |
| 497 | def multimode(data): |
no test coverage detected
searching dependent graphs…