-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNLP Assignment 3 clustering 1.py
199 lines (92 loc) · 2.76 KB
/
NLP Assignment 3 clustering 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
# coding: utf-8
# In[148]:
# supress warnings
import warnings
warnings.filterwarnings('ignore')
# Importing all required packages
import numpy as np
import pandas as pd
# Data viz lib
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
from matplotlib.pyplot import xticks
# In[149]:
bank = pd.read_csv('D://bankmarketing.csv')
# In[150]:
bank.head()
# In[151]:
bank.columns
# In[152]:
# Importing Categorical Columns
bank_cust = bank[['age','job', 'marital', 'education', 'default', 'housing', 'loan','contact','month','day_of_week','poutcome']]
# In[153]:
bank_cust.head()
# In[154]:
# Converting age into categorical variable
bank_cust['age_category'] = pd.cut(bank_cust['age'], [0, 30, 40, 50, 60, 70, 80, 90, 100],
labels=['0-30', '30-40', '40-50','50-60','60-70','70-80', '80-90','90-100'])
bank_cust = bank_cust.drop('age',axis = 1)
bank_cust.head()
# In[155]:
bank_cust.shape
# In[156]:
bank_cust.describe()
# In[157]:
bank_cust.info()
# In[158]:
# Checking Null values
bank_cust.isnull().sum()*100/bank_cust.shape[0]
# There are no NULL values in the dataset, hence it is clean.
# In[159]:
# First we will keep a copy of data
bank_cust_copy = bank_cust.copy()
# In[160]:
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
bank_cust = bank_cust.apply(le.fit_transform)
bank_cust.head()
# In[161]:
pip install kmodes
# In[162]:
# Importing Libraries
from kmodes.kmodes import KModes
# In[163]:
km_cao = KModes(n_clusters=2, init = "Cao", n_init = 1, verbose=1)
fitClusters_cao = km_cao.fit_predict(bank_cust)
# In[164]:
# Predicted Clusters
fitClusters_cao
# In[165]:
clusterCentroidsDf = pd.DataFrame(km_cao.cluster_centroids_)
clusterCentroidsDf.columns = bank_cust.columns
# Mode of the clusters
clusterCentroidsDf
# In[166]:
import pandas as pd
df = pd.read_csv("D:\\Mall_Customers.csv")
print(df.head())
# In[170]:
#initialize an instance of the GaussianMixture class
from sklearn.mixture import GaussianMixture
#inputs = age and spending score
X = df[['Age', 'Spending Score (1-100)']].copy()
#considering three clusters and fit the model to inputs (age and spending score):
n_clusters = 4
gmm_model = GaussianMixture(n_components=n_clusters)
gmm_model.fit(X)
#cluster lables
cluster_labels = gmm_model.predict(X)
X = pd.DataFrame(X)
X['cluster'] = cluster_labels
#plot each cluster within a for-loop
for k in range(0,n_clusters):
data = X[X["cluster"]==k]
plt.scatter(data["Age"],data["Spending Score (1-100)"])
#format out plot
plt.title("Clusters Identified by Guassian Mixture Model")
plt.ylabel("Spending Score (1-100)")
plt.xlabel("Age")
plt.show()
# In[ ]: