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 nomi
(data)
| 684 | |
| 685 | |
| 686 | def mode(data): |
| 687 | """Return the most common data point from discrete or nominal data. |
| 688 | |
| 689 | ``mode`` assumes discrete data, and returns a single value. This is the |
| 690 | standard treatment of the mode as commonly taught in schools: |
| 691 | |
| 692 | >>> mode([1, 1, 2, 3, 3, 3, 3, 4]) |
| 693 | 3 |
| 694 | |
| 695 | This also works with nominal (non-numeric) data: |
| 696 | |
| 697 | >>> mode(["red", "blue", "blue", "red", "green", "red", "red"]) |
| 698 | 'red' |
| 699 | |
| 700 | If there are multiple modes with same frequency, return the first one |
| 701 | encountered: |
| 702 | |
| 703 | >>> mode(['red', 'red', 'green', 'blue', 'blue']) |
| 704 | 'red' |
| 705 | |
| 706 | If *data* is empty, ``mode``, raises StatisticsError. |
| 707 | |
| 708 | """ |
| 709 | pairs = Counter(iter(data)).most_common(1) |
| 710 | try: |
| 711 | return pairs[0][0] |
| 712 | except IndexError: |
| 713 | raise StatisticsError('no mode for empty data') from None |
| 714 | |
| 715 | |
| 716 | def multimode(data): |
no test coverage detected