<jupyter_start><jupyter_text>Author : <NAME> 2 :Prediction using Unsupervised Machine Learning GRIP @ The Sparks FoundationIn this K-means clustering task, I tried to predict the optimum number of clusters and represented it visually from the given ‘Iris’ dataset.  Technical Stack : Scikit Learn, Numpy Array, Pandas, Matplotlib<jupyter_code>import numpy as np 
import pandas as pd
from sklearn import datasets
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Step 1. Loading the Iris Data<jupyter_code>iris = datasets.load_iris()
iris_df = pd.DataFrame(iris.data, columns = iris.feature_names)
iris_df
print(iris.target_names)
print(iris.target)<jupyter_output>[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]<jupyter_text>Step 2. Visualizing the input data<jupyter_code># Finding the optimum number of clusters for k-means classification
X = iris_df.iloc[:, [0, 1, 2, 3]].values
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')
plt.show()<jupyter_output><empty_output><jupyter_text>As you can see, the optimal value of k is between 2 and 4, as the elbow-like shape is formed at k=3 in the above graph  Step 3 - Implement k-means clustering with k=3<jupyter_code>kmeans = KMeans(n_clusters = 3, init = 'k-means++',max_iter = 300, n_init = 10, random_state = 0)
y_kmeans = kmeans.fit_predict(X)
print(y_kmeans)
kmeans.cluster_centers_<jupyter_output><empty_output><jupyter_text>Step 4 - Visualizing Clustering<jupyter_code># Visualising the clusters - On the first two columns
plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 100, c = 'orange', label = 'Iris-setosa')
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 100, c = 'grey', label = 'Iris-versicolour')
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1],s = 100, c = 'yellow', label = 'Iris-virginica')

# Plotting the centroids of the clusters
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], 
            s = 100, c = 'blue', label = 'Centroids')
plt.title("K-Means")
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11102886750555144, 'fraction_numerical': 0.076980014803849, 'mean_word_length': 3.9414990859232177, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '128930', 'license': 'MIT', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1022, 'n_words': 395, 'path': 'Task2-Clustering.ipynb', 'repo_name': '0angelin0/The_Sparks_Foundation_GRIP', 'size': '2715'}