Skip to content

Instantly share code, notes, and snippets.

@jtemporal
Last active February 16, 2023 00:49
Show Gist options
  • Save jtemporal/e248bbd974c4cc24a482780fe45c6851 to your computer and use it in GitHub Desktop.
Save jtemporal/e248bbd974c4cc24a482780fe45c6851 to your computer and use it in GitHub Desktop.
def optimal_number_of_clusters(wcss):
x1, y1 = 2, wcss[0]
x2, y2 = 20, wcss[len(wcss)-1]
distances = []
for i in range(len(wcss)):
x0 = i+2
y0 = wcss[i]
numerator = abs((y2-y1)*x0 - (x2-x1)*y0 + x2*y1 - y2*x1)
denominator = sqrt((y2 - y1)**2 + (x2 - x1)**2)
distances.append(numerator/denominator)
return distances.index(max(distances)) + 2
@sidleal
Copy link

sidleal commented Jun 25, 2019

Obrigado pelo exemplo! Pequena correção na linha 9: numerator = abs((y2-y1)*x0 - (x2-x1)*y0 + x2*y1 - y2*x1)

@jtemporal
Copy link
Author

valeu @sidleal

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