<jupyter_start><jupyter_text>Graduate Rotational Internship Program (GRIP) The Sparks Foundation Name: <NAME> Task 2 : Prediction using Unsupervised MLFrom the given ‘Iris’ dataset, predict the optimum number of clustersand represent it visually.  **1. Reading the given dataset**<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import datasets
sns.set(rc={'figure.figsize':(11.7,8.27)})
iris=pd.read_csv("Iris.csv")<jupyter_output><empty_output><jupyter_text>**2. Analysis of the Data**<jupyter_code>iris
iris.shape
iris.describe()
iris.duplicated().sum()
iris.isna().sum()<jupyter_output><empty_output><jupyter_text>**3. Preparing the data**<jupyter_code>x=iris.iloc[:,1:-1].values
print(x)
wcss=[]
from sklearn.cluster import KMeans
for i in range(1,8):
    k=KMeans(n_clusters=i, init="k-means++", max_iter=300, n_init=10, random_state=0)
    k.fit(x)
    wcss.append(k.inertia_)
    
p=sns.lineplot(x=range(1,8),y=wcss,)
p.set_xlabel("Number of clusters", fontsize=20)
p.set_ylabel("Within cluster sum of squares", fontsize=20)
p.set_title('WCSS vs No of clusters', fontsize=30)
print("The graph is as follows:")<jupyter_output>The graph is as follows:<jupyter_text>**5. Constructing the model**<jupyter_code>k1=KMeans(n_clusters=3, init="k-means++", random_state=0, n_init=10 )
y=k1.fit_predict(x)
y
len(y)<jupyter_output><empty_output><jupyter_text>**6. Visualizing the clusters formed**<jupyter_code>plt.scatter(x=x[y==0, 0], y=x[y==0, 3], s=50 , label="Iris virginica",c="yellow")
plt.scatter(x=x[y==1, 0], y=x[y==1, 3], s=50, label="<NAME>",c="violet")
plt.scatter(x=x[y==2, 0], y=x[y==2, 3], s=50, label="<NAME>", c="red")
plt.scatter(x= k1.cluster_centers_[:,0], y=k1.cluster_centers_[:, 3], s=200, label="Centroids", c="orange")
plt.legend()
plt.xlabel("Sepal Length", fontweight='bold', fontsize=20)
plt.ylabel("Petal Width", fontweight='bold', fontsize=20)
plt.title("Clusters formed using K-means", fontsize=30)<jupyter_output><empty_output><jupyter_text>**7. Predicting the cluster for a new data point**<jupyter_code>sLen=7.2
sWid=3.0
pLen=5.7
pWid=2.0

y1=k1.predict([[sLen,sWid,pLen,pWid]])
col=""
if(y1==0):
    col="blue"
elif(y1==1):
    col="green"
elif(y1==2):
    col="red"

plt.scatter(x=x[y==0, 0], y=x[y==0, 3], s=50 , label="<NAME>",c="yellow")
plt.scatter(x=x[y==1, 0], y=x[y==1, 3], s=50, label="<NAME>",c="violet")
plt.scatter(x=x[y==2, 0], y=x[y==2, 3], s=50, label="<NAME>", c="red")
plt.scatter(x= k1.cluster_centers_[:,0], y=k1.cluster_centers_[:, 3], s=200, label="Centroids", c="orange")

plt.scatter(x=sLen, y=pWid, c=col,s=300)  

plt.legend()
plt.xlabel("Sepal Length", fontweight='bold', fontsize=20)
plt.ylabel("Petal Width", fontweight='bold', fontsize=20)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20021413276231265, 'fraction_numerical': 0.04139900071377588, 'mean_word_length': 7.786833855799373, 'pattern_counts': {'":': 0, '<': 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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '304900', 'license': 'MIT', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1192, 'n_words': 219, 'path': 'GRIP_Task_2.ipynb', 'repo_name': 'adi123-bit/GRIP-Task-2', 'size': '2845'}