Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. def initialize_weights(X):
  2. return np.random.rand(X.shape[1] + 1, 1)
  3.  
  4.  
  5. def insert_coeficient_to_weights_matrix(X):
  6. return np.insert(X, 0, 1, axis=1)
  7.  
  8.  
  9. def predict(new_X, B):
  10. return np.dot(new_X, B)
  11.  
  12.  
  13. def calculate_gradient(predictions, new_X, Y):
  14. return (1/new_X.shape[0]) * multiply_residual_by_features_matrix(predictions, new_X, Y)
  15.  
  16.  
  17. def calculate_residual(predictions, real_values):
  18. return np.subtract(predictions, real_values)
  19.  
  20.  
  21. def multiply_residual_by_features_matrix(predictions, new_X, Y):
  22. return np.dot(np.transpose(new_X), calculate_residual(predictions, Y))
  23.  
  24.  
  25. def update_weights(gradient_vector, weights):
  26. alfa = 0.0001
  27. new_weights = np.subtract(weights, (alfa * gradient_vector))
  28. return new_weights
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement