Last active
February 16, 2023 00:49
-
-
Save jtemporal/e248bbd974c4cc24a482780fe45c6851 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obrigado pelo exemplo! Pequena correção na linha 9:
numerator = abs((y2-y1)*x0 - (x2-x1)*y0 + x2*y1 - y2*x1)