Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def least_square_estimator(X, Y, method = None):
  4. """
  5. Compute beta (minimum cost computation)
  6. --------------------------
  7. (((X^T)X)^-1)((X^T)Y)
  8. --------------------------
  9. X : ndarray, shape(m_number, n_features)
  10.  
  11. Y : ndarray, shape(m_number, )
  12. """
  13. if method is 'QR':
  14. """
  15. ((R^-1)((Q^T)Y)
  16. """
  17. R = np.linalg.qr(X)[1]
  18. return np.dot(np.linalg.inv(R), np.dot(Q.T, Y))
  19.  
  20. elif method is 'Cholesky':
  21. """
  22. ((L(L^T))^-1)((X^T)Y)
  23. """
  24. L = np.linalg.cholesky(np.dot(X.T, X))
  25. return np.dot(np.linalg.inv(np.dot(L, L.T)), np.dot(X.T, Y))
  26.  
  27. elif method is 'SVD':
  28. """
  29. (V(D^-1))((U^T)Y)
  30. """
  31. u, s, vh = np.linalg.svd(X, full_matrices = True)
  32. d = np.diag(s)
  33. u = u[:, :d.shape[0]]
  34. return np.dot(np.dot(np.dot(vh.T, np.linalg.inv(d)), u.T), Y)
  35. return np.dot(np.linalg.inv(np.dot(X.T, X)), np.dot(X.T, Y))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement