  @paulfears/

# linear regression tkinter ## quick bit of cs homework

Files
• main.py
• graphics.py
• nohup.out
main.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
```
```'linear_regression.py'
'draw a line of best fit through a bunch of points'

__author__ = 'paul fears'
__date__ = '12/4/2019'
from graphics import *

def main():
print("making sure")
win = GraphWin("data thing",500, 500)
done = Rectangle(Point(0,0), Point(50, 50))
done.draw(win)
text = Text(Point(25,25), "done")
text.draw(win)
points = []

while True:
click = win.getMouse()
if(click.getX()<50 and click.getX()>0):
if(click.getY()<50 and click.getY()>0):
break
points.append(click)
Circle(click, 5).draw(win)
print("done clicked")
ys = list(map(lambda point: point.getY(), points))
xs = list(map(lambda point: point.getX(), points))
length = len(points)
x_avg = sum(xs)/length
y_avg = sum(ys)/length
top_sum = 0
bottom_sum = 0
top_sum = sum([p.getX()*p.getY() for p in points])-(length*x_avg*y_avg)
bottom_sum = sum(map(lambda x: x**2, xs))-(length*x_avg**2)
m = top_sum/bottom_sum
equ = lambda x: y_avg+(m*(x-x_avg))
p1 = Point(0, equ(0))
p2 = Point(500, equ(500))
Line(p1,p2).draw(win)

win.getMouse()

if __name__ == '__main__':
main()

```