Forde Forde -4 years ago 66
Python Question

Average values in two Numpy arrays

Given two ndarrays

old_set = [[0, 1], [4, 5]]
new_set = [[2, 7], [0, 1]]


I'm looking to get the mean of the respective values between the two arrays so that the data ends up something like:

end_data = [[1, 4], [2, 3]]


basically it would apply something like

for i in len(old_set):
end_data[i] = (old_set[i]+new_set[i])/2


But I'm unsure what syntax to use.. Thanks for the help in advance!

Answer Source
>>> import numpy as np
>>> old_set = [[0, 1], [4, 5]]
>>> new_set = [[2, 7], [0, 1]]
>>> (np.array(old_set) + np.array(new_set)) / 2
array([[1, 4],
       [2, 3]])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download