-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGMMimages.py
193 lines (140 loc) · 5.84 KB
/
GMMimages.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import os
import numpy as np
from sklearn.metrics import DetCurveDisplay, ConfusionMatrixDisplay, confusion_matrix
from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt
import random
def extract_data(directory):
Xt_class = []
for filename in os.listdir(directory):
with open(os.path.join(directory, filename), 'r') as f:
data = f.readlines()
f.close()
for d in data:
Xt_class.append(np.array(list(map(float,d[:].split( )))))
return np.array(Xt_class)
# In[45]:
Xt = [0]*5
Xt[0] = extract_data(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\coast\train")
Xt[1] = extract_data(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\forest\train")
Xt[2] = extract_data(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\highway\train")
Xt[3] = extract_data(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\mountain\train")
Xt[4] = extract_data(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\opencountry\train")
# In[24]:
import os
import numpy as np
def extract_data_dev(directory):
Xd_class = []
for filename in os.listdir(directory):
with open(os.path.join(directory, filename), 'r') as f:
temp = []
data = f.readlines()
f.close()
for d in data:
temp.append(np.array(list(map(float,d[:].split( )))))
Xd_class.append(np.array(temp))
return (Xd_class)
# In[46]:
Xd = []; numd= []
class_actual = []
Xd_class = extract_data_dev(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\coast\dev")
Xd.extend(Xd_class)
class_actual.extend([1]*len(Xd_class))
numd.append(len(Xd_class))
Xd_class = extract_data_dev(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\forest\dev")
Xd.extend(Xd_class)
class_actual.extend([2]*len(Xd_class))
numd.append(len(Xd_class))
Xd_class = extract_data_dev(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\highway\dev")
Xd.extend(Xd_class)
class_actual.extend([3]*len(Xd_class))
numd.append(len(Xd_class))
Xd_class = extract_data_dev(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\mountain\dev")
Xd.extend(Xd_class)
class_actual.extend([4]*len(Xd_class))
numd.append(len(Xd_class))
Xd_class = extract_data_dev(r"C:\Users\HEMA\OneDrive\Documents\PRML\Assignment_3\Features\opencountry\dev")
Xd.extend(Xd_class)
class_actual.extend([5]*len(Xd_class))
numd.append(len(Xd_class))
Xd = np.array(Xd)
# In[40]:
d = 23
for c in range(5):
for j in range(d):
mini = min(Xt[c][:,j])
maxi = max(Xt[c][:,j])
diff = maxi - mini
Xt[c][:,j] = (Xt[c][:,j]-mini)*100/diff
Xd[:numd[c],:,j] = (Xd[:numd[c],:,j]-mini)*100/diff
# In[11]:
def Kmeans_update(data,K,Niter = 10):
indices = np.random.choice(data.shape[0],K)
means = data[indices,:]
distances = cdist(data,means)
points = np.array([np.argmin(d) for d in distances])
for _ in range(Niter):
means = [sum([data[i] for i in np.where(points==j)[0]])/len(np.where(points==j)[0]) for j in range(K)]
distances = cdist(data,means)
points = np.array([np.argmin(d) for d in distances])
cov = []
pik = []
for j in range(K):
nk = len(np.where(points==j)[0])
cov.append(sum([(data[i].reshape(d,1)-means[j].reshape(d,1))@(data[i].reshape(d,1)-means[j].reshape(d,1)).T for i in np.where(points==j)[0]])/nk)
pik.append(nk/n)
return np.array(means),np.array(cov),pik
# In[12]:
def clusters_update(pik,means,cov):
gamma = np.zeros((n,K))
for i in range(n):
den = 0
for j in range(K):
t1 = (-0.5*(Xt[c][i].reshape(1,d)-means[j].reshape(1,d))@np.linalg.inv(cov[j])@(Xt[c][i].reshape(d,1)-means[j].reshape(d,1)))
den += pik[j]*np.exp(t1)/(np.linalg.det(cov[j]))**0.5
for j in range(K):
num = pik[j]*np.exp(-0.5*(Xt[c][i].reshape(1,d)-means[j].reshape(1,d))@np.linalg.inv(cov[j])@(Xt[c][i].reshape(d,1)-means[j].reshape(d,1)))/(np.linalg.det(cov[j]))**0.5
gamma[i,j] = num/den
nk = [sum([gamma[i,j] for i in range(n)]) for j in range(K)]
temp_means = [sum([gamma[i,j]*Xt[c][i] for i in range(n)])/nk[j] for j in range(K)]
temp_cov = [sum([gamma[i,j]*(Xt[c][i].reshape(d,1)-means[j].reshape(d,1))@(Xt[c][i].reshape(d,1)-means[j].reshape(d,1)).T for i in range(n)])/nk[j] for j in range(K)]
temp_pik = [nk[j]/n for j in range(K)]
print(sum(temp_pik))
return np.array(temp_pik), np.array(temp_means), np.array(temp_cov)
# In[59]:
k = 20; d=23 ; niter =7
means_all = []; cov_all = []; pik_all = []
for c in [0,1,2,3,4]:
n = len(Xt[c])
clusters_means,clusters_cov,clusters_pik = Kmeans_update(Xt[c],K)
for i in range(niter):
clusters_pik,clusters_means,clusters_cov = clusters_update(clusters_pik,clusters_means,clusters_cov)
means_all.append(clusters_means)
cov_all.append(clusters_cov)
pik_all.append(clusters_pik)
# In[65]:
def classification(X,pik,means,cov):
Class = []
b = 36
ans = -1
temp1 = 0
for i in range(len(X)):
for c in range(5):
temp2 = 0
for a in range(b):
temp2 += np.log(sum([pik[c][j]*np.exp(-0.5*(X[i][a]-means[c][j]).T@np.linalg.pinv(cov[c][j])@(X[i][a]-means[c][j]))/(np.linalg.det(cov[c][j]))**0.5 for j in range(K)]))
#print(temp2)
if temp2>temp1:
temp1 = temp2
ans = c
Class.append(ans+1)
temp1 = 0
return np.array(Class)
# In[69]:
Cdev = confusion_matrix(class_actual, class_pre)
ConfusionMatrixDisplay(confusion_matrix=Cdev,display_labels=['Coast','forest','highway','mountain','open country']).plot()
plt.title('Confusion Matrix:(DEV)')
plt.show()