-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKmean Clustering.py
More file actions
58 lines (33 loc) · 986 Bytes
/
Kmean Clustering.py
File metadata and controls
58 lines (33 loc) · 986 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
dataset = pd.read_csv("Mall_Customers.csv")
X = dataset.iloc[:, [3, 4]].values
# In[2]:
import matplotlib.pyplot as plt
plt.scatter(X[:,0], X[:,1], color= "red")
plt.show()
# In[4]:
from sklearn.cluster import KMeans
wcss = []
for i in range(1, 11):
kmeans = KMeans(n_clusters = i, random_state=0)
kmeans.fit(X)
wcss.append(kmeans.inertia_)
plt.plot(wcss)
plt.show()
# In[5]:
kmeans = KMeans(n_clusters = 5, random_state = 0)
y_kmeans = kmeans.fit_predict(X)
# In[8]:
from matplotlib.colors import ListedColormap
raw_colors = ("red", "green", "black", "cyan", "magenta")
colors = ListedColormap(raw_colors)
for i in range(5):
plt.scatter(X[y_kmeans == i,0], X[y_kmeans == i,1],s = 100, c = colors(i))
X_clusters = kmeans.cluster_centers_[:, 0]
Y_clusters = kmeans.cluster_centers_[:, 1]
plt.scatter(X_clusters, Y_clusters, s = 300, c = "yellow")
plt.show()
# In[ ]: