image_output_1.png
from sklearn.cluster import KMeans

import matplotlib.pyplot as plt

import pandas as pd

path = '../input/iris/Iris.csv'
data = pd.read_csv(path)

y = data.Species

data.drop(['Id', 'Species'], axis=1, inplace=True)

data.shape

x = data.iloc[:].values

from sklearn.cluster import KMeans
wcss = []
for i in range(1, 11):
    kmeans = KMeans(n_clusters=i, init='k-means++', n_init=10, max_iter=300, random_state=0)
    kmeans.fit(x)
    wcss.append(kmeans.inertia_)

kmeans = KMeans(n_clusters=3, init='k-means++', n_init=10, max_iter=300, random_state=0)
y = kmeans.fit_predict(x)

plt.scatter(x[y == 0, 0], x[y == 0, 1], s=60, c='b', label='Iris-setosa')
plt.scatter(x[y == 1, 0], x[y == 1, 1], s=60, c='r', label='Iris-versicolour')
plt.scatter(x[y == 2, 0], x[y == 2, 1], s=60, c='g', label='Iris-virginica')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=100, c='yellow', label='Centroid')
plt.title('Cluster with centroids')
plt.legend()
plt.show()