Create an array which is the average of every consecutive subarray of given size using NumPy Last Updated : 02 Sep, 2020 Comments Improve Suggest changes Like Article Like Report 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() functions together. Syntax: numpy.mean(arr, axis = None) Return: Arithmetic mean of the array (a scalar value if axis is none) or array with mean values along specified axis. Syntax: numpy_array.reshape(shape) Return: It returns numpy.ndarray Example : Arr = [1,2,3,4,5,6 7,8,9,10,11 12,13,14,15,16] and K = 2 then Output is [ 1.5, 3.5, 5.5, 7.5, 9.5, 11.5, 13.5, 15.5]. Here, subarray of size k and there average are calculated as : [1 2] avg = ( 1 + 2 ) / 2 = 1.5 [3 4] avg = ( 3 + 4 ) / 2 = 3.5 [5 6] avg = ( 5 + 6 ) / 2 = 5.5 [7 8] avg = ( 7 + 8 ) / 2 = 7.5 [9 10] avg = ( 9 + 10 ) / 2 = 9.5 [11 12] avg = ( 11 + 12 ) / 2 = 11.5 [13 14] avg = ( 13 + 14 ) / 2 = 13.5 [15 16] avg = ( 15 + 16 ) / 2 = 15.5 Below is the implementation: Python3 # importing library import numpy # create numpy array arr = numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) # view array print("Given Array:\n", arr) # declare k k = 2 # find the mean output = numpy.mean(arr.reshape(-1, k), axis=1) # view output print("Output Array:\n", output) Output: Given Array: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] Output Array: [ 1.5 3.5 5.5 7.5 9.5 11.5 13.5 15.5] Comment More infoAdvertise with us Next Article Create an array which is the average of every consecutive subarray of given size using NumPy D deepanshu_rustagi Follow Improve Article Tags : Python Python-numpy Python numpy-program Python numpy-arrayManipulation Python numpy-Statistics Functions +1 More Practice Tags : python Similar Reads Compute the weighted average of a given NumPy array 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 2 min read Calculate the average, variance and standard deviation in Python using NumPy Numpy in Python is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy provides very easy methods to calculate the average, variance 5 min read 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 Average of Each N-length Consecutive Segment in a List - Python The task is to compute the average of each n-length consecutive segment from a given list. For example, if the input list is [1, 2, 3, 4, 5, 6] and n = 2, the output should be [1.5, 2.5, 3.5, 4.5, 5.5]. Let's discuss various ways in this can be achieved.Using List ComprehensionThis method uses list 3 min read Like