Median

Yaser Rahmati | یاسر رحمتی

Definition

  • The median is the middle number in a sorted list of numbers.

  • If there are an even number of data points, the median is the average of the two middle numbers.

Example 1

Let me walk you through an example in detail.

Step1:

Let’s say we have the following set of numbers:

[ 5, 3, 9, 2, 8, 4, 7 ]

Step 2:

Sort the numbers in ascending order:

[ 2, 3, 4, 5, 7, 8, 9 ]

Step 3:

Count the total numbers In this case, we have 7 numbers in the set.

Step 4:

Identify the middle number. Since there are 7 numbers, the middle number is the fourth number, which is 5. So, in this case, the median is 5.

Example 2

If we had an even number of data points, we would take the average of the two middle numbers. For example, if we had the data:

[ 3, 6, 2, 9, 1, 7 ]

Step 1:

Sort the numbers in ascending order:

[ 1, 2, 3, 6, 7, 9 ]

Step 2:

Count the total numbers In this case, we have 6 numbers in the set.

Step 3:

Identify the two middle numbers The two middle numbers are 3 and 6. In this case, the median is the average of these two numbers:

Median=3+62=4.5\text{Median} = \frac{3 + 6}{2} = 4.5

So, the median of the given set of numbers is 4.5.

Python

This code calculates the median of the given list of numbers using np.median from the numpy library and prints the result.

import numpy as np

numbers = [3, 6, 2, 9, 1, 7]

med = np.median(numbers)

print("Median:", med)

My Training Video

Keywords

Web Development (Django, Flask) , Data Science (Pandas, NumPy, Matplotlib) , Machine Learning (Scikit-learn, TensorFlow, PyTorch) , Artificial Intelligence , Automation , GUI Development (Tkinter, PyQt) , Game Development (Pygame) , Scientific Computing , Financial Analysis (Pandas, NumPy) , Network Programming , Image Processing (OpenCV) , Web Scraping (Beautiful Soup, Scrapy) , Internet of Things (IoT) Development , Robotics , Cybersecurity , Mobile App Development (Kivy) , Cloud Computing (Boto3, AWS Lambda) , Big Data Analysis (PySpark) , Geographical Information Systems , Natural Language Processing (NLTK, spaCy) , Data Visualization (Seaborn, Plotly) , Bioinformatics (Biopython) , Multimedia Applications , Education and Teaching

Last updated