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 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:
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:
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)
To get the absolute mean deviation, you simply take the average of all the absolute deviations.
Formula: AMD=n1∑i=1n∣xi−m∣
Where:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)