<jupyter_start><jupyter_text>K-Means Clustering<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets
iris = datasets.load_iris()
iris_df = pd.DataFrame(iris.data, columns = iris.feature_names)
iris_df.head()<jupyter_output><empty_output><jupyter_text>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_)
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>The above diagram shows that this is clearly the 'The Elbow Method'.  From this we choose the number of clusters as **'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)
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')

plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], s=100, c = 'yellow', label = 'Centroids')

plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16097875080489377, 'fraction_numerical': 0.03219575016097875, 'mean_word_length': 6.128440366972477, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '154605', 'license': 'Unlicense', 'n_tokens_mistral': 685, 'n_tokens_neox': 627, 'n_words': 133, 'path': 'Task 2.ipynb', 'repo_name': 'vision266/TSF', 'size': '1553'}