-
Notifications
You must be signed in to change notification settings - Fork 0
/
Customer segmentation (1).py
95 lines (46 loc) · 1.3 KB
/
Customer segmentation (1).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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# coding: utf-8
# In[4]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# In[5]:
df =pd.read_csv("Mall_Customers.csv")
df.head(10)
# In[6]:
df.shape
# In[7]:
df.info()
# In[8]:
X=df.iloc[:,[3,4]].values
# In[9]:
from sklearn.cluster import KMeans
wcss=[]
# In[21]:
for i in range(1,11):
kmeans=KMeans(n_clusters= i,init='k-means++',random_state=0)
kmeans.fit(X)
wcss.append(kmeans.inertia_)
# In[22]:
plt.plot(range(1,11),wcss)
plt.title("The elobow method")
plt.xlabel("No. of clusters")
plt.ylabel('WCSS Values')
plt.show()
# In[45]:
kmeanmodel=KMeans(n_clusters = 5, init ='k-means++',random_state=0)
# In[46]:
y_kmeans=kmeanmodel.fit_predict(X)
# In[48]:
plt.scatter(X[y_kmeans==0,0],X[y_kmeans==0,1], s=80, c="red",label="Customer 1")
plt.scatter(X[y_kmeans==1,0],X[y_kmeans==1,1], s=80, c="blue",label="Customer 2")
plt.scatter(X[y_kmeans==2,0],X[y_kmeans==2,1], s=80, c="yellow",label="Customer 3")
plt.scatter(X[y_kmeans==3,0],X[y_kmeans==3,1], s=80, c="cyan",label="Customer 4")
plt.scatter(X[y_kmeans==4,0],X[y_kmeans==4,1], s=80, c="black",label="Customer 5")
plt.title('Clusters of customers')
plt.xlabel("Annual Income")
plt.ylabel('Spending Score (1-100)')
plt.legend()
plt.show()
# In[ ]: