- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Absolute Deviation and Absolute Mean Deviation using NumPy

In Statistical analysis study of data variability in a sample indicates how dispersed are the values in a given data sample. The two important ways we calculate the variability are **Absolute Deviation** and ** Mean Absolute Deviation**.

## Absolute Deviation

In this method we first find the mean value of the given sample and then calculate the difference between each value and the mean value of the sample called as the absolute deviation value of each data sample. So for the values higher than mean the value the deviation value will be positive and for those lower than the mean value the deviation value will be negative. Next we take the absolute values using the **absolute** function to make each deviation as positive. Summing up all these absolute deviation gives a positive result. Without absolute value the sum of these deviations would be zero.

In the below example we take a data sample and calculate the absolute deviation for each data element.

## Example

from numpy import mean, absolute data = [12, 42, 53, 13, 112] # Find mean value of the sample M = mean(data) print "Sample Mean Value = ",mean(data) print "\n" # Calculate absolute deviation print "Data-Mean","","deviation" for i in range(len(data)): dev = absolute(data[i] - M) print data[i],"-",M,round((dev),2)

## Output

Running the above code gives us the following result −

Sample Mean Value = 46.4 Data-Mean deviation 12 - 46.4 34.4 42 - 46.4 4.4 53 - 46.4 6.6 13 - 46.4 33.4 112 - 46.4 65.6

## Mean Absolute Deviation (MAD)

Mean Absolute Deviation (MAD) is the mean value of all the absolute deviations we calculate for each data point. Taking the same sample as in previous example, we add code to sum up the value of the absolute deviations and divide it by the sample size.

## Example

from numpy import mean, absolute data = [12, 42, 53, 13, 112] # Find mean value of the sample M = mean(data) print "Sample Mean Value = ",mean(data) sum = 0 # Calculate mean absolute deviation for i in range(len(data)): dev = absolute(data[i] - M) sum = sum + round(dev,2) print "Mean Absolute Deviation: ", sum/len(data)

## Output

Running the above code gives us the following result −

Sample Mean Value = 46.4 Mean Absolute Deviation: 28.88

- Related Questions & Answers
- Program for Mean Absolute Deviation in C++
- Write a Python program to find the mean absolute deviation of rows and columns in a dataframe
- Plot mean and standard deviation in Matplotlib
- Python – Mean deviation of Elements
- How to create boxplot using mean and standard deviation in R?
- Variance and Standard Deviation
- C++ code to find minimum arithmetic mean deviation
- How to find mean and standard deviation from frequency table in R?
- PyTorch – How to normalize an image with mean and standard deviation?
- Absolute Positioning Using CSS
- How to create a line chart with mean and standard deviation using ggplot2 in R?
- How to compute the mean and standard deviation of a tensor in PyTorch?
- Calculate the absolute value element-wise in Numpy
- Average Returns and Standard Deviation of Securities
- CSS Absolute and Relative Units