﻿ NumPy: Average of every consecutive triplet of elements of a given array - w3resource # NumPy: Average of every consecutive triplet of elements of a given array

## NumPy: Array Object Exercise-157 with Solution

Write a NumPy program to create a new array which is the average of every consecutive triplet of elements of a given array.

Sample Solution:

Python Code:

``````import numpy as np
arr1 = np.array([1,2,3, 2,4,6, 1,2,12, 0,-12,6])
print("Original array:")
print(arr1)
result = np.mean(arr1.reshape(-1, 3), axis=1)
print("Average of every consecutive triplet of elements of the said array:")
print(result)
```
```

Sample Output:

```Original array:
[  1   2   3   2   4   6   1   2  12   0 -12   6]
Average of every consecutive triplet of elements of the said array:
[ 2.  4.  5. -2.]
```

Pictorial Presentation: Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Set comprehension:

```>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}
```