Anupznk

OnlineRegression

Jul 2nd, 2021
14
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import math
  3. from matplotlib import pyplot as plt
  4.  
  5.  
  6. def showGraphScatter(x, y):
  7. plt.scatter(x, y, color='red')
  8. plt.show()
  9.  
  10.  
  11. def polynomialFunc(x, constants):
  12. funcVal = constants[0]
  13. constants = np.array(constants)
  14. for i in range(1, constants.size):
  15. funcVal += constants[i] * x
  16. x *= x
  17.  
  18. return funcVal
  19.  
  20.  
  21. def showGraph(lowerLim, upperLim, constants, xarr, yarr):
  22. x = np.arange(lowerLim, upperLim, .1)
  23. y = []
  24. for i in range(x.size):
  25. y.append(polynomialFunc(x[i], constants))
  26. plt.plot(x, y, color='blue')
  27. # plt.ylim(0, 0.4)
  28. plt.xlabel('x')
  29. plt.ylabel('y')
  30. plt.scatter(xarr, yarr, color='red')
  31. plt.grid(True, which='both')
  32. plt.axhline(y=0, color='green')
  33. plt.axvline(x=0, color='green')
  34. plt.show()
  35.  
  36.  
  37. def showGraphExp(lowerLim, upperLim, a_0, a_1, xarr, yarr):
  38. x = np.arange(lowerLim, upperLim, .1)
  39. y = []
  40. for i in range(x.size):
  41. y.append(expFunc(x[i], a_0, a_1))
  42. plt.plot(x, y, color='blue')
  43. plt.ylim(0, 1)
  44. plt.xlabel('x')
  45. plt.ylabel('y')
  46. plt.scatter(xarr, yarr, color='red')
  47. plt.grid(True, which='both')
  48. plt.axhline(y=0, color='green')
  49. plt.axvline(x=0, color='green')
  50. plt.show()
  51.  
  52.  
  53. def gaussianElimination(A, B, d=0):
  54. # FORWARD ELIMINATION
  55. i = 0
  56. n = B.size
  57. while i < n:
  58. j = i + 1
  59. while j < n:
  60. # RATIO = R2/R1
  61. r = A[j][i] / A[i][i]
  62.  
  63. k = 0
  64. while k < n:
  65. # (R2 = R2 - R1 * r)
  66. A[j][k] -= r * A[i][k]
  67.  
  68. k += 1
  69.  
  70. B[j] -= r * B[i]
  71. if d:
  72. print('Coefficient Matrix:')
  73. print(A)
  74. print('Constant Matrix:')
  75. print(B)
  76.  
  77. j += 1
  78.  
  79. i += 1
  80.  
  81. # BACK SUBSTITUTION
  82. solution = np.zeros(n)
  83. # CALCULATING THE VALUE OF THE LAST VARIABLE
  84. solution[n - 1] = B[n - 1] / A[n - 1][n - 1]
  85.  
  86. # FINDING THE REST OF THE SOLUTIONS USING THE PREVIOUS SOLUTIONS
  87. i = n - 2
  88. while i >= 0:
  89. solution[i] = B[i]
  90. j = i + 1
  91. while j < n:
  92. solution[i] = solution[i] - A[i][j] * solution[j]
  93. j += 1
  94. solution[i] /= A[i][i]
  95. i -= 1
  96.  
  97. return solution
  98.  
  99.  
  100. def polynomialRegression(x, y, numOfDataPoints, degree):
  101. # degree = 1 for linear regression
  102. x = np.array(x)
  103. y = np.array(y)
  104. x_exp_sums = np.zeros(2 * degree + 1)
  105. x_exp_y_sums = np.zeros(2 * degree + 1)
  106. x_exp_sums[0] = numOfDataPoints
  107. x_exp_y_sums[0] = sum(y)
  108.  
  109. power_x = np.ones(len(x))
  110. for i in range(1, 2 * degree + 1):
  111. power_x = power_x * x
  112. for j in range(numOfDataPoints):
  113. x_exp_sums[i] += power_x[j]
  114. x_exp_y_sums[i] += y[j] * power_x[j]
  115.  
  116. matA = np.zeros((degree + 1, degree + 1))
  117. matB = np.zeros(degree + 1)
  118.  
  119. for i in range(degree + 1):
  120. matB[i] = x_exp_y_sums[i]
  121. for j in range(degree + 1):
  122. matA[i][j] = x_exp_sums[i + j]
  123.  
  124. return gaussianElimination(matA, matB)
  125.  
  126.  
  127. def expFunc(x, a_0, a_1):
  128. return a_0 * math.e ** (a_1 * x)
  129.  
  130.  
  131. def exponentialRegression(x, y, n):
  132. # mapped to ln
  133. topLeft = 0
  134. x_sum = 0
  135. z_sum = 0
  136. bottomLeft = 0
  137. for i in range(n):
  138. topLeft += x[i] * math.log(y[i])
  139. x_sum += x[i]
  140. z_sum += math.log(y[i])
  141. bottomLeft += x[i] ** 2
  142.  
  143. topLeft *= n
  144. bottomLeft *= n
  145. topRight = x_sum * z_sum
  146. bottomRight = x_sum ** 2
  147.  
  148. a_1 = (topLeft - topRight) / (bottomLeft - bottomRight)
  149. a_0 = z_sum / n - a_1 * x_sum / n
  150.  
  151. return math.e ** a_0, a_1
  152.  
  153.  
  154. def getDataFromFile(filename):
  155. x = []
  156. y = []
  157.  
  158. file = open(filename)
  159.  
  160. i = 0
  161. in_x = True
  162. for line in file:
  163. for word in line.split():
  164. if i == 0:
  165. n1 = int(word)
  166. elif i == 1:
  167. n2 = int(word)
  168. elif in_x:
  169. x.append(float(word))
  170. in_x = False
  171. else:
  172. y.append(float(word))
  173. in_x = True
  174. i += 1
  175.  
  176. file.close()
  177. x = np.array(x)
  178. y = np.array(y)
  179. return x, y
  180.  
  181.  
  182. x = [80, 40, -40, -120, -200, -280, -340]
  183. y = [6.47e-6, 6.24e-6, 5.72e-6, 5.09e-6, 4.30e-6, 3.33e-6, 2.45e-6]
  184. # showGraphScatter(x, y)
  185. constants = polynomialRegression(x, y, 7, 2)
  186. showGraph(-400, 200, constants, x, y)
  187. print('result using polynomial', constants)
  188.  
  189. # x = [0, 1, 3, 5, 7, 9]
  190. # y = [1, 0.891, 0.708, 0.562, 0.447, 0.355]
  191. input = getDataFromFile('input.txt')
  192. x = input[0]
  193. y = input[1]
  194. # showGraphScatter(x, y)
  195. constants = exponentialRegression(x, y, 6)
  196. print('result using exponential', constants)
  197. showGraphExp(0, 30, constants[0], constants[1], x, y)
  198.  
RAW Paste Data