Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. import numpy as np
  2. import pandas
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. def main():
  7. df = pandas.read_csv('./Salary.csv')
  8. x = df.iloc[:, :-2].values
  9. y = df.iloc[:, -1].values.reshape(df.shape[0], 1)
  10. lear_and_fit(x, y)
  11.  
  12.  
  13. def lear_and_fit(x, y):
  14. weights = initialize_weights(len(x[0]) + 1, 1)
  15. xNew = insert_coefficient_to_weights_matrix(x)
  16.  
  17.  
  18. # for i in range(700000): # uncomment when weights will be proper updated
  19. predictions = predict(xNew, weights)
  20. gradient_vector = calculate_gradient(predictions, xNew, y)
  21. # print(gradient_vector)
  22. weights = update_weights(gradient_vector, weights, predictions)
  23.  
  24. # plt.scatter(x, y)
  25. # plt.plot(x, predictions)
  26. # plt.show()
  27.  
  28. def update_weights(gradient_vector, weights, predictions):
  29. alpha = 0.00001
  30. d = np.multiply(gradient_vector, alpha)
  31. return np.subtract(weights, d)
  32. # calculate partial derivatives and update weights
  33.  
  34. def calculate_gradient(preconditions, x, y):
  35. residuals = calculate_residual(y, preconditions)
  36. gradient_vector = multiply_residual_by_features_matrix(x.transpose(), residuals)
  37. return gradient_vector * 1 / len(x)
  38.  
  39. def multiply_residual_by_features_matrix(x, residual):
  40. return np.dot(x, residual)
  41.  
  42.  
  43. def calculate_residual(y, predictions):
  44. return np.subtract(predictions, y)
  45.  
  46. def initialize_weights(x, y):
  47. return np.random.rand(x, y)
  48.  
  49.  
  50. def insert_coefficient_to_weights_matrix(matrix):
  51. ones_column = np.ones((len(matrix), 1))
  52. return np.hstack((ones_column, matrix))
  53.  
  54.  
  55. def predict(x, weights):
  56. return np.dot(x, weights)
  57.  
  58.  
  59. if __name__ == "__main__":
  60. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement