-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
353 lines (301 loc) · 10.3 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 12 12:35:48 2018
@author: huwei
"""
#%load_ext watermark
#import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix as sm
import seaborn as sns
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
from matplotlib.patches import Ellipse
from sklearn import preprocessing
from sklearn.decomposition import PCA
from scipy.spatial.distance import cdist
#%watermark
#%watermark -p pandas,numpy,scipy,sklearn,matplotlib,seaborn
# k-means weaknesses that mixture models address directly
# code sourced from:
# http://nbviewer.jupyter.org/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/05.12-Gaussian-Mixtures.ipynb
# Predefined parameters
def plot_kmeans(kmeans, X, n_clusters, rseed=2, ax=None):
dot_size = 50
cmap = 'viridis'
labels = kmeans.fit_predict(X)
# plot input data
#ax = ax or plt.gca() # <-- nice trick
fig, ax = plt.subplots(figsize=(9,7))
ax.axis('equal')
ax.scatter(X[:, 0], X[:, 1],
c=labels, s=dot_size, cmap=cmap, zorder=2)
# plot the representation of Kmeans model
centers = kmeans.cluster_centers_
radii = [cdist(X[labels==i], [center]).max()
for i, center in enumerate(centers)]
for c, r in zip(centers, radii):
ax.add_patch(plt.Circle(c, r, fc='#CCCCCC',edgecolor='slategrey',
lw=4, alpha=0.5, zorder=1))
return
# code sourced from:
# http://nbviewer.jupyter.org/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/05.12-Gaussian-Mixtures.ipynb
def draw_ellipse(position, covariance, ax=None, **kwargs):
"""Draw an ellipse with a given position and covariance"""
# Convert covariance to principal axes
if covariance.shape == (2, 2):
U, s, Vt = np.linalg.svd(covariance)
angle = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
width, height = 2 * np.sqrt(s)
else:
angle = 0
width, height = 2 * np.sqrt(covariance)
# Draw the Ellipse
for nsig in range(1, 4):
ax.add_patch(Ellipse(position, nsig * width, nsig * height,
angle, **kwargs))
def plot_gmm(gmm, X, label=True, ax=None):
dot_size = 50
cmap = 'viridis'
fig, ax = plt.subplots(figsize=(9,7))
ax = ax or plt.gca()
labels = gmm.fit(X).predict(X)
if label:
ax.scatter(X[:, 0], X[:, 1], c=labels, s=dot_size, cmap=cmap, zorder=2)
else:
ax.scatter(X[:, 0], X[:, 1], s=dot_size, zorder=2)
ax.axis('equal')
w_factor = 0.2 / gmm.weights_.max()
for pos, covar, w in zip(gmm.means_, gmm.covariances_, gmm.weights_):
draw_ellipse(pos, covar, ax=ax, alpha=w * w_factor)
'''
path = '/Users/huwei/Dropbox/On_Local/' \
'1.Study/0_CityU_ISP/Semester A 201819/' \
'FB8918 Machine Learning for Business Research/' \
'Project 1'
os.chdir(path)
'''
wine = pd.read_csv('wine.data', \
names = ['Label', \
'Alcohol', \
'Malic acid', \
'Ash', \
'Alcalinity of ash', \
'Magnesium', \
'Total phenols', \
'Flavanoids', \
'Nonflavanoid phenols', \
'Proanthocyanins', \
'Color intensity', \
'Hue', \
'OD280', \
'Proline'])
# True labels
label = wine['Label']
del wine['Label']
# Data description
wine.dtypes
wine.describe()
# Scatter plot
sm(wine, alpha = 0.7, figsize = (18,18))
plt.show()
# Correlation Heatmap
correlation = wine.corr()
plt.subplots(figsize = (9,9))
sns.heatmap(correlation.round(2),
annot = True,
vmax = 1,
square = True,
cmap = 'RdYlGn_r')
plt.show()
# regression
sns.jointplot(x=wine.columns[5],
y=wine.columns[6],
data=wine,
kind="reg");
plt.show()
######################
# Normalize
scaler = preprocessing.StandardScaler()
scaler.fit(wine)
X_scaled_array = scaler.transform(wine)
winenorm = pd.DataFrame(X_scaled_array, columns = wine.columns)
########################################################################
############ K-Means
########################################################################
seed = 0
elbow = dict()
for k in range(1,11):
estimator = KMeans(n_clusters = k,random_state=seed)
res = estimator.fit_predict(winenorm)
inertia = estimator.inertia_
elbow[k] = inertia
elbow_df = pd.Series(elbow)
ax = elbow_df.plot(title = 'Elbow Method')
ax.set_xlabel('Number of clusters')
ax.set_ylabel('Inertia')
plt.plot(3,elbow_df[3],'ro')
#####
# K-Means
KM = KMeans(n_clusters = 3, random_state=seed)
res = KM.fit_predict(winenorm)
label_pred_KM = KM.labels_
print(label_pred_KM)
print('Length of labels is same as data entry', label_pred_KM.shape)
centroids_KM= KM.cluster_centers_
print(centroids_KM.shape)
print(centroids_KM)
inertia_KM = KM.inertia_
print(inertia_KM)
# Pairplot
winenorm['cluster'] = label_pred_KM.astype(str)
sns_plot = sns.pairplot(winenorm, hue = "cluster")
#sns_plot.savefig('pairplot.png')
########################################################################
##### GMM
########################################################################
aic = dict()
bic = dict()
for k in range(1,11):
estimator = GaussianMixture(n_components = k, random_state=seed)
res = estimator.fit(winenorm)
ic1 = estimator.aic(winenorm)
ic2 = estimator.bic(winenorm)
aic[k] = ic1
bic[k] = ic2
aic_df = pd.Series(aic)
bic_df = pd.Series(bic)
temp = {'AIC' : aic_df,
'BIC' : bic_df}
ic_df = pd.DataFrame(temp)
ax = ic_df.plot(title='AIC/BIC of GMM')
ax.set_xlabel('Number of clusters')
ax.set_ylabel('AIC/BIC')
plt.plot(3,aic_df[3],'ro')
plt.plot(3,bic_df[3],'ro')
####################
# GMM
GMM = GaussianMixture(n_components = 3, random_state=seed)
res_GMM = GMM.fit(winenorm)
res_prob_GMM = GMM.predict_proba(winenorm)
np.set_printoptions(formatter={'float_kind':'{:.3f}'.format})
print(res_prob_GMM)
weights_GMM = GMM.weights_
means_GMM = GMM.means_
covariance_GMM = GMM.covariances_
print(type(covariance_GMM))
covariance_GMM.size
label_pred_GMM = GMM.predict(winenorm)
print(label_pred_GMM)
print('Length of labels is same as data entry', label_pred_GMM.shape)
####
winenorm['cluster'] = label_pred_GMM.astype(str)
sns_plot = sns.pairplot(winenorm, hue = "cluster")
########################################################################
###### PCA
########################################################################
pca = PCA(random_state=seed)
pca.fit(winenorm)
winenorm_pca_array = pca.transform(winenorm)
winenorm_pca = pd.DataFrame(winenorm_pca_array)
print(winenorm_pca.head())
var_ratio = pca.explained_variance_ratio_
cum_var_ratio = np.cumsum(var_ratio)
print(var_ratio)
print(cum_var_ratio)
sv = pca.singular_values_
print(sv)
print(sum(var_ratio[0:5]))
# PLOT OUT THE EXPLAINED VARIANCES SUPERIMPOSED
plt.figure(figsize=(10, 5))
plt.bar(range(len(var_ratio)),
var_ratio,
alpha=0.3333,
align='center',
label='individual explained variance',
color = 'g')
plt.step(range(len(cum_var_ratio)),
cum_var_ratio,
where='mid',
label='cumulative explained variance')
plt.ylabel('Explained variance ratio')
plt.xlabel('Principal components')
plt.legend(loc='best')
plt.show()
#################################
# Plot PCA1~PCA2
pca = PCA(n_components=2, random_state=seed)
pca.fit(winenorm)
winenorm_pca_array = pca.transform(winenorm)
winenorm_PCA = pd.DataFrame(winenorm_pca_array)
plt.scatter(x=winenorm_PCA.iloc[:,0],
y=winenorm_PCA.iloc[:,1],
alpha = 0.7)
plt.ylabel('PCA2')
plt.xlabel('PCA1')
plt.show()
#######
KM = KMeans(n_clusters = 3, random_state=seed)
plot_kmeans(KM, winenorm_PCA.as_matrix(),n_clusters=3)
plt.ylabel('PCA2')
plt.xlabel('PCA1')
plt.title('Clusters of K-Means in two PCAs', fontsize=18, fontweight='demi')
###
GMM = GaussianMixture(n_components = 3, random_state=seed)
plot_gmm(GMM, winenorm_PCA.as_matrix())
plt.ylabel('PCA2')
plt.xlabel('PCA1')
plt.title('Clusters of GMM in two PCAs', fontsize=18, fontweight='demi')
##########################################
# we slecte six components
pca = PCA(n_components = 6, random_state=seed)
pca.fit(winenorm)
winenorm_pca_array = pca.transform(winenorm)
winenorm_PCA = pd.DataFrame(winenorm_pca_array)
print(winenorm_pca)
var_ratio = pca.explained_variance_ratio_
print(var_ratio)
sv = pca.singular_values_
print(sv)
print(sum(var_ratio[0:5]))
# K-Means + PCA
KM = KMeans(n_clusters = 3, random_state=seed)
res = KM.fit(winenorm_PCA)
label_pred_KM_PCA = KM.predict(winenorm_PCA)
# GMM + PCA
GMM = GaussianMixture(n_components = 3, random_state=seed)
res_GMM = GMM.fit(winenorm_PCA)
label_pred_GMM_PCA = GMM.predict(winenorm_PCA)
########################################################################
# Evaluation
from sklearn import metrics
sh_score_KM = metrics.silhouette_score(winenorm, label_pred_KM)
print(sh_score_KM)
sh_scores_KM = metrics.silhouette_samples(winenorm, label_pred_KM)
sns.distplot(sh_scores_KM);
sh_score_KM_PCA = metrics.silhouette_score(winenorm, label_pred_KM_PCA)
print(sh_score_KM_PCA)
sh_scores_KM_PCA = metrics.silhouette_samples(winenorm, label_pred_KM_PCA)
sns.distplot(sh_scores_KM_PCA);
sh_score_GMM = metrics.silhouette_score(winenorm, label_pred_GMM)
print(sh_score_GMM)
sh_scores_GMM = metrics.silhouette_samples(winenorm, label_pred_GMM)
sns.distplot(sh_scores_GMM);
sh_score_GMM_PCA = metrics.silhouette_score(winenorm, label_pred_GMM_PCA)
print(sh_score_GMM_PCA)
sh_scores_GMM_PCA = metrics.silhouette_samples(winenorm, label_pred_GMM_PCA)
sns.distplot(sh_scores_GMM_PCA);
###
from sklearn.metrics.cluster import adjusted_rand_score
#
ar_score_KM = adjusted_rand_score(label, label_pred_KM)
print(ar_score_KM)
ar_score_KM_PCA = adjusted_rand_score(label, label_pred_KM_PCA)
print(ar_score_KM_PCA)
ar_score_GMM = adjusted_rand_score(label, label_pred_GMM)
print(ar_score_GMM)
ar_score_GMM_PCA = adjusted_rand_score(label, label_pred_GMM_PCA)
print(ar_score_GMM_PCA)