<jupyter_start><jupyter_text>Prediction using Unsupervised ML  To predict the minimum number of clusters from the 'Iris Dataset', and represent it visually. By <NAME>  _**Dataset : https://bit.ly/3kXTdox**__**Importing the Libraries**_<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets<jupyter_output><empty_output><jupyter_text>_**Loading the 'Iris Dataset'**_<jupyter_code>iris = datasets.load_iris()
iris_df = pd.DataFrame(iris.data, columns = iris.feature_names)
iris_df
iris_df.shape
iris_df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 4 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   sepal length (cm)  150 non-null    float64
 1   sepal width (cm)   150 non-null    float64
 2   petal length (cm)  150 non-null    float64
 3   petal width (cm)   150 non-null    float64
dtypes: float64(4)
memory usage: 4.8 KB<jupyter_text>_**No null values present**_<jupyter_code>iris_df.describe()<jupyter_output><empty_output><jupyter_text>_**Data Visualization**_<jupyter_code>iris_df.hist()
plt.show()<jupyter_output><empty_output><jupyter_text>**Finding optimum number of Clusters for K-Means Classification using Elbow Method**<jupyter_code>from sklearn.cluster import KMeans

x = iris_df.iloc[:, 0:4].values
wcss = []

for i in range(1, 10):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0).fit(x)
    wcss.append(kmeans.inertia_)<jupyter_output><empty_output><jupyter_text>_**Plotting the graph to derive the value of k**_<jupyter_code>plt.plot(range(1, 10), wcss,color='blue',marker='o',markeredgecolor='black')
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('Cluster Sum of Squares') 
print("\n")
plt.show()
print("\n")
plt.plot(range(1, 10), wcss,color='blue',marker='o',markeredgecolor='black')
plt.annotate("Optimum Number",(3,100))
plt.plot(3,80,'r^')
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('Cluster Sum of Squares') 
print("\n")
plt.show()<jupyter_output><jupyter_text>**According to the Elbow Method, the most accurate value for the number of clusters is 3**  **Applying the K-Means Cluster**<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)<jupyter_output><empty_output><jupyter_text>_**Visualising the Clusters**_<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 = 'black', label = 'Centroids')

plt.legend()
print("\n")<jupyter_output>
{'fraction_non_alphanumeric': 0.15649606299212598, 'fraction_numerical': 0.031496062992125984, 'mean_word_length': 5.338877338877339, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '67073', 'license': 'MIT', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1138, 'n_words': 265, 'path': 'Data_Science_&_Business_Analytics_Task_2.ipynb', 'repo_name': 'Candida18/Data-Science-Business-Analytics', 'size': '3057'}