Advertisement
tresonance

Polynomial Regression

Feb 13th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 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 PolynomialRegression(object):
  11.     def __init__(self, height, width):  #Poly degre 2
  12.         self.height = height
  13.         self.width = width
  14.         self.X = []
  15.         self.y = []
  16.         self.a, self.b , self.c = None, None, None
  17.  
  18.  
  19.         self.initUI()
  20.  
  21.     def initUI(self):
  22.         self.myCanvas = tk.Canvas(root, bg="#000080", height=500, width=700)
  23.        
  24.  
  25.         #self.draw_point(self.width-5, 67)
  26.         self.myCanvas.bind('<Button-1>', self.draw_point)
  27.         #self.draw()
  28.         self.myCanvas.pack()
  29.        
  30.  
  31.     def draw_point(self, event):
  32.         x = event.x
  33.         y = event.y
  34.         self.myCanvas.create_oval(x, y, x + 8, y + 8, fill="white")
  35.         (self.X).append(x)
  36.         (self.y).append(y)
  37.         self.draw()
  38.    
  39.    
  40.  
  41.     def draw(self):
  42.         self.redraw()
  43.         if(len(self.X) > 3):
  44.             self.prediction()
  45.  
  46.     def linear_coef(self,choice=1):  #1= covariance methode, 2=matrix_methode
  47.         #a = (cov(x,y)/V(x) = (1/N)*sigma(xi,yi) - xbar * ybar)/( cov(x,y)/V(x) = (1/N)*sigma(xi,xi) - xbar * xbar)
  48.         #b = ybar - a * xbar
  49.         xysum, xxsum, xxxsum , xxxxsum, xsum, n = 0,0,0, 0,0, len(self.X)
  50.         X, y = self.X, self.y
  51.         ysum, xxysum = 0, 0
  52.  
  53.         if len(self.X) < 4:
  54.             return None, None, None
  55.        
  56.         for i in range(n):
  57.             xysum += (X[i]*y[i])
  58.  
  59.             xxsum += (X[i] ** 2)
  60.             xxxsum += (X[i] ** 3)
  61.             xxxxsum += (X[i] ** 4)
  62.  
  63.             xsum += X[i]
  64.             ysum += y[i]
  65.             xxysum += y[i] * (X[i] ** 2)
  66.        
  67.        
  68.         matrixLeft = np.array([n, xsum, xxsum,
  69.                               xsum, xxsum, xxxsum,
  70.                                xxsum, xxxsum, xxxxsum
  71.             ]).reshape(3,3)
  72.  
  73.         matrixRight = np.array([ysum, xysum, xxysum]).reshape(3,1)
  74.         resultMatrix = np.matmul(np.linalg.inv(matrixLeft), matrixRight).reshape(1, -1)
  75.            
  76.         resultMatrix = resultMatrix.reshape(1, 3)
  77.         resultMatrix = resultMatrix[0]
  78.        
  79.         a, b, c = resultMatrix[2], resultMatrix[1], resultMatrix[0]
  80.    
  81.         return a,b,c
  82.    
  83.     def prediction(self):
  84.         a, b, c  = self.linear_coef()
  85.  
  86.         if a is None and b is None:
  87.             return
  88.         self.a = a
  89.         self.b = b
  90.         self.c = c
  91.         M = len(self.X)
  92.  
  93.         predict = [((a * (xi ** 2))  + (b * xi) + c) for xi in self.X]
  94.         x0,y0 = self.X[0], predict[0]
  95.         for i in range(1, M):
  96.                 x1, y1 = self.X[i], predict[i]
  97.                 self.myCanvas.create_line(x0, y0, x1,y1, fill="orange")
  98.                 x0, y0 = x1, y1
  99.    
  100.     def redraw(self):
  101.          self.myCanvas.delete("all")
  102.          for i in range(len(self.X)):
  103.              x, y = (self.X)[i], (self.y)[i]
  104.              #regionColor = self.get_regionColor(x, y)
  105.              self.myCanvas.create_oval(x, y, x + 8, y + 8, fill='white', dash=(4,2))
  106.    
  107.     def get_regionColor(self,x,y):
  108.         if (self.a != None) and (self.b != None):
  109.             fx = (self.a * x ) + self.b - y
  110.             if fx > 0:
  111.                 return 'blue'
  112.             return 'green'
  113.         return 'white'
  114.  
  115.        
  116.  
  117.  
  118. if __name__ == '__main__':
  119.     root = tk.Tk()
  120.     sr = PolynomialRegression(2000, 1200)
  121.    
  122.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement