Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2.  
  3. u = [-2, 0, 2, 1, -1]
  4. y = [4, 6, 8, 7, 5]
  5.  
  6. N=len(u)
  7.  
  8. def avg(a):
  9.     suma = 0
  10.     for element in a:
  11.         suma+=element
  12.     return suma/len(a)
  13.  
  14. def theta1(u,y):
  15.     u_avg= avg(u)
  16.     y_avg = avg(y)
  17.  
  18.     theta_licznik = 0
  19.     theta_mianownik = 0
  20.  
  21.     for un, yn in zip(u,y):
  22.         theta_licznik+=1/N * ((yn*un)-u_avg*y_avg)
  23.         theta_mianownik+= 1/N * (un**2-u_avg**2)
  24.     return (theta_licznik/theta_mianownik)
  25.  
  26. def theta0(u,y):
  27.     return avg(y) - theta1(u,y)*avg(u)
  28.  
  29. plt.scatter(u,y, s=5, c='red')
  30. t1 = theta1(u,y)
  31. t0 = theta0(u,y)
  32.  
  33. y2=[]
  34. for el in u:
  35.     y2.append(t1*el+t0)
  36.  
  37. u, y2 = (list(x) for x in zip(*sorted(zip(u, y2))))
  38.  
  39. plt.plot(u,y2, linewidth=0.5, c='blue')
  40. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement