Learn practical skills, build real-world projects, and advance your career
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import KMeans
import sklearn.metrics as sm
import pandas as pd
import numpy as np
l1 = [0, 1, 2]
def rename(s):
    l2 = []
    for i in s:
        if i not in l2:
            l2.append(i)
    for i in range(len(s)):
        pos = l2.index(s[i])
        s[i] = l1[pos]
    return s
iris = datasets.load_iris()
x = pd.DataFrame(iris.data)
x.columns = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']
y = pd.DataFrame(iris.target)
y.columns = ['Targets']
model = KMeans(n_clusters=3)
model.fit(x)
plt.figure(figsize=(14, 7))
colormap = np.array(['red', 'lime', 'black'])
plt.subplot(1, 2, 1)
plt.scatter(x.Petal_Length, x.Petal_Width, c=colormap[y.Targets], s=40)
plt.title('Real')
Text(0.5,1,'Real')
Notebook Image
plt.subplot(1,2,2)
plt.scatter(x.Petal_Length, x.Petal_Width, c=colormap[model.labels_], s=40)
plt.title('KMeans')
plt.show()
Notebook Image
km = rename(model.labels_)
print('Accuracy: ', sm.accuracy_score(y, km))
print('Confusion Matrix\n', sm.confusion_matrix(y, km))
Accuracy: 0.8933333333333333 Confusion Matrix [[50 0 0] [ 0 48 2] [ 0 14 36]]