Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import pandas as pandas
  2. import numpy as numpy
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6.  
  7.  
  8. # punkt 4
  9. def initialize_weights():
  10. # print("----------------------initialize_weights----------------------------")
  11. matrix = numpy.random.rand(x.shape[1] + 1, 1)
  12. return matrix
  13.  
  14. # punkt 5
  15. def insert_coeficient_to_weights_matrix(x):
  16. # print("-----------------------insert_coeficient_to_weights_matrix---------------------")
  17. return numpy.insert(x, 0, 1, axis=1)
  18.  
  19. # punkt 6
  20. def predict(x, macierzWspolRegresji):
  21. # print("----------------------------------predict-----------------------------------------")
  22. predictions = numpy.dot(x, macierzWspolRegresji)
  23. return predictions
  24.  
  25. # punkt 7a
  26. def calculate_residual(predictions, df):
  27. # print("-------------------------------calculate_residual---------------------------------------")
  28. residuals = numpy.subtract(predictions, df)
  29. return residuals
  30.  
  31. #punkt 7b
  32. def multiply_residual_by_features_matrix(x, df):
  33. # print("-------------------------------multiply_residual_by_features_matrix----------------------------------------")
  34. gradient_vector = numpy.dot(numpy.transpose(x), calc_residuals(prediction, df))
  35. return gradient_vector
  36.  
  37. #punkt 7
  38. def calculate_gradient(predictions, df, x):
  39. # print("-------------------------------calculate_gradient----------------------------------------")
  40. residuals = calculate_residual(predictions, df)
  41. gradient_vector = multiply_residual_by_features_matrix(x, df)
  42. n = 35
  43. gradient_vector = gradient_vector * 1/n
  44. return gradient_vector
  45.  
  46. #punkt 8
  47. def update_weights(macierzWspolRegresji, gradient_vector):
  48. # print("-------------------------------update_weights----------------------------------------")
  49. alfa = 0.0001
  50. matrix = numpy.subtract(macierzWspolRegresji, (alfa * gradient_vector))
  51. return matrix
  52.  
  53.  
  54. def learn_and_fit(x, y):
  55. # print("-------------------------------learn and fit----------------------------------------")
  56. weights = initialize_weights()
  57. # print(weights)
  58. expanded_x = insert_coeficient_to_weights_matrix(x)
  59. # print(expanded_x)
  60.  
  61. # punkt 9
  62. for i in range(140000):
  63. predictions = predict(expanded_x, weights)
  64. # print(predictions)
  65. gradient_vector = calculate_gradient(predictions, df, expanded_x)
  66. # print(gradient_vector)
  67. weights = update_weights(weights, gradient_vector)
  68. return weights
  69.  
  70.  
  71. # ---------------------------------------------------------------------------------------
  72. df = pandas.read_csv('Salary.csv')
  73. x = df.iloc[:,:-2].values
  74. y = df.iloc[:,-1].values.reshape(df.shape[0], 1)
  75.  
  76. obliczoneWspolRegresji = learn_and_fit(x, y)
  77. x1 = obliczoneWspolRegresji.iloc[:,:-2].values
  78. y1 = obliczoneWspolRegresji.iloc[:,-1].values.reshape(df.shape[0], 1)
  79.  
  80. # punkt 11
  81. plt.plot(range(35), y, color="green")
  82. plt.plot(range(35), y1*100, color="red")
  83. plt.grid(True);
  84. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement