import numpy as np
import pandas as pd
from sklearn.cluster import KMeans 
from sklearn.datasets.samples_generator import make_blobs 
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt

#Using synthetic data
X, y=make_blobs(n_samples=5000,centers=[[4,4], [-2, -1], [2, -3], [1, 1]],cluster_std=0.9)
plt.scatter(X[:, 0],X[:, 1],marker='.')

#K-means model
k_means=KMeans(init = "k-means++", n_clusters = 4, n_init = 12)
k_means.fit(X)

k_means_labels=k_means.labels_
# print(k_means_labels)

#To find the centroids
k_means_cluster_centers = k_means.cluster_centers_
k_means_cluster_centers

# Initialize the plot with the specified dimensions.
fig = plt.figure(figsize=(10, 8))

colors = plt.cm.Spectral(np.linspace(0, 1, len(set(k_means_labels))))

ax = fig.add_subplot(1, 1, 1)
for k, col in zip(range(len([[4,4], [-2, -1], [2, -3], [1, 1]])), colors):
    my_members = (k_means_labels == k)
    
    # Defining centroid
    cluster_center = k_means_cluster_centers[k]
    
    # Ploting data points with colour
    ax.plot(X[my_members, 0], X[my_members, 1], 'w', markerfacecolor=col, marker='.')
    
    # Ploting centroid with an outline
    ax.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,  markeredgecolor='k', markersize=6)

print(plt.show())
