<jupyter_start><jupyter_text>NAME: <NAME>  The Sparks Foundation Task2  K-Means Clustering( Unsupervised ML Algorithm)<jupyter_code>#importing libraries
import numpy as  np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets
import seaborn as sns


from sklearn.cluster import KMeans
#loading the dataset
iris=datasets.load_iris()
df=pd.DataFrame(iris.data,columns=iris.feature_names)
df.shape
print(df.head(3))
print(df.tail(3))
df.info()
df.describe()
df.isnull().any()<jupyter_output><empty_output><jupyter_text>Visualisation<jupyter_code>g = sns.PairGrid(df)
g.map_upper(sns.scatterplot,color='purple')
g.map_lower(sns.scatterplot, color='green')
g.map_diag(plt.hist)
x=df.iloc[:,[0,1,2,3]].values
x[0:4]
#labels of the dataset
iris.target_names
s = []

for i in range(1, 11):
    km = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
    km.fit(x)
    s.append(km.inertia_)
    
# `Using 'The elbow' curve method to determine the no.of clusters that can be formed
plt.plot(range(1, 11), s)
plt.title('The elbow method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS') 
plt.show()<jupyter_output><empty_output><jupyter_text>Model Creation<jupyter_code>#From the above curve we can see that there is no much significant change from value 3; so we create 3 clusters 
#  Creating the kmeans classifier
k = KMeans(n_clusters = 3, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
y = k.fit_predict(x)
plt.scatter(x[y == 0, 0], x[y == 0, 1], s = 90, c = 'orange', label = 'setosa')
plt.scatter(x[y == 1, 0], x[y == 1, 1], s = 90, c = 'green', label = 'versicolor')
plt.scatter(x[y == 2, 0], x[y == 2, 1], s = 90, c = 'blue', label = 'virginica')
plt.scatter(k.cluster_centers_[:, 0], k.cluster_centers_[:,1],s = 100, c = 'red', label = 'Centroids')

plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15795034337031166, 'fraction_numerical': 0.027997886951928157, 'mean_word_length': 5.420338983050847, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '518897', 'license': 'MIT', 'n_tokens_mistral': 808, 'n_tokens_neox': 741, 'n_words': 188, 'path': 'Optimum Number of Clusters-IRIS Dataset.ipynb', 'repo_name': 'likitha4/MLTasks', 'size': '1906'}