Fastest way to calculate the centroid of a set of coordinate tuples in python without numpy
import numpy as np
data = np.random.randint(0, 10, size=(100000, 2))
this here is fast
def centeroidnp(arr):
length = arr.shape[0]
sum_x = np.sum(arr[:, 0])
sum_y = np.sum(arr[:, 1])
return sum_x/length, sum_y/length
%timeit centeroidnp(data)
10000 loops, best of 3: 181 µs per loop
surprisingly, this is much slower:
%timeit data.mean(axis=0)
1000 loops, best of 3: 1.75 ms per loop
numpy seems very quick to me...
For completeness:
def centeroidpython(data):
x, y = zip(*data)
l = len(x)
return sum(x) / l, sum(y) / l
#take the data conversion out to be fair!
data = list(tuple(i) for i in data)
%timeit centeroidpython(data)
10 loops, best of 3: 57 ms per loop
In Cartesian coordinates, the centroid is just the mean of the components:
data = ((0,0), (1,1), (2,2))
np.mean(data, axis=0)
>>> array([1., 1.])