-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron_rosenblatt.py
59 lines (45 loc) · 1.86 KB
/
perceptron_rosenblatt.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
class PerceptronRosenblatt:
def __init__(self, learningRate = 0.1, interations = 10):
self.learningRate = learningRate
self.errors = []
self.interations = interations
# LOAD CSV DATASET TO TRAINING
dataFrame = pd.read_csv('training.data')
y = dataFrame.iloc[0:60, 7].values
# MARK WITH -1 ANY LESS MY RUs
self.y = np.where(y=='no', -1, 1)
self.X = dataFrame.iloc[0:60, 0:7].values
# Generate first weights with zeros + first BIAS
self.w = np.zeros(self.X.shape[1]+1)
def calculateWeights(self):
for _ in range(self.interations):
errorCount = 0
for xi, target in zip(self.X, self.y):
predictedValue = self.predict(xi)
wDelta = self.updateWeights(target, predictedValue, xi)
errorCount += int(wDelta != 0.0)
self.errors.append(errorCount)
print("DONE! WEIGHTS:")
for wFinal in zip(self.w):
print(wFinal)
def predict(self, xi):
# SUM (x1 * w1, x2 * w2, xn * wn) + CURRENT_BIAS
netInput = np.dot(xi, self.w[1:])+self.w[0]
predictedValue = np.where(netInput > 0.0, 1, -1)
return predictedValue
def updateWeights(self, target, predictedValue, xi):
# Calculate wDelta (LEARNING_RATE * (TARGET-PREDICTED_VALUE)) * VALUES_INPUT
wDelta = self.learningRate * (target - predictedValue)
self.w[1:] += wDelta * xi
# Update BIAS
self.w[0] += wDelta
return wDelta
def plotErrors(self):
# Plot epochs and all misclassifications
plt.plot(range(1, len(self.errors)+1), self.errors, marker='o', color='black')
plt.xlabel('epochs')
plt.ylabel('misclassifications')
plt.show()