-
Notifications
You must be signed in to change notification settings - Fork 0
/
project3_code.py
252 lines (184 loc) · 5.85 KB
/
project3_code.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
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 29 01:38:49 2017
@author: rajaz
"""
import numpy as np
from matplotlib.pylab import (figure, bar,hist,plot, semilogx, imshow, xticks, yticks, loglog, xlabel, ylabel, legend, title, clim, subplot, show, hold, plot, contour, contourf, cm, colorbar, legend,ylim)
import os
import xlrd
from sklearn.mixture import GaussianMixture
from sklearn import cross_validation
from scipy.stats.kde import gaussian_kde
from sklearn.neighbors import NearestNeighbors
os.chdir("C:\\Users\\rajaz\\OneDrive\\Documents\\Machine\\project_data\\")
doc = xlrd.open_workbook(".\\glass_data.xls").sheet_by_index(0)
attributeNames = doc.row_values(0, 1, 10)
# Extract class names to python list,
# then encode with integers (dict)
classLabels = doc.col_values(10, 1, 214)
classNames = sorted(set(classLabels))
classDict = dict(zip(classNames, range(6)))
y = np.array([classDict[value] for value in classLabels])
y = y.reshape(213,)
y = y.astype(float)
# Preallocate memory, then extract excel data to matrix X
X = np.mat(np.empty((213, 9)))
for i, col_id in enumerate(range(1, 10)):
X[:, i] = np.mat(doc.col_values(col_id, 1, 214)).T
X = np.asarray(X)
X = X-X.mean(axis=0)
# Compute values of N (number of observations), M (number of attributes) and C (classes).
N = len(y)
M = len(attributeNames)
C = len(classNames)
C = C
#%%
from scipy.linalg import svd
U, s, V = svd(X)
k1 = V.T[:, 0]
k2 = V.T[:, 1]
W2 = V.T[:, :2]
X2D = np.dot(X,W2)
Q = X2D
i = 0
j = 1
f = figure()
f.hold()
title('Glass Data PCA')
#Z = array(Z)
for p in range(C):
# select indices belonging to class c:
class_mask = y.ravel()==p
plot(Q[class_mask,i], Q[class_mask,j], 'o')
legend(classNames)
xlabel('PC{0}'.format(i+1))
ylabel('PC{0}'.format(j+1))
#%%
from toolbox_02450 import clusterplot, clusterval
from sklearn.cluster import k_means
# Number of clusters:
K = 5
# K-means clustering:
centroids, cls, inertia = k_means(X,K)
# Plot results:
figure(figsize=(14,9))
clusterplot(X, cls, centroids, y)
show()
# Maximum number of clusters:
K = 10
# Allocate variables:
Rand = np.zeros((K,))
Jaccard = np.zeros((K,))
NMI = np.zeros((K,))
for k in range(K):
# run K-means clustering:
#cls = Pycluster.kcluster(X,k+1)[0]
centroids, cls, inertia = k_means(X,k+1)
# compute cluster validities:
Rand[k], Jaccard[k], NMI[k] = clusterval(y,cls)
# Plot results:
figure(1)
title('Cluster validity')
plot(np.arange(K)+1, Rand)
plot(np.arange(K)+1, Jaccard)
plot(np.arange(K)+1, NMI)
ylim(-2,1.1)
legend(['Rand', 'Jaccard', 'NMI'], loc=4)
show()
from scipy.cluster.hierarchy import linkage, fcluster, dendrogram
Method = 'single'
Metric = 'euclidean'
Z = linkage(X, method=Method, metric=Metric)
# Compute and display clusters by thresholding the dendrogram
Maxclust = 4
cls = fcluster(Z, criterion='maxclust', t=Maxclust)
figure(1)
clusterplot(X, cls.reshape(cls.shape[0],1), y=y)
# Display dendrogram
max_display_levels=6
figure(2,figsize=(10,4))
dendrogram(Z, truncate_mode='level', p=max_display_levels)
show()
#%% Day 11
# Number of clusters
K = 10
cov_type = 'diag'
# type of covariance, you can try out 'diag' as well
reps = 1
# number of fits with different initalizations, best result will be kept
# Fit Gaussian mixture model
gmm = GaussianMixture(n_components=K, covariance_type=cov_type, n_init=reps).fit(X)
cls = gmm.predict(X)
# extract cluster labels
cds = gmm.means_
# extract cluster centroids (means of gaussians)
covs = gmm.covariances_
# extract cluster shapes (covariances of gaussians)
if cov_type == 'diag':
new_covs = np.zeros([K,M,M])
count = 0
for elem in covs:
temp_m = np.zeros([M,M])
for i in range(len(elem)):
temp_m[i][i] = elem[i]
new_covs[count] = temp_m
count += 1
covs = new_covs
# Plot results:
#figure(figsize=(14,9))
#clusterplot(X, clusterid=cls, centroids=cds, y=y, covars=covs)
#show()
## In case the number of features != 2, then a subset of features most be plotted instead.
figure(figsize=(14,9))
idx = [0,1] # feature index, choose two features to use as x and y axis in the plot
clusterplot(X[:,idx], clusterid=cls, centroids=cds[:,idx], y=y, covars=covs[:,idx,:][:,:,idx])
show()
# Range of K's to try
KRange = range(1,11)
T = len(KRange)
covar_type = 'full' # you can try out 'diag' as well
reps = 3 # number of fits with different initalizations, best result will be kept
# Allocate variables
BIC = np.zeros((T,))
AIC = np.zeros((T,))
CVE = np.zeros((T,))
# K-fold crossvalidation
CV = cross_validation.KFold(N,10,shuffle=True)
for t,K in enumerate(KRange):
print('Fitting model for K={0}\n'.format(K))
# Fit Gaussian mixture model
gmm = GaussianMixture(n_components=K, covariance_type=covar_type, n_init=reps).fit(X)
# Get BIC and AIC
BIC[t,] = gmm.bic(X)
AIC[t,] = gmm.aic(X)
# For each crossvalidation fold
for train_index, test_index in CV:
# extract training and test set for current CV fold
X_train = X[train_index]
X_test = X[test_index]
# Fit Gaussian mixture model to X_train
gmm = GaussianMixture(n_components=K, covariance_type=covar_type, n_init=reps).fit(X_train)
# compute negative log likelihood of X_test
CVE[t] += -gmm.score_samples(X_test).sum()
# Plot results
figure(1);
plot(KRange, BIC,'-*b')
plot(KRange, AIC,'-xr')
plot(KRange, 2*CVE,'-ok')
legend(['BIC', 'AIC', 'Crossvalidation'])
xlabel('K')
show()
x = np.linspace(-10, 10, 50)
# Compute kernel density estimate
kde = gaussian_kde(X.ravel())
xe = np.linspace(-10, 10, 100)
# Plot kernel density estimate
figure(figsize=(6,7))
subplot(2,1,1)
hist(X,x)
title('Data histogram')
subplot(2,1,2)
plot(xe, kde.evaluate(xe))
title('Kernel density estimate')
show()