<jupyter_start><jupyter_text>Task 02: K- Means Clustering Author: <NAME> KMeans ClusteringKmeans clustering is a method of vector quantization, originally from signal processing, that aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean (cluster centers or cluster centroid), serving as a prototype of the cluster. This results in a partitioning of the data space into Voronoi cells. It is popular for cluster analysis in data mining. k-means clustering minimizes within-cluster variances (squared Euclidean distances), but not regular Euclidean distances, which would be the more difficult Weber problem: the mean optimizes squared errors, whereas only the geometric median minimizes Euclidean distances. For instance, better Euclidean solutions can be found using k-medians and k-medoids. Advantages and DisadvantagesHere is a list of the main advantages and disadvantages of this algorithm.Advantages:1. Means is simple and computationally efficient.2. It is very intuitive and their results are easy to visualize.Disadvantages:1. K-Means is highly scale dependent and is not suitable for data of varying shapes and densities.2. Evaluating results is more subjective. It requires much more human evaluation than trusted metrics. Problem Statement: Predicting the optimum number of clusters and representing it visually. We are using ‘Iris’ dataset and it is taken from: https://bit.ly/3kXTdox Importing required libraries<jupyter_code># Importing the libraries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import datasets<jupyter_output><empty_output><jupyter_text>Loading the IRIS dataset<jupyter_code># Load the iris dataset
iris_df = pd.read_csv('Iris.csv')
iris_df.head(5)<jupyter_output><empty_output><jupyter_text>Dropping redundant data<jupyter_code># Id is dropped as it is creating confusion with the S.No.

iris_df.drop('Id', axis =1, inplace = True)
iris_df.head()<jupyter_output><empty_output><jupyter_text>Checking for missing data<jupyter_code>iris_df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Finding the optimum number of clusters for K Means & determining 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

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, allows us to observe 'The elbow'
plt.plot(range(1, 11), wcss)
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'.<jupyter_code># Applying kmeans to the dataset / Creating the k-means classifier
kmeans = KMeans(n_clusters = 3, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
y_kmeans = kmeans.fit_predict(x)
# 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.08833922261484099, 'fraction_numerical': 0.014891468955073196, 'mean_word_length': 5.041158536585366, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 1, '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': '582894', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1178, 'n_words': 472, 'path': 'Task 02 - K Means Clustering.ipynb', 'repo_name': 'RupaliShekhawat/TheSparksFoundationInternship', 'size': '3972'}