'''
lecture24_elbow_fail.py
Illustration of Elbow method failure to detect number of clusters
Oliver W. Layton
CS251: Data Analysis and Visualization
'''
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
import seaborn as sns
import lecture24_hierarchical_clustering as hc

n_samps_per_clust = 200

# 6 clusters, difference variance
a = np.random.multivariate_normal([0, 0], [[3, 1], [1, 4]], size=[n_samps_per_clust, ])
b = np.random.multivariate_normal([20, 10], [[3, 1], [1, 4]], size=[n_samps_per_clust, ])
c = np.random.multivariate_normal([40, 40], [[20, 1], [1, 30]], size=[n_samps_per_clust, ])
d = np.random.multivariate_normal([80, 80], [[30, 1], [1, 30]], size=[n_samps_per_clust, ])
e = np.random.multivariate_normal([100, 10], [[100, 1], [1, 100]], size=[n_samps_per_clust, ])
f = np.random.multivariate_normal([140, 140], [[100, 1], [1, 100]], size=[n_samps_per_clust, ])
xy = np.vstack([a, b, c, d, e, f])
print(xy.shape)
plt.scatter(xy[:,0], xy[:,1])
plt.show()

dists = linkage(xy, 'single')

hc.plot_dendrogram(dists, 20)

# Take last p merges (10). Merge dists are in the 3rd col of dists
p = 10
top_merges = dists[-p:, 2]

# We need to reverse the dist list from low-to-high to hogh-to-low
top_merges = top_merges[::-1]

# Display elbow plot of the last p (10) cluster merges
hc.plot_elbow(top_merges)
