Advertisement
STANAANDREY

reg trial

Dec 23rd, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. import numpy as np
  2. from numpy.linalg import inv
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. def get_best_param(X, y):
  7. X_transpose = X.T
  8. best_params = inv(X_transpose.dot(X)).dot(X_transpose).dot(y)
  9. # normal equation
  10. # theta_best = (X.T * X)^(-1) * X.T * y
  11.  
  12. return best_params # returns a list
  13.  
  14.  
  15. def generate_data():
  16. X = 2 * np.random.rand(100, 1)
  17. y = 4 + 3 * X + np.random.randn(100, 1)
  18.  
  19. # y = 4 + 3X + Gaussian noise
  20. # theta_0 or Bias Term = 4
  21. # theta_1 = 3
  22.  
  23.  
  24.  
  25. return X, y
  26.  
  27.  
  28. def main():
  29. X, y = generate_data()
  30. plt.plot(X, y, "r.")
  31. plt.ylabel('price (x1000)')
  32. plt.xlabel('size(m^2)')
  33. plt.show()
  34.  
  35.  
  36. # main
  37. if __name__ == "__main__":
  38. main()
  39. print(2 * np.random.rand(3, 3))
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement