-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.py
68 lines (54 loc) · 1.6 KB
/
canvas.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
from audioop import bias
from dis import dis
from typing import List
import pygame as py
import random as r
from point import Point # type: ignore
from perceptron import Perceptron # type: ignore
from settings import h, w
from utils import f
# SUM = x0 * w0 + x1 * w1
# DIGN(N) if n > 0 -> 1 else -> -1
# Error = answer - guess
display: py.Surface = py.display.set_mode((h, w))
tiempo: py.time.Clock = py.time.Clock()
perceptron: Perceptron = Perceptron()
training_points: List[Point] = []
test_points: List[Point] = []
pos_y: float
pos_x: float
# create the points for train
for x in range(500):
pos_y = r.uniform(-1, 1)
pos_x = r.uniform(-1, 1)
training_points.append(Point((pos_x, pos_y), display, train=True))
# end
# train the perceptron
for pt in training_points:
perceptron.set_inputs((*pt.pos, pt.bias))
perceptron.train(pt.label)
# end
# create test points
for x in range(25):
pos_y = r.uniform(-1, 1)
pos_x = r.uniform(-1, 1)
test_points.append(Point((pos_x, pos_y), display, train=False))
# end
while True:
for event in py.event.get():
if event.type == py.QUIT:
py.quit()
quit()
display.fill((255, 255, 255))
# py.draw.line(display, (0, 0, 0), (0, h), (w, 0))
p1: Point = Point((-1, f(-1)), display)
p2: Point = Point((1, f(1)), display)
py.draw.aaline(display, (0, 0, 0), p1.get_translated_pos(), p2.get_translated_pos())
for p in test_points:
# predict
perceptron.set_inputs((*p.pos, p.bias))
p.label = perceptron.guess()
p.draw()
# end
tiempo.tick(60)
py.display.update()