Professional Documents
Culture Documents
Import libraries
In [3]:
1 import numpy as np
2 import pandas as pd
3 import matplotlib.pyplot as plt
4 import seaborn as sns
5 from sklearn.model_selection import train_test_split
6 from sklearn.cluster import KMeans
In [4]:
1 data = pd.read_csv('iris.csv')
2 data.head(6)
Out[4]:
In [5]:
Out[5]:
In [6]:
1 kmeans5 = KMeans(n_clusters=5)
2 y_kmeans5 = kmeans5.fit_predict(X)
3 print(y_kmeans5)
4
5 kmeans5.cluster_centers_
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 4 3 3 3 4 3 4 4 3 4 3 4 3 3 4 3 4 3 4 3 3
3 3 3 3 3 4 4 4 4 3 4 3 3 3 4 4 4 3 4 4 4 4 4 3 4 4 0 3 2 0 0 2 4 2 0 2 0
0 0 3 0 0 0 2 2 3 0 3 2 3 0 2 3 3 0 2 2 2 0 3 3 2 0 0 3 0 0 0 3 0 0 0 3 0
0 3]
Out[6]:
In [13]:
1 Error = []
2 for i in range(1, 11):
3 kmeans = KMeans(n_clusters=i).fit(X)
4 kmeans.fit(X)
5 Error.append(kmeans.inertia_)
6
7 import matplotlib.pyplot as plt
8
9 plt.grid()
10 plt.plot(range(1, 11), Error, 'r')
11 plt.plot(range(1, 11), Error, 'o')
12 plt.title('Elbow method')
13 plt.xlabel('No of clusters')
14 plt.ylabel('Error')
15 plt.show()
In [8]:
1 kmeans3 = KMeans(n_clusters=3)
2 y_kmeans3 = kmeans3.fit_predict(X)
3 print(y_kmeans3)
4
5 kmeans3.cluster_centers_
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 2 2 2 0 2 2 2 2
2 2 0 0 2 2 2 2 0 2 0 2 0 2 2 0 0 2 2 2 2 2 0 2 2 2 2 0 2 2 2 0 2 2 2 0 2
2 0]
Out[8]:
Visualizing Clustering
In [9]:
Out[9]:
In [10]:
Out[10]: