{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Function to test quartiles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def count_of_less(values, q):\n", " \"\"\" \n", " Return the count of values less than quartile value q.\n", " @param values the list of values.\n", " @param q the quartile value.\n", " @return the count.\n", " \"\"\"\n", " \n", " count = 0;\n", " \n", " for v in values:\n", " if (v < q):\n", " count += 1\n", " \n", " return count" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some random values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "x_list = [int(random.uniform(0, 1000)) for _ in range(0, 100)]\n", "count = len(x_list)\n", "\n", "print(f'{count} data values')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## First quartile" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "q1 = np.percentile(x_list, 25)\n", "print(f'Q1 = np.percentile(x_list, 25) = {q1:.2f}')\n", "\n", "q1_under = count_of_less(x_list, q1)\n", "print(f'count of values less than Q1: {q1_under}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Second quartile" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q2 = np.percentile(x_list, 50)\n", "print(f'Q2 = np.percentile(x_list, 50) = {q2:.2f}')\n", "\n", "list_median = np.median(x_list)\n", "print(f'np.median(x_list) = {list_median}')\n", "\n", "q2_under = count_of_less(x_list, q2)\n", "print(f'count of values less than Q2: {q2_under}')\n", "\n", "list_median = np.median(x_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Third quartile" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q3 = np.percentile(x_list, 75)\n", "print(f'Q3 = np.percentile(x_list, 75) = {q3:.2f}')\n", "\n", "q3_under = count_of_less(x_list, q3)\n", "print(f'count of values less than Q3: {q3_under}')" ] } ], "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 }