You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Least_squares.py 5.3 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # ---
  2. # jupyter:
  3. # jupytext_format_version: '1.2'
  4. # kernelspec:
  5. # display_name: Python 3
  6. # language: python
  7. # name: python3
  8. # language_info:
  9. # codemirror_mode:
  10. # name: ipython
  11. # version: 3
  12. # file_extension: .py
  13. # mimetype: text/x-python
  14. # name: python
  15. # nbconvert_exporter: python
  16. # pygments_lexer: ipython3
  17. # version: 3.5.2
  18. # ---
  19. # # Least squares
  20. #
  21. # A mathematical procedure for finding the best-fitting curve to a given set of points by minimizing the sum of the squares of the offsets ("the residuals") of the points from the curve. The sum of the squares of the offsets is used instead of the offset absolute values because this allows the residuals to be treated as a continuous differentiable quantity. However, because squares of the offsets are used, outlying points can have a disproportionate effect on the fit, a property which may or may not be desirable depending on the problem at hand.
  22. #
  23. # ### Show the data
  24. #
  25. # +
  26. # %matplotlib inline
  27. import matplotlib.pyplot as plt
  28. import numpy as np
  29. import sklearn
  30. from sklearn import datasets
  31. # load data
  32. d = datasets.load_diabetes()
  33. X = d.data[:, 2]
  34. Y = d.target
  35. # draw original data
  36. plt.scatter(X, Y)
  37. plt.xlabel("X")
  38. plt.ylabel("Y")
  39. plt.show()
  40. # -
  41. # ### Theory
  42. # For $N$ observation data:
  43. # $$
  44. # \mathbf{X} = \{x_1, x_2, ..., x_N \} \\
  45. # \mathbf{Y} = \{y_1, y_2, ..., y_N \}
  46. # $$
  47. #
  48. # We want to find the model which can predict the data. The simplest model is linear model, which has the form of
  49. # $$
  50. # y = ax + b
  51. # $$
  52. #
  53. # The purpose is to find parameters $a, b$ which best fit the model to the observation data.
  54. #
  55. # We use the sum of squares to measure the differences (loss function) between the model's prediction and observation data:
  56. # $$
  57. # L = \sum_{i=1}^{N} (y_i - a x_i + b)^2
  58. # $$
  59. #
  60. # To make the loss function minimize, we can find the parameters:
  61. # $$
  62. # \frac{\partial L}{\partial a} = -2 \sum_{i=1}^{N} (y_i - a x_i - b) x_i \\
  63. # \frac{\partial L}{\partial b} = -2 \sum_{i=1}^{N} (y_i - a x_i - b)
  64. # $$
  65. # When the loss is minimized, therefore the partial difference is zero, then we can get:
  66. # $$
  67. # -2 \sum_{i=1}^{N} (y_i - a x_i - b) x_i = 0 \\
  68. # -2 \sum_{i=1}^{N} (y_i - a x_i - b) = 0 \\
  69. # $$
  70. #
  71. # We reoder the items as:
  72. # $$
  73. # a \sum x_i^2 + b \sum x_i = \sum y_i x_i \\
  74. # a \sum x_i + b N = \sum y_i
  75. # $$
  76. # By solving the linear equation we can obtain the model parameters.
  77. # ### Program
  78. # +
  79. N = X.shape[0]
  80. S_X2 = np.sum(X*X)
  81. S_X = np.sum(X)
  82. S_XY = np.sum(X*Y)
  83. S_Y = np.sum(Y)
  84. A1 = np.array([[S_X2, S_X],
  85. [S_X, N]])
  86. B1 = np.array([S_XY, S_Y])
  87. coeff = np.linalg.inv(A1).dot(B1)
  88. print('a = %f, b = %f' % (coeff[0], coeff[1]))
  89. x_min = np.min(X)
  90. x_max = np.max(X)
  91. y_min = coeff[0] * x_min + coeff[1]
  92. y_max = coeff[0] * x_max + coeff[1]
  93. plt.scatter(X, Y, label='original data')
  94. plt.plot([x_min, x_max], [y_min, y_max], 'r', label='model')
  95. plt.legend()
  96. plt.show()
  97. # -
  98. # ## How to fit polynomial function?
  99. #
  100. # If we observe a missle at some time, then how to estimate the trajectory? Acoording the physical theory, the trajectory can be formulated as:
  101. # $$
  102. # y = at^2 + bt + c
  103. # $$
  104. # The we need at least three data to compute the parameters $a, b, c$.
  105. #
  106. #
  107. # +
  108. t = np.array([2, 4, 6, 8])
  109. #t = np.linspace(0, 10)
  110. pa = -20
  111. pb = 90
  112. pc = 800
  113. y = pa*t**2 + pb*t + pc
  114. plt.scatter(t, y)
  115. plt.show()
  116. # -
  117. # ## How to use sklearn to solve linear problem?
  118. #
  119. #
  120. # +
  121. from sklearn import linear_model
  122. # load data
  123. d = datasets.load_diabetes()
  124. X = d.data[:, np.newaxis, 2]
  125. Y = d.target
  126. # create regression model
  127. regr = linear_model.LinearRegression()
  128. regr.fit(X, Y)
  129. a, b = regr.coef_, regr.intercept_
  130. print("a = %f, b = %f" % (a, b))
  131. x_min = np.min(X)
  132. x_max = np.max(X)
  133. y_min = a * x_min + b
  134. y_max = a * x_max + b
  135. plt.scatter(X, Y)
  136. plt.plot([x_min, x_max], [y_min, y_max], 'r')
  137. plt.show()
  138. # -
  139. # ## How to use sklearn to fit polynomial function?
  140. # +
  141. # Fitting polynomial functions
  142. from sklearn.preprocessing import PolynomialFeatures
  143. from sklearn.linear_model import LinearRegression
  144. from sklearn.pipeline import Pipeline
  145. t = np.array([2, 4, 6, 8])
  146. pa = -20
  147. pb = 90
  148. pc = 800
  149. y = pa*t**2 + pb*t + pc
  150. model = Pipeline([('poly', PolynomialFeatures(degree=2)),
  151. ('linear', LinearRegression(fit_intercept=False))])
  152. model = model.fit(t[:, np.newaxis], y)
  153. model.named_steps['linear'].coef_
  154. # -
  155. # ## How to estimate some missing value by the model?
  156. #
  157. # +
  158. # load data
  159. d = datasets.load_diabetes()
  160. N = d.target.shape[0]
  161. N_train = int(N*0.9)
  162. N_test = N - N_train
  163. X = d.data[:N_train, np.newaxis, 2]
  164. Y = d.target[:N_train]
  165. X_test = d.data[N_train:, np.newaxis, 2]
  166. Y_test = d.target[N_train:]
  167. # create regression model
  168. regr = linear_model.LinearRegression()
  169. regr.fit(X, Y)
  170. Y_est = regr.predict(X_test)
  171. print("Y_est = ", Y_est)
  172. print("Y_test = ", Y_test)
  173. err = (Y_est - Y_test)**2
  174. score = regr.score(X_test, Y_test)
  175. print("err = %f, score = %f" % (np.sqrt(np.sum(err))/N_test, score))
  176. # plot data
  177. a, b = regr.coef_, regr.intercept_
  178. print("a = %f, b = %f" % (a, b))
  179. x_min = np.min(X)
  180. x_max = np.max(X)
  181. y_min = a * x_min + b
  182. y_max = a * x_max + b
  183. plt.scatter(X, Y, label='train data')
  184. plt.scatter(X_test, Y_test, label='test data')
  185. plt.plot([x_min, x_max], [y_min, y_max], 'r', label='model')
  186. plt.legend()
  187. plt.show()
  188. # -

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。

Contributors (1)