
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Program for Mean Absolute Deviation in C++
- Write a Python program to find the mean absolute deviation of rows and columns in a dataframe
- Python – Mean deviation of Elements
- Plot mean and standard deviation in Matplotlib
- How to create boxplot using mean and standard deviation in R?
- C++ code to find minimum arithmetic mean deviation
- PyTorch – How to normalize an image with mean and standard deviation?
- Variance and Standard Deviation
- How to create a line chart with mean and standard deviation using ggplot2 in R?
- How to find mean and standard deviation from frequency table in R?
- Generate random numbers by giving certain mean and standard deviation in Excel
- Absolute Positioning Using CSS
- Calculate the absolute value element-wise in Numpy
- How to compute the mean and standard deviation of a tensor in PyTorch?
- Return the standard deviation of the masked array elements in NumPy
