-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceptronModel.py
53 lines (40 loc) · 2.04 KB
/
perceptronModel.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
class Perceptron:
def __init__(self, learning_rate = 0.1, num_inputs = 4):
self.learning_rate = 0.1
self.weights = [0 for _ in range(num_inputs)]
self.bias = 0
def activation(self, input_vector):
return 1 if sum([w * v for w, v in zip(self.weights, input_vector)]) + self.bias > 0 else -1
def train(self, data, epochs = 10):
for _ in range(epochs):
for dp in data:
prediction = self.activation(dp[:4])
loss = dp[4] - prediction
self.weights = [w + self.learning_rate * loss * v for w, v in zip(self.weights, dp[:4])]
self.bias += self.learning_rate * loss
def main():
with open("iris.csv", "r") as f:
data = f.readlines()
data = [d.replace("Iris-setosa", "-1").replace("Iris-versicolor", "1") for d in data]
data = [d.strip().split(",") for d in data]
data = [[float(v) for v in d] for d in data]
perceptron = Perceptron()
test_set = data[45:50] + data[95:100]
training_set = data[:45] + data[50:95]
perceptron.train(training_set)
print(f"Trained Weights: {perceptron.weights}")
print("Test Set: ")
for dp in test_set:
prediction = perceptron.activation(dp[:4])
print(f"Predicted: {prediction} | Actual: {dp[4]}")
import matplotlib.pyplot as plt
plt.scatter([d[1] for d in data[:50]], [d[2] for d in data[:50]], color="red", label="Iris-setosa")
plt.scatter([d[1] for d in data[50:100]], [d[2] for d in data[50:100]], color="blue", label="Iris-versicolor")
plt.xlabel("Sepal Width")
plt.ylabel("Petal Length")
slope = (-(perceptron.bias)/(perceptron.weights[2])) / ((perceptron.bias)/(perceptron.weights[1]))
plt.plot([2, 5], [slope * w + (-perceptron.bias / perceptron.weights[2]) for w in (2, 5)], label='Decision Boundary', color='black')
plt.legend()
plt.show()
if __name__ == "__main__":
main()