Absolute Deviation and Absolute Mean Deviation Using NumPy | python

Rejection:
Rejection — it is a measure of the difference between the observed value of a variable and some other value, often the mean of that variable.

Absolute Deviation:
The absolute deviation of a dataset item — it is the absolute difference between this element and this point. The absolute deviation of observations X1, X2, X3,… .., Xn around the value A is defined as —

For discrete (ungrouped) data

For continuous (ungrouped) data

Absolute mean deviation:
Absolute mean deviation measures spread and spread of data, preferably median, in terms of absolute deviation. The absolute deviation of observations X1, X2, X3, ……, Xn is minimal when measured around the median, i.e. A — median of data. Then the resulting absolute deviation is called the absolute average deviation and is defined as:

For discrete (ungrouped) data — 

For continuous (ungrouped) data — 

Decide:

  1. A dataset with a higher absolute mean deviation (or absolute deviation) has more variability.
  2. A dataset with a lower absolute mean deviation (or absolute deviation) is preferred. 
    – & gt; If there are two datasets with absolute averages AMD1 and AMD2 and AMD1 & gt; AMD2, then AMD1 data is considered to have more volatility than AMD2 data.

Example:
Below is the number of candidates enrolled each day within the last 20 days for Python.Engineering -DS & amp; Algo — 
75, 69, 56, 46, 47, 79, 92, 97, 89, 88, 36, 96, 105, 32, 116, 101, 79, 93, 91, 112

Code # 1: Absolute rejection using NumPy

# Import mean, absolute value from numy

from numpy import mean, absolute

 

data = [ 75 , 69 , 56 , 46 , 47 , 79 , 92 , 97 , 89 , 88 ,

36 , 96 , 105 , 32 , 116 , 101 , 79 , 93 , 91 , 112 ]

 
# Suppose any point A about which # absolute deviation is calculated

A = 79

 

sum = 0   # Initialize sum to 0

 
# Absolute deviation calculation

  

for i in range ( len (data)):

av = absolute (data [i] - A)  # Absolute difference value

  # of each data point and A

 

# Sum all these absolute values ​​

sum = sum + av 

 
# Amount divided by the length of data outputs
# absolute rejection

print ( sum / len (data)) 

Exit:

 20.15  

Code # 2: Absolute mean deviation using NumPy

# Import mean, absolute value from numy

from numpy import mean, absolute

 

data = [ 75 , 69 , 56 , 46 , 47 , 79 , 92 , 97 , 89 , 88

36 , 96 , 105 , 32 , 116 , 101 , 79 , 93 , 91 , 112 ]

 
# Absolute mean deviation

mean (absolute (data - mean (data)))

Exit:

20.055

Code # 3: Absolute mean deviation using pandas

# Import pandas library as pd

import pandas as pd

 

data = [ 75 , 69 , 56 , 46 , 47 , 79 , 92 , 97 , 89 , 88 ,

  36 , 96 , 105 , 32 , 116 , 101 , 79 , 93 , 91 , 112 ]

 
# Create a given data data frame

df = pd.DataFrame (data)

  
# Absolute mean deviation

df.mad ()  # mad () - function of mean absolute deviation

Exit:

 20.055