<jupyter_start><jupyter_text><NAME> Task 2 - Prediction using Unsupervised ML Problem statement - From the given 'Iris' dataset, predict the optimum number of                                                      clusters and represent it visually.<jupyter_code>#Python Implementation of K-means Clustering Algorithm

#The steps to be followed for the implementation are given below:

#Data Pre-processing
#Finding the optimal number of clusters using the elbow method
#Training the K-means algorithm on the training dataset
#Visualizing the clusters<jupyter_output><empty_output><jupyter_text>Step-1: Data pre-processing Step<jupyter_code>#We are Importing libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import KMeans
import sklearn.metrics as sm
#Loading iris datasets
iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns = iris.feature_names)#Importing the Dataset using feature names
df_target = pd.DataFrame(iris.target, columns = ['Target'])
print(df)
#Taget values
print(iris.target_names)
df_target.head()#displays the first five lines
# Data is divided into attributes
x = df.iloc[:, [0, 1, 2, 3]].values  # x is independent variable
y = df.iloc[:, 0].values #y is dependent variable
print(x)
print(y)<jupyter_output>[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]
 [5.4 3.9 1.7 0.4]
 [4.6 3.4 1.4 0.3]
 [5.  3.4 1.5 0.2]
 [4.4 2.9 1.4 0.2]
 [4.9 3.1 1.5 0.1]
 [5.4 3.7 1.5 0.2]
 [4.8 3.4 1.6 0.2]
 [4.8 3.  1.4 0.1]
 [4.3 3.  1.1 0.1]
 [5.8 4.  1.2 0.2]
 [5.7 4.4 1.5 0.4]
 [5.4 3.9 1.3 0.4]
 [5.1 3.5 1.4 0.3]
 [5.7 3.8 1.7 0.3]
 [5.1 3.8 1.5 0.3]
 [5.4 3.4 1.7 0.2]
 [5.1 3.7 1.5 0.4]
 [4.6 3.6 1.  0.2]
 [5.1 3.3 1.7 0.5]
 [4.8 3.4 1.9 0.2]
 [5.  3.  1.6 0.2]
 [5.  3.4 1.6 0.4]
 [5.2 3.5 1.5 0.2]
 [5.2 3.4 1.4 0.2]
 [4.7 3.2 1.6 0.2]
 [4.8 3.1 1.6 0.2]
 [5.4 3.4 1.5 0.4]
 [5.2 4.1 1.5 0.1]
 [5.5 4.2 1.4 0.2]
 [4.9 3.1 1.5 0.2]
 [5.  3.2 1.2 0.2]
 [5.5 3.5 1.3 0.2]
 [4.9 3.6 1.4 0.1]
 [4.4 3.  1.3 0.2]
 [5.1 3.4 1.5 0.2]
 [5.  3.5 1.3 0.3]
 [4.5 2.3 1.3 0.3]
 [4.4 3.2 1.3 0.2]
 [5.  3.5 1.6 0.6]
 [5.1 3.8 1.9 0.4]
 [4.8 3.  1.4 0.3]
 [5.1 3.8 1.6 0.2]
 [4.6 3.2 1.4 0.2]
 [5.3 3.7 1.5 0.2]
 [5.  3.3 1.4 0.2]
 [7.  3.2 4.7 1.4]
 [6.4 3.2 4.5 1.5]
 [6.9 3.1 4.[...]<jupyter_text>Step-2: Finding the optimal number of clusters using the elbow method<jupyter_code># Finding the optimum number of clusters for k-means classification

x = df.iloc[:, [0, 1, 2, 3]].values
wcss = []#Initializing the list for the values of WCSS
#Using for loop for iterations from 1 to 10.  

for i in range(1, 11):
  KM = KMeans(n_clusters= i, max_iter = 300)
  KM.fit(x)

#calculate square error
wcss.append(KM.inertia_)

#plot wcss against k mean values
# Plotting the results onto a line graph, 
# `allowing us to observe 'The elbow'
plt.plot(range(1, 11), wcss, color = 'g', linewidth ='2' )
plt.title('Elbow method')
plt.xlabel("value of K")
plt.ylabel("wcss")# Within cluster sum of squares
plt.show()
#We can 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_output><empty_output><jupyter_text>Step- 3: Training the K-means algorithm on the training dataset<jupyter_code>#training the K-means model on a 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>Step-4: Visualizing the Clusters<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 = 'red', label = 'Iris-setosa')
plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1],s = 100, c = 'blue', label = 'Iris-versicolor')
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1],s = 100, c = 'green', label = 'Iris-virginica')

#plotting centroids of clusters
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1],s = 100, c = 'yellow', label = 'Centroids')
plt.legend()
#Evalute performance of model
iris_k_mean = KMeans(n_clusters=3)
iris_k_mean.fit(x)
predict_y = np.choose(iris_k_mean.labels_ , [1,0,2]).astype(np.int64)
print("Accuracy:",sm.accuracy_score(predict_y, df_target['Target']))<jupyter_output>Accuracy: 0.8933333333333333
{'fraction_non_alphanumeric': 0.15745245466607696, 'fraction_numerical': 0.10791685095090668, 'mean_word_length': 3.2389878163074037, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '629706', 'license': 'MIT', 'n_tokens_mistral': 2215, 'n_tokens_neox': 1954, 'n_words': 600, 'path': 'Task 2.ipynb', 'repo_name': 'ShashankVSonar/Data-Science-and-Business-Analytics-The-Sparks-Foundation', 'size': '4533'}