<jupyter_start><jupyter_text>Task 2 : Prediction Using Unsupervised Maching Learning Author : <NAME>ship by GRIP @ The Sparks Foundation  Task : From the given 'IRIS' dataset we have to predict the optimal number of clusters and represents it visually  Importing the requried libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.cluster import KMeans
import sklearn.metrics as metrics
import seaborn as sns

from sklearn.preprocessing import StandardScaler
from sklearn.metrics import silhouette_score

from mpl_toolkits.mplot3d import Axes3D
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Reading Data<jupyter_code>data = pd.read_csv("C:\\Users\\sahil\\Downloads\\Iris.csv")
data.head(5)
data["Species"].value_counts()<jupyter_output><empty_output><jupyter_text>Visualization of Data<jupyter_code># Boxplot
fig=plt.gcf()
fig.set_size_inches(10,7)
fig=sns.boxplot(x='Species',y='SepalLengthCm',data=data)
fig=sns.stripplot(x='Species',y='SepalLengthCm',data=data,jitter=True,edgecolor='gray')<jupyter_output><empty_output><jupyter_text>By observing the boxplot , we see that some outliers are present int Iris-Virginica<jupyter_code>tmp = data.drop('Id', axis=1)
tmp.hist(edgecolor='yellow', linewidth=0.2)
fig=plt.gcf()
fig.set_size_inches(15,8)
plt.show()
sns.pairplot(tmp, hue="Species", diag_kind="hist", size=2.9);<jupyter_output><empty_output><jupyter_text>This shows versicolor and virginica are very similar rather than setosa .But there could be features that you didn't measure that would more clearly separate the species.Therefore we need Unsupervised Learning to separate the correctly  Converting Species to Numeric data for data preparation<jupyter_code>d = data.drop(['Species'], axis=1)
y = data['Species']
def y_label (invalue):
    if invalue == 'Iris-setosa' :
        return 1
    elif invalue == 'Iris-virginica' :
        return 0
    else :
        return 2
df1 = pd.DataFrame(data=y.values, columns=['species'])

df1['index']=df1['species'].apply(y_label)
d.dtypes
d
# Data Scaling
std_scale = StandardScaler().fit(d)
data_scaled = std_scale.transform(d)

X_scaled = pd.DataFrame(data_scaled, columns = d.columns)

X_scaled.sample(5)<jupyter_output><empty_output><jupyter_text>K-Means Algorithm<jupyter_code>def plot_kmeans_scale(k) :

    kmeans_model = KMeans(n_clusters=k, random_state=123)
    kmeans_model.fit(data_scaled)
    #Make predictions
    labels=kmeans_model.predict(data_scaled)
    #to get centroids
    centroid=kmeans_model.cluster_centers_
    colors=['r','g','p','b','o','y','m','w']
    fig = plt.figure(1, figsize=(6,6))
    kx = Axes3D(fig, rect=[0, 0, 1, 1], elev=49, azim=135)
    for i in range(k) :
        points=np.array([data_scaled[j]for j in range(len(data_scaled))if labels[j]>=i])
        kx.scatter(points[:, 3], points[:, 0], points[:, 2],s=5, cmap='jet')#colors[i]) 
        kx.scatter(centroid[:,0],centroid[:,1],marker='*',s=200,c='red')
      
        #plt.title('Number of clusters = {}'.format(k))
        plt.show()
k=4
for i in range(k+1):
    if  i>1 :
        plot_kmeans_scale(i)<jupyter_output><empty_output><jupyter_text>Now let us found optimal clusters<jupyter_code>k=9
WSS = []
for k in range(1,9):
        kmeans_model = KMeans(n_clusters=k, random_state=123)
        kmeans_model.fit(data_scaled)
        WSS.append(kmeans_model.inertia_)
plt.plot(range(1,9), WSS, marker='o')
plt.xlabel("Number of clusters")
plt.ylabel("Within-cluster WSS")
plt.title("Scree Plot")
plt.plot([3]*6000, range(1,6001), ",")
plt.text(3.1, 5001, "optimal number of clusters = 3")<jupyter_output><empty_output><jupyter_text>Therefore three  clusters were considered as optimal clusters  K-means clustering with 3 optimal Clusters<jupyter_code>km = KMeans(n_clusters=3, random_state=123)
km.fit(data_scaled)
print('inertia with clusters=3 -->' ,km.inertia_)
km.cluster_centers_<jupyter_output>inertia with clusters=3 --> 177.05608895062977<jupyter_text>Making predictions using K - 3<jupyter_code>predicted_cluster = km.predict(data_scaled)
predicted_labels = km.labels_

fig = plt.figure(1, figsize=(9,9))
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=50, azim=120)
ax.scatter(data_scaled[:, 3], data_scaled[:, 0], data_scaled[:, 2],
          c=predicted_labels.astype(np.float), cmap='jet',edgecolor="k", s=150)
ax.set_xlabel("Petal width")
ax.set_ylabel("Sepal length")
ax.set_zlabel("Petal length")
plt.title("K Means", fontsize=20)<jupyter_output><empty_output><jupyter_text>Now we are getting much separated versicolor and verginica<jupyter_code>def predict_species (invalue):
    if invalue == 1:
        return 'Iris-setosa' 
    elif invalue == 0 :
        return 'Iris-virginica'
    else :
        return 'Iris-versicolor'
df1['predict_label']= pd.DataFrame(data=predicted_labels, columns=['predict_label'])

df1['predict_species']=df1['predict_label'].apply(predict_species)
df1[df1['species']!=df1['predict_species']]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13745839044448796, 'fraction_numerical': 0.025455257489719992, 'mean_word_length': 5.987688098495212, 'pattern_counts': {'":': 0, '<': 41, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '471387', 'license': 'MIT', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1854, 'n_words': 371, 'path': 'Task 2.ipynb', 'repo_name': 'Sahillather002/The-Sparks-Foundation-Tasks', 'size': '5113'}