-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK-Means Clustering.py
56 lines (36 loc) · 1.39 KB
/
K-Means Clustering.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('Mall_Customers.csv')
print(df.shape)
print(df.head())
x = df.iloc[:,[3,4]].values
print(x.shape)
from sklearn.cluster import KMeans
wcss = []
for i in range(1, 11):
km = KMeans(n_clusters = i, init = 'k-means++', max_iter=300, n_init= 10, random_state = 0)
km.fit(x)
wcss.append(km.inertia_)
plt.plot(range(1,11), wcss)
plt.title('Elbow method', fontsize=20)
plt.xlabel('No of Clusters')
plt.ylabel("wcss")
plt.show()
km = KMeans(n_clusters = 5, init = 'k-means++', max_iter=300, n_init= 10, random_state = 0)
y_means = km.fit_predict(x)
plt.rcParams['figure.figsize'] = (15,8)
plt.style.use('fivethirtyeight')
plt.scatter(x[y_means == 0,0],x[y_means == 0,1], s=100 , c='pink', label = 'general')
plt.scatter(x[y_means == 1,0] ,x[y_means == 1,1], s=100 , c='yellow', label ='spendtrift')
plt.scatter(x[y_means == 2,0] ,x[y_means == 2,1], s= 100 ,c='cyan', label ='target')
plt.scatter(x[y_means == 3,0] ,x[y_means == 3,1], s=100, c='magenta' ,label = 'careful')
plt.scatter(x[y_means == 4,0] ,x[y_means == 4,1], s=100 ,c='orange' , label = 'miser')
plt.style.use('fivethirtyeight')
plt.title('K_Means_Clustering',fontsize = 20)
plt.xlabel('Annual Income')
plt.ylabel('Spending Score')
plt.legend()
plt.grid()
plt.show()