km.random_init(data2, 3)

"""
Explanation: 0. random init
for initial centroids
End of explanation
"""


init_centroids = km.random_init(data2, 3)
init_centroids

x = np.array([1, 1])

fig, ax = plt.subplots(figsize=(6,4))
ax.scatter(x=init_centroids[:, 0], y=init_centroids[:, 1])

for i, node in enumerate(init_centroids):
    ax.annotate('{}: ({},{})'.format(i, node[0], node[1]), node)
    
ax.scatter(x[0], x[1], marker='x', s=200)

km._find_your_cluster(x, init_centroids)

"""
Explanation: 1. cluster assignment
http://stackoverflow.com/questions/14432557/matplotlib-scatter-plot-with-different-text-at-each-data-point
find closest cluster experiment
End of explanation
"""


C = km.assign_cluster(data2, init_centroids)
data_with_c = km.combine_data_C(data2, C)
data_with_c.head()

"""
Explanation: 1 epoch cluster assigning
End of explanation
"""


sns.lmplot('X1', 'X2', hue='C', data=data_with_c, fit_reg=False)

"""
Explanation: See the first round clustering result
End of explanation
"""


km.new_centroids(data2, C)

"""
Explanation: 2. calculate new centroid
End of explanation
"""


final_C, final_centroid, _= km._k_means_iter(data2, 3)
data_with_c = km.combine_data_C(data2, final_C)

sns.lmplot('X1', 'X2', hue='C', data=data_with_c, fit_reg=False)

"""
Explanation: putting all together, take1
this is just 1 shot k-means, if the random init pick the bad starting centroids, the final clustering may be very sub-optimal
End of explanation
"""


km.cost(data2, final_centroid, final_C)

"""
Explanation: calculate the cost
End of explanation
"""


best_C, best_centroids, least_cost = km.k_means(data2, 3)

least_cost

data_with_c = km.combine_data_C(data2, best_C)
sns.lmplot('X1', 'X2', hue='C', data=data_with_c, fit_reg=False)

"""
Explanation: k-mean with multiple tries of randome init, pick the best one with least cost
End of explanation
"""


from sklearn.cluster import KMeans

sk_kmeans = KMeans(n_clusters=3)

sk_kmeans.fit(data2)

sk_C = sk_kmeans.predict(data2)

data_with_c = km.combine_data_C(data2, sk_C)
sns.lmplot('X1', 'X2', hue='C', data=data_with_c, fit_reg=False)

"""
Explanation: try sklearn kmeans
End of explanation
"""


