<jupyter_start><jupyter_text>**Author : <NAME>**  Task-2 Prediction using Unsupervised ML  [GRIP @ The Spark Foundation](https://www.thesparksfoundationsingapore.org/)  **Objective- In this task I tried to predict the optimum number of clusters and represent it visuallyusing K-means Clustering from the given ‘Iris’ dataset.** Dataset URL:"https://bit.ly/3kXTdox"  Import Libraries<jupyter_code># importing the libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
%matplotlib inline
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
from sklearn import datasets<jupyter_output><empty_output><jupyter_text>Loading the Data<jupyter_code>from google.colab import files
uploaded= files.upload()
# Import Data
iris_df=pd.read_csv('Iris.csv')
print("The Data is Imported")
iris_df<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis<jupyter_code># checking the information of Dataset
iris_df.info()

# Checking shape of Dataset
iris_df.shape<jupyter_output><empty_output><jupyter_text>**We have 150 rows and 6 columns**<jupyter_code># Checking the presence of null values and Missing values
iris_df.isnull().sum()<jupyter_output><empty_output><jupyter_text>**We do not have missing values in this dataset.**<jupyter_code># Checking the Data type of each Attribute
iris_df.dtypes
# Checking the Statistical details of Datasets
iris_df.describe()
# Checking the co-relation b/w the Attributes
iris_df.corr()<jupyter_output><empty_output><jupyter_text>Let's find the optimum number if KMean Clusteering and Determine the value of K<jupyter_code># Finding the optimum number of clusters for k-means classification

x= iris_df.iloc[:, [0,1,2,3]].values

# Within_cluster_sum_of_square (WCSS)
wcss = []

for i in range(1,10):
  kmeans = KMeans(n_clusters=i, init= 'k-means++', n_init=10, max_iter=300, random_state=0)
  kmeans.fit(x)
  wcss.append(kmeans.inertia_)

# Plotting the results onto a line graph, and `allowing us to observe 'The elbow'
plt.figure(figsize=(8,5))
plt.plot(range(1,10), wcss)
plt.title('The elbow Method')
plt.xlabel('Numbers of Cluster')
plt.ylabel('Within Cluster sum of Square')
plt.show<jupyter_output><empty_output><jupyter_text>You 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 the above graph we can see that the elbow curve start at K=3, therefore, we choose the optimum number of clusters as 3.**<jupyter_code># Applying kmeans to the dataset / Creating the kmeans classifier
kmeans = KMeans(n_clusters=3, init= 'k-means++', n_init=10, max_iter=300, random_state=0)

y_kmeans=kmeans.fit_predict(x)<jupyter_output><empty_output><jupyter_text>Visualization of Clusters<jupyter_code># Visualising the clusters - On the first two columns
plt.figure(figsize = (10,5))
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()
plt.xlabel('Sepal Length in cm')
plt.ylabel('Petal Length in cm')
plt.title('K-Means Clustering')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11456582633053221, 'fraction_numerical': 0.017366946778711485, 'mean_word_length': 6.199596774193548, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, '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': '490261', 'license': 'MIT', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1223, 'n_words': 365, 'path': 'Task_2_Clustering.ipynb', 'repo_name': 'chiranjeev9292/The-Spark-Foundation-Tasks-GRIPJUNE', 'size': '3581'}