# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # GRADUATE ROTATIONAL INTERNSHIP PROGRAM (GRIP)

# # THE SPARKS FOUNDATION

# ## Data Science and Business Analytics Internship

# ## Task 2 : Prediction using Unsupervised ML

# ### *From the given IRIS Dataset, predict the optimum number of clusters and represent it visually.*

# #### IRIS Dataset which is available in sklearn library.
# #### *Based on the Data Source : [https://bit.ly/3kXTdox](https://bit.ly/3kXTdox)*

# ### Performed and Demonstrated by : <NAME> | [<EMAIL>](mailto:<EMAIL>)

# ## Importing Required Libraries

# +
import warnings
warnings.filterwarnings('ignore')

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# -

# ## Reading the data

data = pd.read_csv('Iris.csv')
data

# ## Data Pre-Processing

data.shape

data.info()

# +
# Dropping the 'Id' Column

data.drop('Id', axis=1, inplace=True)
data.columns

# +
# Checking for Null and Duplicates

print(data.isnull().sum(), '\n\n-> Number of Duplicated Rows : ', data.duplicated().sum())

# +
# Dropping the Duplicate Rows

data.drop_duplicates(inplace=True)
data.shape[0]  # gives number of rows. Simiarly, data.shape[1] gives the number of columns.

# Now, the number of rows left are 147, and earlier there were 150 rows.

# +
# Checking for any outliers in numeric data

for i in data.columns:
    if data[i].dtype == 'float64':
        plt.figure(figsize=(6,3))
        sns.boxplot(data[i])
        plt.show()

# +
# Treating the outliers present in 'SepalWidthCm' column.

q1, q3 = np.percentile(data['SepalWidthCm'], [25, 75])
iqr = q3 - q1
lower_fence = q1 - (1.5 * iqr)
upper_fence = q3 + (1.5 * iqr)
data['SepalWidthCm'] = data['SepalWidthCm'].apply(
    lambda x : upper_fence if x > upper_fence else lower_fence if x < lower_fence else x
)

sns.boxplot(data['SepalWidthCm'])
# -

# ## Understanding the Data

# +
# Target Class

print(data.Species.value_counts())
sns.countplot(data.Species);
# -

from IPython.display import Image
Image('iris.png')

data.describe()

data.Species.unique()

# +
# Correlation Matrix

data.corr()
# -

plt.figure(figsize=(10, 5))
sns.heatmap(abs(data.corr()), cmap='GnBu', annot = True);

# ## K-means Clustering

# +
from sklearn.cluster import KMeans

SSE = []
for i in range(1, 10):
    kmeans = KMeans(n_clusters = i, init='k-means++')
    kmeans.fit(data.iloc[:, [0, 1, 2, 3]])
    SSE.append(kmeans.inertia_)
    
    
df = pd.DataFrame({'Cluster':range(1, 10), 'SSE':SSE})

plt.figure(figsize=(12, 6))
plt.plot(df['Cluster'], df['SSE'], marker='o')
plt.xlabel('Number of Clusters')
plt.ylabel('Inertia')
plt.title("ELBOW METHOD TO DETERMINE OPTIMAL VALUE OF 'K'\n");
# -

kmeans = KMeans(n_clusters=3, init='k-means++')
kmeans.fit(data.iloc[:, [0, 1, 2, 3]])
kmeans.cluster_centers_

kmeans.labels_

data['cluster'] = kmeans.labels_
data

display(data['cluster'].value_counts(), 
        data['Species'].value_counts()
       )

plt.figure(figsize=(10, 5))
plt.scatter(data['SepalLengthCm'], data['SepalWidthCm'], c=data.cluster)
plt.title('Predicted Clusters\n')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, c='Crimson', label = 'Centroids')
plt.show()

# +
data.loc[data['Species'] == 'Iris-setosa']['cluster'].value_counts() 

# The Model has identified 'iris-setosa' with an accuracy of 100%.
# -

data.loc[data['Species'] == 'Iris-versicolor']['cluster'].value_counts()

data.loc[data['Species'] == 'Iris-virginica']['cluster'].value_counts()

# +
data['Species_encoded'] = data['Species'].apply(
    lambda x: 1 if x == 'Iris-setosa' else 2 if x == 'Iris-virginica' else 0
)

data

# +
from sklearn.metrics import classification_report

print(classification_report(data['Species_encoded'], data['cluster']))

# +
from sklearn.metrics import confusion_matrix

sns.heatmap(confusion_matrix(data['Species_encoded'], data['cluster']),annot = True);

StarcoderdataJupyterScriptsDedupFiltered