Advertisement
tresonance

Simple Linear Regression

Feb 11th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import tkinter as tk
  5.  
  6. ##################################################################################
  7. #                                  MATH PHYSIC CODE: 12-02-2020
  8. ##################################################################################
  9.  
  10. class SimpleRegression(object):
  11.     def __init__(self, height, width):
  12.         self.height = height
  13.         self.width = width
  14.         self.X = []
  15.         self.y = []
  16.         self.a, self.b = None, None
  17.        
  18.  
  19.  
  20.         self.initUI()
  21.  
  22.     def initUI(self):
  23.         self.myCanvas = tk.Canvas(root, bg="black", height=500, width=700)
  24.        
  25.  
  26.         #self.draw_point(self.width-5, 67)
  27.         self.myCanvas.bind('<Button-1>', self.draw_point)
  28.         #self.draw()
  29.         self.myCanvas.pack()
  30.        
  31.  
  32.     def draw_point(self, event):
  33.         x = event.x
  34.         y = event.y
  35.         self.myCanvas.create_oval(x, y, x + 8, y + 8, fill="white")
  36.         (self.X).append(x)
  37.         (self.y).append(y)
  38.         self.draw()
  39.    
  40.    
  41.  
  42.     def draw(self):
  43.         self.redraw()
  44.         if(2 == len(self.X)):
  45.             x0, y0, x1, y1 = (self.X)[0], (self.y)[0], (self.X)[1], (self.y)[1]
  46.            
  47.             self.myCanvas.create_line(x0,y0,x1, y1, fill="red")
  48.         else:
  49.             self.prediction()
  50.  
  51.     def linear_coef(self):
  52.         xysum, xxsum, xsum, ysum, M = 0,0,0,0, len(self.X)
  53.         X, y = self.X, self.y
  54.  
  55.         for i in range(M):
  56.             xysum += (X[i]*y[i])
  57.             xxsum += (X[i] ** 2)
  58.             xsum += X[i]
  59.             ysum += y[i]
  60.         xbar, ybar = xsum/ M, ysum/M
  61.         a = ((0.5/M)*xysum - xbar*ybar)/((0.5/M)*xxsum - xbar * xbar)
  62.         b = ybar - a * xbar
  63.         return a,b
  64.    
  65.     def prediction(self):
  66.         a, b  = self.linear_coef()
  67.         self.a = a
  68.         self.b = b
  69.         M = len(self.X)
  70.  
  71.         predict = [(a * xi + b) for xi in self.X]
  72.         x0,y0 = self.X[0], predict[0]
  73.         for i in range(1, M):
  74.                 x1, y1 = self.X[i], predict[i]
  75.                 self.myCanvas.create_line(x0, y0, x1,y1, fill="red")
  76.                 x0, y0 = x1, y1
  77.  
  78.     def redraw(self):
  79.          self.myCanvas.delete("all")
  80.          for i in range(len(self.X)):
  81.              x, y = (self.X)[i], (self.y)[i]
  82.              regionColor = self.get_regionColor(x, y)
  83.              self.myCanvas.create_oval(x, y, x + 8, y + 8, fill=regionColor)
  84.    
  85.     def get_regionColor(self,x,y):
  86.         if (self.a != None) and (self.b != None):
  87.             fx = (self.a * x ) + self.b - y
  88.             if fx > 0:
  89.                 return 'blue'
  90.             return 'green'
  91.         return 'white'
  92.  
  93.        
  94.  
  95.  
  96. if __name__ == '__main__':
  97.     root = tk.Tk()
  98.     sr = SimpleRegression(2000, 1200)
  99.    
  100.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement