{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_list_odd = [6, 20, 15, 13, 2, 6, 8] # sorted [2, 6, 6, 8, 13, 15, 20]\n", "x_list_even = [6, 20, 15, 13, 2, 6, 8, 25] # sorted [2, 6, 6, 8, 13, 15, 20, 25]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function to explicitly compute the median from its definition" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def my_median(values):\n", " \"\"\"\n", " Compute the median of a list of values.\n", " @param values the list of values.\n", " @return the median.\n", " \"\"\"\n", " \n", " count = len(values) # count of values \n", " middle_index = count//2 # index of the middle value when count is odd\n", " sorted_values = sorted(values) # must work with sorted values\n", " \n", " if count%2 == 1: # odd number of values\n", " median_value = sorted_values[middle_index]\n", " else: # even number of values\n", " low_median_value = sorted_values[middle_index - 1]\n", " high_median_value = sorted_values[middle_index]\n", " median_value = (low_median_value + high_median_value)/2\n", "\n", " return median_value " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_median_odd = my_median(x_list_odd)\n", "my_median_even = my_median(x_list_even)\n", " \n", "print(f'my_median(x_list_odd) = {my_median_odd:5.2f}')\n", "print(f'my_median(x_list_even) = {my_median_even:5.2f}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## statistics.median function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import statistics\n", "\n", "median_list_odd = statistics.median(x_list_odd)\n", "median_list_even = statistics.median(x_list_even)\n", "\n", "print(f'statistics.median(x_list_odd) = {median_list_odd:5.2f}')\n", "print(f'statistics.median(x_list_even) = {median_list_even:5.2f}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create NumPy arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "x_array_odd = np.array(x_list_odd)\n", "x_array_even = np.array(x_list_even)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.median function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np_median_list_odd = np.median(x_list_odd) # operate on an odd-numbered list\n", "np_median_list_even = np.median(x_list_even) # operate on an even-numbered list\n", "np_median_array_odd = np.median(x_array_odd) # operate on an odd-numbered array\n", "np_median_array_even = np.median(x_array_even) # operate on an even-numbered array\n", "\n", "print(f'np.median(x_list_odd) = {np_median_list_odd:5.2f}')\n", "print(f'np.median(x_list_even) = {np_median_list_even:5.2f}')\n", "print(f'np.median(x_array_odd) = {np_median_array_odd:5.2f}')\n", "print(f'np.median(x_array_even) = {np_median_array_even:5.2f}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2-d array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matrix = np.array([[2, 5, 6, 3],\n", " [1, 7, 0, 4],\n", " [3, 1, 2, 6]])\n", "\n", "median_of_all = np.median(matrix)\n", "median_of_cols = np.median(matrix, axis=0)\n", "median_of_rows = np.median(matrix, axis=1)\n", "\n", "print(f'np.median(matrix) [all] = {median_of_all}')\n", "print(f'np.median(matrix, axis=0) [cols] = {median_of_cols}')\n", "print(f'np.median(matrix, axis=1) [rows] = {median_of_rows}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }