Machine Learning – Median

The median is used to get the middle value in a dataset. It is a measure of central tendency and is calculated by:

  • Arranging the values in the dataset in order and finding the value that lies in the middle. This will only happen if there are an odd number of values in the dataset
  • Calculating the average of the two middle values. This will only happen if there is an even number of values in the dataset.

Note: Median is the mid-point value.

Mean vs Median

Mean is used to get the mean i.e. the average values. The mean is affected by the outlier.

The median is a useful measure of central tendency because it is not affected by outliers, meaning that extreme values do not significantly affect the value of the median. Let us understand what this means.

An outlier is a data point that is significantly different from the other data points in a dataset. It can skew the results of statistical measures, like the mean, but has little to no effect on the median.

For example, in the dataset [1, 2, 3, 4, 5, 50], the number 50 is an outlier. The mean would be heavily influenced by this extreme value, but the median would still be 3.5. By focusing on the median, you get a better sense of the central tendency without the distortion caused by outliers.

Coding Example – Calculate Median with Python

Let us see an example. To calculate the median in Python, we will use the median() method of the NumPy package.

The NumPy package is used for scientific computing with Python. We will also use the Pandas library to create a Pandas DataFrame. The Pandas DataFrame is a Two-dimensional tabular data structure i.e. table with rows and columns.

Read: Free NumPy Tutorial

Read: Free Pandas Tutorial

The following is the Python code to get the median:

The following is the output:


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Machine Learning - Mean
Machine Learning - Mode
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading