w3resource

NumPy: Calculate average values of two given numpy arrays

NumPy: Array Object Exercise-158 with Solution

Write a NumPy program to calculate average values of two given numpy arrays.

Sample Solution:

Python Code:

import numpy as np
array1 = [[0, 1], [2, 3]]
array2 = [[4, 5], [0, 3]]
print("Original arrays:")
print(array1)
print(array2)
print("Average values of two said numpy arrays:")
result = (np.array(array1) + np.array(array2)) / 2
print(result)

Sample Output:

Original arrays:
[[0, 1], [2, 3]]
[[4, 5], [0, 3]]
Average values of two said numpy arrays:
[[2. 3.]
 [1. 3.]]

Pictorial Presentation:

NumPy: Calculate average values of two given numpy arrays

Python Code Editor:

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

Previous: Write a NumPy program to create a new array which is the average of every consecutive triplet of elements of a given array.
Next: Write a NumPy program to rearrange columns of a given numpy 2D array using given index positions.

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}