<jupyter_start><jupyter_text>-----------------:The Sparks Foundation Internship Programme:----------- Task2 - Prediction using Unsupervised ML (Sparks Foundation internship programme)            --------------------------------- Program written and  Submitted By -  <NAME>  K- Means Clustering :k-means 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. 1) Importing the required Library and Load Iris Dataset      i)Iris Dataset :In this analysis, I begin to explore the infamous iris dataset. This dataset is famous in the machine learning world to help develop skills in machine learning techniques. This dataset can be approached in a number of different ways and this will depend on your objectives when starting out your analysis. I shall highlight the objectives later in the analysis. Let’s just describe the data so we know what we’re dealing with.The dataset consists of 50 samples of three different species of iris flowers (Iris Setosa, Iris Versicolor and Iris Virginica). Five features are captured in the dataset. These are:·         Sepal length (cm)·         Sepal width (cm)·         Petal length (cm)·         Petal width (cm)<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets

# Load the iris dataset
iris = datasets.load_iris()
iris_df = pd.DataFrame(iris.data, columns = iris.feature_names)
iris_df.head() # See the first 5 rows<jupyter_output><empty_output><jupyter_text>2) Finding the optimum number of clusters for K-Means and determining the value of K.<jupyter_code>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, 
# `allowing 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>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 this we choose the number of clusters as 3.<jupyter_code># Applying kmeans to the dataset / Creating the kmeans classifier
kmeans = KMeans(n_clusters = 3, init = 'k-means++',
                max_iter = 300, n_init = 10, random_state = 0)
y_kmeans = kmeans.fit_predict(x)<jupyter_output><empty_output><jupyter_text>3) Visualising Clusters on the First 2 Columns using K means .<jupyter_code>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.0970873786407767, 'fraction_numerical': 0.014438635797859098, 'mean_word_length': 3.9119804400977993, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '514495', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1135, 'n_words': 491, 'path': 'Task2 - Prediction using Unsupervised ML (Sparks Foundation internship programme).ipynb', 'repo_name': '22VINAYAK98/-The-Sparks-Foundation---Graduate-Rotational-Internship-Program-', 'size': '4029'}