{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "x_list_unimodal = [4, 7, 4, 0, 3, 4, 7] # sorted [0, 3, 4, 4, 4, 7, 7]\n", "x_list_bimodal = [5, 0, 5, 0, 3, 5, 0] # sorted [0, 0, 0, 3, 5, 5, 5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function to explicitly compute the mode from its definition" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def modes_of_list(values):\n", " \"\"\"\n", " Compute the mode(s) of a list of values.\n", " @param values the list of values.\n", " @return the list of mode(s)\n", " \"\"\"\n", " \n", " count = len(values)\n", " modes = [];\n", " \n", " if count == 0: # if no values then no modes\n", " return []\n", " \n", " sorted_values = sorted(values) # sort to group the values\n", " longest_length = 0 # longest length of consecutive values so far\n", " current_length = 1 # current length of consecutive values\n", " previous_value = sorted_values[0] # previous value in the sorted list\n", " \n", " for i in range(1, count + 1): # start at 1, go up to and include the count\n", " if (i == count) or (sorted_values[i] != previous_value):\n", " # End of the values or the value has changed from the previous one.\n", " if current_length == longest_length:\n", " modes.append(previous_value) # ties the longest_length, so append to modes\n", " elif current_length > longest_length:\n", " longest_length = current_length # found a longer length\n", " modes = [previous_value] # so start a new modes list\n", " \n", " if i < count:\n", " previous_value = sorted_values[i] # start new consecutive values\n", " current_length = 1\n", " else:\n", " # The value hasn't changed, so increment the current length.\n", " current_length += 1\n", " \n", " return modes " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "modes_of_list(x_list_unimodal) = [4]\n" ] } ], "source": [ "modes_unimodal = modes_of_list(x_list_unimodal)\n", "\n", "print(f'modes_of_list(x_list_unimodal) = {modes_unimodal}')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "modes_of_list(my_modes_bimodal) = [0, 5]\n" ] } ], "source": [ "modes_bimodal = modes_of_list(x_list_bimodal)\n", "\n", "print(f'modes_of_list(my_modes_bimodal) = {modes_bimodal}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## statistics.mode function" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "statistics.mode(x_list_unimodal) = 4\n" ] } ], "source": [ "import statistics\n", "mode_list_unimodal = statistics.mode(x_list_unimodal)\n", "\n", "print(f'statistics.mode(x_list_unimodal) = {mode_list_unimodal}')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "StatisticsError", "evalue": "no unique mode; found 2 equally common values", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mStatisticsError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmode_list_bimodal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstatistics\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_list_bimodal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'statistics.mode(x_list_bimodal) = {mode_list_bimodal}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/lib/python3.7/statistics.py\u001b[0m in \u001b[0;36mmode\u001b[0;34m(data)\u001b[0m\n\u001b[1;32m 504\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mtable\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 505\u001b[0m raise StatisticsError(\n\u001b[0;32m--> 506\u001b[0;31m \u001b[0;34m'no unique mode; found %d equally common values'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 507\u001b[0m )\n\u001b[1;32m 508\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mStatisticsError\u001b[0m: no unique mode; found 2 equally common values" ] } ], "source": [ "mode_list_bimodal = statistics.mode(x_list_bimodal)\n", "print(f'statistics.mode(x_list_bimodal) = {mode_list_bimodal}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## String values" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "statistics.mode(colors) = red\n" ] } ], "source": [ "colors = ['red', 'white', 'red', 'blue', 'white', 'red']\n", "colors_mode = statistics.mode(colors)\n", "\n", "print(f'statistics.mode(colors) = {colors_mode}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create NumPy arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "x_array_unimodal = np.array(x_list_unimodal)\n", "x_array_bimodal = np.array(x_list_bimodal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## scipy.stats.mode function" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "stats.mode(x_list_unimodal) = ModeResult(mode=array([4]), count=array([3]))\n", "stats.mode(x_list_bimodal) = ModeResult(mode=array([0]), count=array([3]))\n" ] } ], "source": [ "from scipy import stats\n", "\n", "stats_mode_list_unimodal = stats.mode(x_list_unimodal) # operate on a unimodal list\n", "stats_mode_list_bimodal = stats.mode(x_list_bimodal) # operate on a bimodal list\n", "\n", "print(f'stats.mode(x_list_unimodal) = {stats_mode_list_unimodal}')\n", "print(f'stats.mode(x_list_bimodal) = {stats_mode_list_bimodal}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2-d arrays" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "stats.mode(matrix) = ModeResult(mode=array([[0, 3, 2, 2, 1, 1]]), count=array([[1, 3, 2, 3, 2, 4]]))\n" ] } ], "source": [ "matrix = np.array([[1, 3, 4, 2, 2, 1],\n", " [5, 2, 2, 1, 4, 1],\n", " [3, 3, 2, 2, 1, 1],\n", " [0, 3, 4, 2, 1, 1]])\n", "\n", "matrix_modes_columns = stats.mode(matrix)\n", "print(f'stats.mode(matrix) = {matrix_modes_columns}')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "stats.mode(matrix, axis=None) = ModeResult(mode=array([1]), count=array([8]))\n" ] } ], "source": [ "matrix_modes_all = stats.mode(matrix, axis=None)\n", "print(f'stats.mode(matrix, axis=None) = {matrix_modes_all}')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }