<jupyter_start><jupyter_text>GRIP @ The Sparks Foundation - Data Science and Business Analytics  Task 2: Prediction using Unsupervised Machine Learning Author: <NAME>  ObjectiveFrom the given "Iris" dataset, predict the optimum number of clusters (i.e K) and represent it visually.  Importing all the required libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn import datasets
from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>Loading Dataset<jupyter_code>iris = datasets.load_iris()
iris = pd.DataFrame(iris.data, columns = iris.feature_names)
iris<jupyter_output><empty_output><jupyter_text>Feature Exploration<jupyter_code>iris.shape
iris.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 4 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   sepal length (cm)  150 non-null    float64
 1   sepal width (cm)   150 non-null    float64
 2   petal length (cm)  150 non-null    float64
 3   petal width (cm)   150 non-null    float64
dtypes: float64(4)
memory usage: 4.8 KB<jupyter_text>From the above output, we can see that there is no missing values in the dataset.<jupyter_code>iris.describe()
## Plotting scatter plot between Sepal length and Sepal width

plt.figure(dpi=80)
plt.style.use("default")
plt.scatter(iris.iloc[:,0], iris.iloc[:,1], color = "firebrick", alpha = 0.8)
plt.title("Sepal Length Vs Sepal Width")
plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.show()
## Plotting scatter plot between Petal length and Petal width

plt.figure(dpi=80)
plt.style.use("default")
plt.scatter(iris.iloc[:,2], iris.iloc[:,3], color = "teal", alpha = 0.8)
plt.title("Petal Length Vs Petal Width")
plt.xlabel("Petal Length")
plt.ylabel("Petal Width")
plt.show()
## Converting attributs into 2D-array

iris = iris.iloc[:,0:4].values<jupyter_output><empty_output><jupyter_text>Determining the Optimum number of clusters<jupyter_code>## Finding the optimum number of clusters

wcss = [] #assingning an empty list for "within-cluster sum of square"

for i in range(1,16):
    km = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300,
               n_init = 10, random_state = 0)
    km.fit(iris)
    wcss.append(km.inertia_)
## Plotting the elbow graph

plt.figure(dpi=80)
plt.plot(range(1, 16), wcss, color = "orangered")
plt.title('The elbow Graph')
plt.xlabel('Number of Cluster(K)')
plt.ylabel('WCSS') 
plt.show()<jupyter_output><empty_output><jupyter_text>From the above graph, we get to know that the elbow occurs at k=3. Since, the WCSS does not decreases significanty from 3. Therefore, we can conclude that the optimum number of clusters is "3".  Building the Classifier model and Scoring<jupyter_code>## Building the classifier model with K=3

km = KMeans(n_clusters = 3, init = "k-means++", max_iter = 300,
            n_init = 10, random_state = 0)
y_km = km.fit_predict(iris)
## Prediction class(labels) for each obseravtions in iris dataset

y_km<jupyter_output><empty_output><jupyter_text>Visualizing the Clusters<jupyter_code>## Visualizing the clusters on the Sepal length and Sepal width

plt.figure(dpi=90)
plt.style.use("ggplot")
plt.scatter(iris[y_km == 1, 0], iris[y_km == 1, 1], 
            s = 30, c = 'orchid', label = 'Iris-setosa', alpha = 0.8)
plt.scatter(iris[y_km == 2, 0], iris[y_km == 2, 1],
            s = 30, c = 'orange', label = 'Iris-versicolour', alpha = 0.8)
plt.scatter(iris[y_km == 0, 0], iris[y_km == 0, 1], 
            s = 30, c = 'turquoise', label = 'Iris-virginica', alpha = 0.8)
plt.xlabel("Sepal length")
plt.ylabel("Sepal width")
plt.title("Cluster based on Sepal length & width")


## Plotting the centroids of the clusters

plt.scatter(km.cluster_centers_[:, 0], km.cluster_centers_[:,1], 
            s = 30, c = 'black', label = 'Centroids')
plt.legend(prop={"size":10})
plt.show()
## Visualizing the clusters on the Petal length and Petal width

plt.figure(dpi=90)
plt.style.use("ggplot")
plt.scatter(iris[y_km == 1, 2], iris[y_km == 1, 3], 
            s = 30, c = 'orchid', label = 'Iris-setosa', alpha = 0.8)
plt.scatter(iris[y_km == 2, 2], iris[y_km == 2, 3],
            s = 30, c = 'orange', label = 'Iris-versicolour', alpha = 0.8)
plt.scatter(iris[y_km == 0, 2], iris[y_km == 0, 3], 
            s = 30, c = 'turquoise', label = 'Iris-virginica', alpha = 0.8)
plt.xlabel("Petal length")
plt.ylabel("Petal width")
plt.title("Cluster based on Petal length & width")


## Plotting the centroids of the clusters

plt.scatter(km.cluster_centers_[:, 2], km.cluster_centers_[:,3], 
            s = 30, c = 'black', label = 'Centroids')
plt.legend(prop={"size":11})
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1404494382022472, 'fraction_numerical': 0.029546400332917187, 'mean_word_length': 4.191144708423326, 'pattern_counts': {'":': 2, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '270308', 'license': 'MIT', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1724, 'n_words': 485, 'path': 'Task #2 Prediction using Unsupervised ML.ipynb', 'repo_name': 'danieljosephraj/TSF-Internship', 'size': '4819'}