Skip to content

Instantly share code, notes, and snippets.

@caffeine-potent
Last active July 30, 2021 02:40
Show Gist options
  • Save caffeine-potent/e885a11333be566f33f9a5d2a0c1f2e8 to your computer and use it in GitHub Desktop.
Save caffeine-potent/e885a11333be566f33f9a5d2a0c1f2e8 to your computer and use it in GitHub Desktop.
Geometric Median in one line of native python.
from scipy.spatial.distance import euclidean
points = [
[1,3],
[2,4],
[3,3],
[4,5],
[3,7],
[5,1],
[5,3],
[6,2]
]
distance = euclidean
geometric_mediod = min(map(lambda p1:(p1,sum(map(lambda p2:distance(p1,p2),points))),points), key = lambda x:x[1])[0]
@nunocesarsa
Copy link

Hey! that "geometric median in one line of native python" made me think it was the geometric mean but its actually the medoid isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment