Skip to content

Perceptron

Arevalo-Edgar14 edited this page Sep 2, 2021 · 2 revisions

Weights explicit update

self.w = self.w + np.multiply((self.learning_rate * error), xi) 

Error Calculation

# test method
def Pw(self, x):
    # x dot product weights
    xw = np.dot(x, self.w)
    return self.activation(xw)

def error(self, x, i):
    return self.y[i] - self.Pw(x)

def activation(self, xw):
    if xw >= 0:
        return 1
    return -1 if self.bipolar else 0

Calculate points to draw the line

def calculate_points_to_draw_line(self):
    x1 = np.array([self.bounds[0] - 2, self.bounds[1] + 2])
    m = -self.w[1] / self.w[2]
    b = self.w[0] / self.w[2]
    x2 = m * x1 + b

Initialize weights

self.x = np.array(self.x_)
self.y = np.array(self.y_)
# -3 is the right click value, set corresponding valued as -1 if is
# bipolar or 0 if not
self.y[self.y == 3] = -1 if self.bipolar else 0
# rand values from -bound to bound of x size for this example 2 + 1 (
# +1 because W0)
# x.shape(points number, coords) that's why we use x.shape[1] to just
# set coords elements number as weights, example x, y, just 2 weights
self.w = np.random.uniform(self.bounds[0], self.bounds[1],
                                   self.x.shape[1] + 1)

Add x0 input

Always add x0 = -1 because w0 = Theta = threshold need it as input place it inside the test or inside the epoch to avoid x.size > y.size y[N] don't exits

xy = np.insert(xy, 0, -1.0)

Clone this wiki locally