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 2.9 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. # # Linear regression
  20. #
  21. #
  22. # ## Least squares
  23. #
  24. # 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.
  25. #
  26. # ### Show the data
  27. #
  28. # +
  29. # %matplotlib inline
  30. import matplotlib.pyplot as plt
  31. import numpy as np
  32. import sklearn
  33. from sklearn import datasets
  34. # load data
  35. d = datasets.load_diabetes()
  36. X = d.data[:, 2]
  37. Y = d.target
  38. # draw original data
  39. plt.scatter(X, Y)
  40. plt.show()
  41. # -
  42. # ### Theory
  43. # For $N$ observation data:
  44. # $$
  45. # \mathbf{X} = \{x_1, x_2, ..., x_N \} \\
  46. # \mathbf{Y} = \{y_1, y_2, ..., y_N \}
  47. # $$
  48. #
  49. # We want to find the model which can predict the data. The simplest model is linear model, which has the form of
  50. # $$
  51. # y = ax + b
  52. # $$
  53. #
  54. # The purpose is to find parameters $a, b$ which best fit the model to the observation data.
  55. #
  56. # We use the sum of squares to measure the differences (loss function) between the model's prediction and observation data:
  57. # $$
  58. # L = \sum_{i=1}^{N} (y_i - a x_i + b)^2
  59. # $$
  60. #
  61. # To make the loss function minimize, we can find the parameters:
  62. # $$
  63. # \frac{\partial L}{\partial a} = -2 \sum_{i=1}^{N} (y_i - a x_i - b) x_i \\
  64. # \frac{\partial L}{\partial b} = -2 \sum_{i=1}^{N} (y_i - a x_i - b)
  65. # $$
  66. # When the loss is minimized, therefore the partial difference is zero, then we can get:
  67. # $$
  68. # -2 \sum_{i=1}^{N} (y_i - a x_i - b) x_i = 0 \\
  69. # -2 \sum_{i=1}^{N} (y_i - a x_i - b) = 0 \\
  70. # $$
  71. #
  72. # We reoder the items as:
  73. # $$
  74. # a \sum x_i^2 + b \sum x_i = \sum y_i x_i \\
  75. # a \sum x_i + b N = \sum y_i
  76. # $$
  77. # By solving the linear equation we can obtain the model parameters.
  78. # ### Program
  79. # +
  80. N = X.shape[0]
  81. S_X2 = np.sum(X*X)
  82. S_X = np.sum(X)
  83. S_XY = np.sum(X*Y)
  84. S_Y = np.sum(Y)
  85. A1 = np.array([[S_X2, S_X],
  86. [S_X, N]])
  87. B1 = np.array([S_XY, S_Y])
  88. coeff = np.linalg.inv(A1).dot(B1)
  89. print('a = %f, b = %f' % (coeff[0], coeff[1]))
  90. x_min = np.min(X)
  91. x_max = np.max(X)
  92. y_min = coeff[0] * x_min + coeff[1]
  93. y_max = coeff[0] * x_max + coeff[1]
  94. plt.scatter(X, Y)
  95. plt.plot([x_min, x_max], [y_min, y_max], 'r')
  96. plt.show()

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

Contributors (1)