Numpy Tutorial

Creating NumPy Array

NumPy Array Manipulation

Matrix in NumPy

Operations on NumPy Array

Reshaping NumPy Array

Indexing NumPy Array

Arithmetic operations on NumPy Array

Linear Algebra in NumPy Array

NumPy and Random Data

Sorting and Searching in NumPy Array

Universal Functions

Working With Images

Projects and Applications with NumPy

Absolute Deviation and Absolute Mean Deviation using NumPy

Absolute deviation is a measure of variability that captures the distance between each observation in a dataset and the central point of that dataset. The absolute mean deviation (AMD) is the average of these absolute deviations.

Here's a tutorial on how to calculate both absolute deviation and absolute mean deviation using NumPy:

1. Absolute Deviation

The absolute deviation of an observation from its central point is simply the absolute value of the difference between the observation and the central point.

Formula: ADi​=∣xi​−m∣

Where:

  • xi​ = each observation in the dataset
  • m = the central point (usually the mean or median)

Let's implement this using Python and NumPy:

import numpy as np

# Sample data
data = np.array([2, 4, 4, 4, 5, 5, 7, 9])

# Calculate mean
mean_data = np.mean(data)

# Calculate absolute deviation
absolute_deviation = np.abs(data - mean_data)
print(absolute_deviation)

2. Absolute Mean Deviation

To get the absolute mean deviation, you simply take the average of all the absolute deviations.

Formula: AMD=n1​∑i=1n​∣xi​−m∣

Where:

  • n = number of observations
  • xi​ = each observation in the dataset
  • m = the central point (usually the mean or median)

Let's compute the AMD using Python and NumPy:

# Calculate absolute mean deviation
absolute_mean_deviation = np.mean(absolute_deviation)
print(absolute_mean_deviation)

Putting it all together:

import numpy as np

# Sample data
data = np.array([2, 4, 4, 4, 5, 5, 7, 9])

# Calculate mean
mean_data = np.mean(data)

# Calculate absolute deviation
absolute_deviation = np.abs(data - mean_data)

# Calculate absolute mean deviation
absolute_mean_deviation = np.mean(absolute_deviation)

print("Absolute Deviation:", absolute_deviation)
print("Absolute Mean Deviation:", absolute_mean_deviation)

That's it! With just a few lines of code using NumPy, you can easily compute the absolute deviation and absolute mean deviation of a dataset.

1. Absolute Deviation NumPy:

Absolute deviation measures how much individual elements of a dataset deviate from the mean.

import numpy as np

data = np.array([2, 4, 4, 4, 5, 5, 7, 9])

mean = np.mean(data)
absolute_deviation = np.abs(data - mean)

print("Absolute Deviation:", absolute_deviation)

2. Absolute Mean Deviation in NumPy:

Absolute Mean Deviation is the average of absolute deviations.

import numpy as np

data = np.array([2, 4, 4, 4, 5, 5, 7, 9])

mean = np.mean(data)
absolute_deviation = np.abs(data - mean)
absolute_mean_deviation = np.mean(absolute_deviation)

print("Absolute Mean Deviation:", absolute_mean_deviation)

3. NumPy absolute deviation example:

An example showing how to calculate the absolute deviation using NumPy.

import numpy as np

data = np.array([10, 12, 15, 17, 20])

mean = np.mean(data)
absolute_deviation = np.abs(data - mean)

print("Data:", data)
print("Absolute Deviation:", absolute_deviation)

4. Calculate absolute deviation using NumPy:

Calculating the absolute deviation using NumPy's absolute function.

import numpy as np

data = np.array([3, 5, 8, 10, 12])

absolute_deviation = np.absolute(data - np.mean(data))

print("Absolute Deviation:", absolute_deviation)

5. Python NumPy MAD (Mean Absolute Deviation):

Using NumPy to calculate the MAD (Mean Absolute Deviation).

import numpy as np

data = np.array([3, 5, 8, 10, 12])

mad = np.mean(np.absolute(data - np.mean(data)))

print("Mean Absolute Deviation (MAD):", mad)

6. NumPy absolute deviation formula:

Understanding the formula for calculating absolute deviation in NumPy.

import numpy as np

data = np.array([3, 5, 8, 10, 12])

mean = np.mean(data)
absolute_deviation = np.abs(data - mean)

print("Absolute Deviation Formula:", absolute_deviation)

7. NumPy absolute deviation function:

Using NumPy's built-in function for calculating absolute deviation.

import numpy as np

data = np.array([3, 5, 8, 10, 12])

absolute_deviation = np.abs(np.subtract(data, np.mean(data)))

print("Absolute Deviation (Using NumPy function):", absolute_deviation)

8. Absolute deviation and mean absolute deviation with NumPy:

Calculating both absolute deviation and mean absolute deviation using NumPy.

import numpy as np

data = np.array([3, 5, 8, 10, 12])

mean = np.mean(data)
absolute_deviation = np.abs(data - mean)
mean_absolute_deviation = np.mean(absolute_deviation)

print("Absolute Deviation:", absolute_deviation)
print("Mean Absolute Deviation:", mean_absolute_deviation)

9. NumPy absolute difference array:

Using NumPy to create an array of absolute differences.

import numpy as np

array1 = np.array([2, 5, 8, 12])
array2 = np.array([3, 7, 8, 10])

absolute_difference = np.abs(array1 - array2)

print("Array 1:", array1)
print("Array 2:", array2)
print("Absolute Difference Array:", absolute_difference)