-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.py
158 lines (129 loc) · 4.68 KB
/
knn.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
import math
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
N = 500
mu1 = np.array([2,5])
mu2 = np.array([8,1])
mu3 = np.array([5,3])
means = [None] * 3
means[0] = mu1
means[1] = mu2
means[2] = mu3
cov1 = np.array([[2,0],[0,2]])
cov2 = np.array([[3,1],[1,3]])
cov3 = np.array([[2,1],[1,2]])
covariances = [None] * 3
covariances[0] = cov1
covariances[1] = cov2
covariances[2] = cov3
class1 = np.random.multivariate_normal(mu1, cov1, N)
class2 = np.random.multivariate_normal(mu2, cov2, N)
class3 = np.random.multivariate_normal(mu3, cov3, N)
data = np.concatenate([class1, class2 , class3 ], axis=0)
color = ['blue','red','green']
def plot_true_2d(x,Mu,sigma):
fig1 = plt.figure()
ax1 = fig1.add_subplot(1,1,1)
ax1.set_xlabel("x1")
ax1.set_ylabel("x2")
fig1.suptitle('2D true density')
f0 = np.linspace(x[:,0].min(),x[:,0].max())
f1 = np.linspace(x[:,1].min(),x[:,1].max())
X, Y = np.meshgrid(f0,f1)
for c in range(3):
def pdf(point):
part1 = 1 / (2* np.pi) * (np.linalg.det(sigma[c])**(1/2))
part2 = (-1/2) * ((point - Mu[c]).T @ (np.linalg.inv(sigma[c]))) @((point-Mu[c]))
return float(part1 * np.exp(part2))
z = np.array([pdf(np.array(ponit)) for ponit in zip(np.ravel(X),np.ravel(Y))])
Z = z.reshape(X.shape)
ax1.contour(X, Y, Z,colors=color[c])
def plot_true_3d(x,Mu,sigma):
fig2 = plt.figure(figsize=(6,6))
ax2 = fig2.add_subplot(projection = '3d')
fig2.suptitle('3D true density')
ax2.view_init(10,-100)
ax2.set_xlabel("x1")
ax2.set_ylabel("x2")
ax2.set_zlabel("P(X)")
f0 = np.linspace(x[:,0].min(),x[:,0].max())
f1 = np.linspace(x[:,1].min(),x[:,1].max())
X, Y = np.meshgrid(f0,f1)
for c in range(3):
def pdf(point):
part1 = 1 / (2* np.pi) * (np.linalg.det(sigma[c])**(1/2))
part2 = (-1/2) * ((point - Mu[c]).T @ (np.linalg.inv(sigma[c]))) @((point-Mu[c]))
return float(part1 * np.exp(part2))
z = np.array([pdf(np.array(ponit)) for ponit in zip(np.ravel(X),np.ravel(Y))])
Z = z.reshape(X.shape)
ax2.contour3D(X, Y, Z,60, colors=color[c])
def area(data):
size = 30
X = []
a = min(data[:,0].min(),data[:,1].min())
b = max(data[:,0].max(),data[:,1].max())
for i in range(data.shape[1]):
x=np.linspace(a,b, size)
X.append(x)
return np.array(X)
def knn(data, k):
X = area(data)
size = [len(X[0]), len(X[1])]
knnpdf = np.zeros(size)
n=len(data)
for i in range(size[0]):
for j in range(size[1]):
x = np.array([X[0][i],X[1][j]])
ds = [np.linalg.norm(x-y) for y in data]
ds.sort()
v = math.pi*ds[k-1]*ds[k-1]
if v == 0:
knnpdf[i,j] = 1
else:
knnpdf[i,j] = k/(n*v)
return X, knnpdf
def plotknn(class1,class2,class3):
k_set = [1,9,99]
fig2d = plt.figure()
fig3d = plt.figure()
fig2d.suptitle('2D KNN')
fig3d.suptitle('3D KNN')
pos=1
for k in k_set :
title = "k = %d" % (k)
print(title,"waiting...")
ax2d = fig2d.add_subplot(1, 3, pos)
ax3d = fig3d.add_subplot(1, 3, pos,projection='3d')
ax3d.view_init(10,-100)
pos = pos + 1
for i in range(0,3):
if(i==0):
X,P = knn(class1, k)
px, py = np.meshgrid(X[0], X[1])
ax2d.contour(px, py, P,colors=color[i])
ax3d.plot_surface(px, py,P,alpha=.3,rstride=1,cstride=1,color=color[i],edgecolor='none')
ax3d.contour3D(px, py,P,60, colors=color[i])
if(i==1):
X,P = knn(class2, k)
px, py = np.meshgrid(X[0], X[1])
ax2d.contour(px, py, P,colors=color[i])
ax3d.plot_surface(px, py,P,alpha=.3,rstride=1,cstride=1,color=color[i],edgecolor='none')
ax3d.contour3D(px, py,P,60, colors=color[i])
if(i==2):
X,P = knn(class3, k)
px, py = np.meshgrid(X[0], X[1])
ax2d.contour(px, py, P,colors=color[i])
ax3d.plot_surface(px, py,P,alpha=.3,rstride=1,cstride=1,color=color[i],edgecolor='none')
ax3d.contour3D(px, py,P,60, colors=color[i])
ax2d.set_xlabel('X1')
ax2d.set_ylabel('X2')
ax2d.set_title(title)
ax3d.set_zlabel('P(X)')
ax3d.set_ylabel('Y')
ax3d.set_xlabel('X')
ax3d.set_title(title)
plot_true_2d(data,means,covariances)
plot_true_3d(data,means,covariances)
plotknn(class1,class2,class3)
plt.show()