Skip to content

Instantly share code, notes, and snippets.

@ngbala6
Created May 15, 2021 10:38
Show Gist options
  • Select an option

  • Save ngbala6/a7f6d50bc61aafd8c76b8f980ac13039 to your computer and use it in GitHub Desktop.

Select an option

Save ngbala6/a7f6d50bc61aafd8c76b8f980ac13039 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Import Packages"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import statistics\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sample Data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"data = [1,2,4,5,6,76,8,45]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using Formula without Python Packages\n",
"\n",
"If number of elements = odd -------> n/2\n",
"\n",
"If number of elements = even ------> (n+1)/2\n",
"\n",
"We can't use the (n+1)/2 exactly in coding. Because finding the position using float values gets error.\n",
"So, I am slightly changing the formula for Even.\n",
"\n",
"m1 = (n/2)th position\n",
"\n",
"m2 = ((n/2) - 1) th position\n",
"\n",
"Median = (m1 + m2)/2\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorted Data: [1, 2, 4, 5, 6, 8, 45, 76]\n",
"Position of the data: 4 and 3\n",
"Values in the Position: 6 and 5\n",
"Median: 5.5\n"
]
}
],
"source": [
"sorted_data_median = sorted(data)\n",
"\n",
"print(\"Sorted Data:\", sorted_data_median)\n",
"\n",
"m1 = int(len(sorted_data_median)/ 2)\n",
"m2 = int((m1 - 1))\n",
"\n",
"print(f\"Position of the data: {m1} and {m2}\")\n",
"print(f\"Values in the Position: {sorted_data_median[m1]} and {sorted_data_median[m2]}\")\n",
"\n",
"median = (sorted_data_median[m1] + sorted_data_median[m2])/2\n",
"\n",
"print(\"Median:\", median)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using numpy package"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.5\n"
]
}
],
"source": [
"print(np.median(data))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using Statistics package"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.5\n"
]
}
],
"source": [
"print(statistics.median(data))"
]
},
{
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
@ngbala6

ngbala6 commented May 15, 2021

Copy link
Copy Markdown
Author

median notebook file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment