w3resource

NumPy: Calculate the arithmetic means of corresponding elements of two given arrays of same size

NumPy: Array Object Exercise-202 with Solution

Write a NumPy program to calculate the arithmetic means of corresponding elements of two given arrays of same size.

Sample Solution:

Python Code:

import numpy as np 
nums1 = np.array([[2, 5, 2],
              [1, 5, 5]])
nums2 = np.array([[5, 3, 4],
              [3, 2, 5]])
print("Array1:") 
print(nums1)
print("Array2:") 
print(nums2)
print("\nArithmetic means of corresponding elements of said two arrays:")
print(np.divide(np.add(nums1, nums2), 2))

Sample Output:

Array1:
[[2 5 2]
 [1 5 5]]
Array2:
[[5 3 4]
 [3 2 5]]

Arithmetic means of corresponding elements of said two arrays:
[[3.5 4.  3. ]
 [2.  3.5 5. ]]

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 90x30 array filled with random point numbers, increase the number of items (10 edge elements) shown by the print statement.
Next: Write a NumPy program to create a 11x3 array filled with student information (id, class and name) and shuffle the said array rows starting from 3rd to 9th.

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}