-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron.py
151 lines (107 loc) · 4.65 KB
/
perceptron.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
def perceptron(X, y, max_iterations):
"""
w, b = perceptron(X, y, max_iterations)
Perceptron algorithm.
Implements the perceptron algorithm
(http://en.wikipedia.org/wiki/Perceptron)
:param X: d-dimensional observations, (d, number_of_observations) np array
:param y: labels of the observations (0 or 1), (n,) np array
:param max_iterations: number of algorithm iterations (scalar)
:return w: w - weights, (d,) np array
:return b: b - bias, python float
"""
# https://youtu.be/4Gac5I64LM4?si=Ur4E-cvSn3WVLIjL
z = np.vstack((X,np.ones((1,X.shape[1]))))
z[:,y == 1] = -lz[:,y == 1]
v = np.zeros(X.shape[0] + 1)
w = np.nan
b = np.nan
for i in range(max_iterations):
z_p = v.T @ z
not_sat_zt = z[:,z_p <= 0]
if not_sat_zt.shape[1] == 0:
w = v[:-1]
b = v[-1]
break
upd_zt = not_sat_zt[:,0]
v+= upd_zt
print(v)
return w, b
def lift_dimension(X):
"""
Z = lift_dimension(X)
Lifts the dimensionality of the feature space from 2 to 5 dimensions
:param X: observations in the original space
2-dimensional observations, (2, number_of_observations) np array
:return Z: observations in the lifted feature space, (5, number_of_observations) np array
"""
Z = np.zeros((X.shape[0]+3,X.shape[1]))
Z[:2,:] = X
Z[2,:] = X[0,:] ** 2
Z[3,:] = X[0,:] * X[1,:]
Z[4,:] = X[1,:] ** 2
return Z
def classif_quadrat_perc(tst, model):
"""
K = classif_quadrat_perc(tst, model)
Classifies test samples using the quadratic discriminative function
:param tst: 2-dimensional observations, (2, n) np array
:param model: dictionary with the trained perceptron classifier (parameters of the discriminative function)
model['w'] - weights vector, np array (d, )
model['b'] - bias term, python float
:return: Y - classification result (contains either 0 or 1), (n,) np array
"""
w = model['w']
b = model['b']
X = lift_dimension(tst)
Q = w.T @ X + b
Y = np.where(Q < 0, 1, 0)
return Y
################################################################################
##### #####
##### Below this line are already prepared methods #####
##### #####
################################################################################
def pboundary(X, y, model, figsize=None, style_0='bx', style_1='r+'):
"""
pboundary(X, y, model)
Plot boundaries for perceptron decision strategy
:param X: d-dimensional observations, (d, number_of_observations) np array
:param y: labels of the observations (0 or 1), (n,) np array
:param model: dictionary with the trained perceptron classifier (parameters of the discriminative function)
model['w'] - weights vector, np array (d, )
model['b'] - bias term, python float
"""
plt.figure(figsize=figsize)
plt.plot(X[0, y == 0], X[1, y == 0], style_0, ms=10)
plt.plot(X[0, y == 1], X[1, y == 1], style_1, ms=10)
minx, maxx = plt.xlim()
miny, maxy = plt.ylim()
epsilon = 0.1 * np.maximum(np.abs(maxx - minx), np.abs(maxy - miny))
x_space = np.linspace(minx - epsilon, maxx + epsilon, 1000)
y_space = np.linspace(miny - epsilon, maxy + epsilon, 1000)
x_grid, y_grid = np.meshgrid(x_space, y_space)
x_grid_fl = x_grid.reshape([1, -1])
y_grid_fl = y_grid.reshape([1, -1])
X_grid = np.concatenate([x_grid_fl, y_grid_fl], axis=0)
Y_grid = classif_quadrat_perc(X_grid, model)
Y_grid = Y_grid.reshape([1000, 1000])
blurred_Y_grid = ndimage.gaussian_filter(Y_grid, sigma=0)
plt.contour(x_grid, y_grid, blurred_Y_grid, colors=['black'])
plt.xlim(minx, maxx)
plt.ylim(miny, maxy)
################################################################################
##### #####
##### Below this line you may insert debugging code #####
##### #####
################################################################################
def main():
# HERE IT IS POSSIBLE TO ADD YOUR TESTING OR DEBUGGING CODE
pass
if __name__ == "__main__":
main()