I have a list of coordinates like [[1, 2, 2], [1, 2, 1], [1, 1, 1]]
in python, and I want to get the average of them, such as in this case is [1, 1.66666666, 1.333333333]
. However, I can't quite figure out how to do that. I have tried NumPy and basic list manipulation but they all failed to work. Can someone help me? I am using python 3.6
.
I guess this is what you are looking for:
data = [[1, 2, 2], [1, 2, 1], [1, 1, 1]]
average = [sum(x)/len(x) for x in zip(*data)]
print(average)
hope you're working with python 3.x.
You can use NumPy arrays for a column wise average as following. axis=0
computes the average columnwise. You can also use np.mean()
here
data = np.array([[1, 2, 2], [1, 2, 1], [1, 1, 1]] )
averaged = np.average(data, axis=0)
print (averaged)
# [1. 1.66666667 1.33333333]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.