<jupyter_start><jupyter_text>CLUSTER PREDICTION MODEL(USING UNSUPERVISED MACHINE LEARNING) ***cluster analysis***Cluster analysis or clustering is the task of grouping a set of objects in such a way that objects in the same group are more similar to each other than to those in other groupsin this model we will predict the optimum number of clusters and represent them visually using sckit-learn's kmean clustering.<jupyter_code>#importing the iris dataset
import pandas as pd
import numpy as np 
from sklearn import datasets
iris=datasets.load_iris()
df=pd.DataFrame(iris.data,columns=iris.feature_names)
df.head()
df.dropna()
df.describe()<jupyter_output><empty_output><jupyter_text>***The elbow method***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>#finding the optimum number of clusters for k-means classification
x=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_)
#ploting the 'ellbow method'
import matplotlib.pyplot as plt
%matplotlib notebook
plt.style.use('seaborn')
plt.figure('The ellbow method')
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()
# 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)
# Visualising the clusters - On the first two columns
%matplotlib notebook
plt.figure()
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>
StarcoderdataJupyterStructuredCleanDedup