Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- u = [-2, 0, 2, 1, -1]
- y = [4, 6, 8, 7, 5]
- N=len(u)
- def avg(a):
- suma = 0
- for element in a:
- suma+=element
- return suma/len(a)
- def theta1(u,y):
- u_avg= avg(u)
- y_avg = avg(y)
- theta_licznik = 0
- theta_mianownik = 0
- for un, yn in zip(u,y):
- theta_licznik+=1/N * ((yn*un)-u_avg*y_avg)
- theta_mianownik+= 1/N * (un**2-u_avg**2)
- return (theta_licznik/theta_mianownik)
- def theta0(u,y):
- return avg(y) - theta1(u,y)*avg(u)
- plt.scatter(u,y, s=5, c='red')
- t1 = theta1(u,y)
- t0 = theta0(u,y)
- y2=[]
- for el in u:
- y2.append(t1*el+t0)
- u, y2 = (list(x) for x in zip(*sorted(zip(u, y2))))
- plt.plot(u,y2, linewidth=0.5, c='blue')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement