Compute the weighted average of a given NumPy array Last Updated : 29 Aug, 2020 Comments Improve Suggest changes Like Article Like Report In NumPy, we can compute the weighted of a given array by two approaches first approaches is with the help of numpy.average() function in which we pass the weight array in the parameter. And the second approach is by the mathematical computation first we divide the weight array sum from weight array then multiply with the given array to compute the sum of that array. Method 1: Using numpy.average() method Example 1: Python import numpy as np # Original array array = np.arange(5) print(array) weights = np.arange(10, 15) print(weights) # Weighted average of the given array res1 = np.average(array, weights=weights) print(res1) Output: [0 1 2 3 4] [10 11 12 13 14] 2.1666666666666665 Example 2: Python import numpy as np # Original array array = np.arange(2, 7) print(array) weights = np.arange(2, 7) print(weights) # Weighted average of the given array res1 = np.average(array, weights=weights) print(res1) Output: [2 3 4 5 6] [2 3 4 5 6] 4.5 Method 2: Using mathematical operation Example 1: Python import numpy as np # Original array array = np.arange(2, 7) print(array) weights = np.arange(2, 7) print(weights) # Weighted average of the given array res2 = (array*(weights/weights.sum())).sum() print(res2) Output: [2 3 4 5 6] [2 3 4 5 6] 4.5 Example 2: Python import numpy as np # Original array array = np.arange(5) print(array) weights = np.arange(10, 15) print(weights) # Weighted average of the given array res2 = (array*(weights/weights.sum())).sum() print(res2) Output: [0 1 2 3 4] [10 11 12 13 14] 2.166666666666667 Comment More infoAdvertise with us Next Article Compute the weighted average of a given NumPy array V vipinyadav15799 Follow Improve Article Tags : Python Python-numpy Python numpy-Mathematical Function Practice Tags : python Similar Reads Calculate average values of two given NumPy arrays Finding average of NumPy arrays is quite similar to finding average of given numbers. We just have to get the sum of corresponding array elements and then divide that sum with the total number of arrays. Let's see an example: Example 1: Calculate average values of two given NumPy 1d-arrays Python3 # 1 min read Averaging over every N elements of a Numpy Array In this article, we will learn how to find the average over every n element of a NumPy array. For doing our task, we will some inbuilt methods provided by NumPy module which are as follows: numpy.average() to calculate the average i.e the sum of all the numbers divided by the number of elementsnumpy 3 min read Compute the median of the flattened NumPy array In this article, we will discuss how to compute the median of the flattened array. Median is basically that value that separates the lower half of the array with the higher half of array. Example: If there are odd numbers in an array. A = [1,2,3,4,5,6,7] Then the median element will be 7+1/2= 4th el 2 min read Compute the covariance matrix of two given NumPy arrays In NumPy for computing the covariance matrix of two given arrays with help of numpy.cov(). In this, we will pass the two arrays and it will return the covariance matrix of two given arrays. Syntax: numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None) Example 1: Pyth 2 min read Create an array which is the average of every consecutive subarray of given size using NumPy In this article, we will see the program for creating an array of elements in which every element is the average of every consecutive subarrays of size k of a given numpy array of size n such that k is a factor of n i.e. (n%k==0). This task can be done by using numpy.mean() and numpy.reshape() funct 2 min read Like