Skip to content

Instantly share code, notes, and snippets.

@sei-dupdyke
Last active September 15, 2021 01:40
Show Gist options
  • Save sei-dupdyke/7f9f85235a00a4cfd0ea762d7b1acb3e to your computer and use it in GitHub Desktop.
Save sei-dupdyke/7f9f85235a00a4cfd0ea762d7b1acb3e to your computer and use it in GitHub Desktop.
import pandas as pd
def determine_optimal_elbow(vals):
elbow_data = pd.DataFrame(columns=['index', 'k', 'delta'])
last = None
i = 0
for x in vals:
if (last is None):
last = x
delta = last - x
row = [i, x, delta]
elbow_data.loc[i] = row
i += 1
last = x
# we want the cluster AFTER the largest delta, unless it is the last cluster in the array
row = elbow_data.loc[elbow_data["delta"].idxmax()]
row_count = 0
if row["index"] < len(elbow_data) - 1:
row = elbow_data.loc[row["index"] + 1]
return row
vals = [9.120197530868204,
6.084063492055672,
2.0175925925981977,
1.3755925925991808]
print(determine_optimal_elbow(vals))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment