-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMeans.py
35 lines (25 loc) · 820 Bytes
/
KMeans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
df = pd.read_csv('xclara.csv')
x1 = df['V1']
x2 = df['V2']
range_cluster = range(1, 11)
sse = []
for k in range_cluster:
model = KMeans(n_clusters = k)
model.fit(df)
sse.append(model.inertia_)
# Here we used Elbow method to determine the optimal number of clusters
plt.plot(range_cluster, sse, color = 'pink', linestyle = '--',
lw = 2, marker = '*', markeredgecolor = 'pink',
markerfacecolor = 'cyan', markersize = 12)
plt.xlabel('Number of Clusters')
plt.ylabel('Cost')
plt.show()
# after closed the plot window, enter the number of the clusters manually
k = int(input('\nNumber of Clusters : '))
model = KMeans(n_clusters = k)
model.fit(df)
plt.scatter(x1, x2, c = model.labels_)
plt.show()