"""
From the given ‘Iris’ dataset, predict the optimum number of clusters and represent it visually.
"""
import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
"""
# Reading the Dataset:
"""
df = pd.DataFrame(load_iris().data, columns = load_iris().feature_names)
df
"""
# EDA
"""
df.head()
df.tail()
df.isnull().sum()
df.shape
df.columns
df.nunique()
df.info()
"""
# ELBOW METHOD
"""
x = df.iloc[:, [0, 1, 2, 3]].values
from sklearn.cluster import KMeans
wcss = []
for i in range(1, 11):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', 
                    max_iter = 300, n_init = 10, random_state = 0)
    kmeans.fit(x)
    wcss.append(kmeans.inertia_)
    
# Plotting the results onto a line graph, 
# `allowing us to observe 'The elbow'
plt.subplots(figsize=(10,5))
plt.plot(range(1, 11), wcss)
plt.title('The Elbow Method', color="RED",size=20)
plt.xlabel('Number of clusters', color="#FDD017",size=15)
plt.ylabel('WCSS', color="#FDD017",size=15) # Within cluster sum of squares
plt.show()
print(wcss)
kmeans = KMeans(n_clusters = 3, init = 'k-means++',
                max_iter = 300, n_init = 10, random_state = 0)
y_kmeans = kmeans.fit_predict(x)
y_kmeans
"""
# Visualization:
"""
plt.subplots(figsize=(10,5))
plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100, c = '#FF2400', label = 'Iris-setosa')
plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c = '#306EFF', label = 'Iris-versicolour')
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c = '#FBB917', label = 'Iris-virginica')
# Plotting the centroids of the clusters
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], s = 200, c = '#FF00FF',marker = '*', label = 'Centroids')
_ = plt.legend()