<jupyter_start><jupyter_text>**<NAME>**

**<EMAIL>**

  **Unsupervised Machine Learning Using Iris Dataset Task 2** 

> **Importing Libraries**<jupyter_code># Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn import datasets<jupyter_output><empty_output><jupyter_text>> **Load the iris dataset**<jupyter_code># Importing Iris dataset
df = pd.read_csv("/Iris.csv")
df.drop(["Id"],axis=1,inplace=True)
#diplay the fisrt 5 dataset
df.head(10)
df.shape
df.info()
df.describe()<jupyter_output><empty_output><jupyter_text>> **Checking for Null values**<jupyter_code>df.isnull().sum()<jupyter_output><empty_output><jupyter_text>> **Check for datatypes**<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
 #   Column         Non-Null Count  Dtype  
---  ------         --------------  -----  
 0   SepalLengthCm  150 non-null    float64
 1   SepalWidthCm   150 non-null    float64
 2   PetalLengthCm  150 non-null    float64
 3   PetalWidthCm   150 non-null    float64
 4   Species        150 non-null    object 
dtypes: float64(4), object(1)
memory usage: 6.0+ KB<jupyter_text>> **Understanding the distribution of variables vs Species**<jupyter_code>sns.pairplot(data=df,hue="Species",palette="Set1")
plt.show()<jupyter_output><empty_output><jupyter_text>> from above pairplot we can see that iris-sentosa can easily seperable from other two.

 

>**How we can find the optimum number of clusters for K Means and how can we determine the value of K**<jupyter_code># Finding the optimum number of clusters for k-means classification

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.plot(range(1, 11), wcss, color='green',marker='o')
plt.title('The elbow method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS') # Within cluster sum of squares
plt.show()<jupyter_output><empty_output><jupyter_text>> We can clearly see why it is called 'The elbow method' from the above graph, the optimum clusters is where the elbow occurs. This is when the within cluster sum of squares (WCSS) doesn't decrease significantly with every iteration.

From this we choose the number of clusters as 3

 

> **Initialization using K-means++**<jupyter_code># Applying kmeans to the dataset / Creating the kmeans classifier
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<jupyter_output><empty_output><jupyter_text>> **Visualizing the Clusters**<jupyter_code># Visualising the clusters - On the first two columns
plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], 
            s = 100, c = 'red', label = 'Iris-setosa')
plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], 
            s = 100, c = 'blue', label = 'Iris-versicolour')
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1],
            s = 100, c = 'green', label = 'Iris-virginica')

# Plotting the centroids of the clusters
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], 
            s = 100, c = 'yellow', label = 'Centroids')

plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1286549707602339, 'fraction_numerical': 0.02729044834307992, 'mean_word_length': 3.907103825136612, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 45, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '452676', 'license': 'Apache-2.0', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1258, 'n_words': 358, 'path': 'TSFDataScienceInternshipTask2.ipynb', 'repo_name': 'bimalthomas/TSFDataScienceInternshipTask1', 'size': '3615'}