This article mainly introduces the relevant data about the least squares fitting of matplotlib in Python, and introduces in detail the realization process of fitting curve of the least squares fitting line and the least square method through the example code, and the friends who need can refer to it for reference.
Objective
The least squares least square method, as the basis of the categorical regression algorithm, has a long history (presented by Mari Le Jende in 1806). It matches by minimizing the squared error and finding the best function of the data. By using the least squares method, the unknown data can be easily obtained, and the sum of the errors between the obtained data and the actual data is minimized. The least squares can also be used for curve fitting. Other optimization problems can also be expressed by minimizing the energy or maximizing the entropy using the least squares method.
The following article mainly introduces you to the MATPLOTLIB implementation of the least squares in python to fit the relevant content, the following words do not say, come together to see the detailed introduction:
First, least squares fitting straight line
Generating sample points
First, we generate random points that obey the normal distribution near the line y = 3 + 5x, as a sample point to fit the line.
?
12345678910 |
import numpy as np
import matplotlib.pyplot as plt
# 在直线 y = 3 + 5x 附近生成随机点
X
= np.arange(
0
,
5
,
0.1
)
Z
= [
3 + 5 * x
for x
in X]
Y
= [np.random.normal(z,
0.5
)
for z
in Z]
plt.plot(X, Y,
‘ro‘
)
plt.show()
|
Sample points:
Fit Straight Line
Set y = a0 + a1*x, we use the regular equations of least squares to solve the unknown coefficients a0 and A1.
There is a solve function in the Linalg module of NumPy, which can solve the unknown quantity according to the coefficients matrix of the equations and the vectors of the right end of the equation.
?
12345678910111213 |
def linear_regression(x, y):
N
= len
(x)
sumx
= sum
(x)
sumy
= sum
(y)
sumx2
= sum
(x
*
*
2
)
sumxy
= sum
(x
*
y)
A
= np.mat([[N, sumx], [sumx, sumx2]])
b
= np.array([sumy, sumxy])
return np.linalg.solve(A, b)
a0, a1
= linear_regression(X, Y)
|
Draw a line
At this point, we have obtained the linear equation coefficients a0 and A1 after fitting. Next, we draw the line and compare it to the sample point.
?
1234567 |
# 生成拟合直线的绘制点
_X
= [
0
,
5
]
_Y
= [a0
+ a1
* x
for x
in _X]
plt.plot(X, Y,
‘ro‘
, _X, _Y,
‘b‘
, linewidth
=
2
)
plt.title(
"y = {} + {}x"
.
format
(a0, a1))
plt.show()
|
The fit effect is as follows:
Two, least squares fitting curves
Generating sample points
As with the generated line sample points, we generate random points that obey the normal distribution near the curve y = 2 + 3x + 4x^2, as a sample point to fit the curve.
?
12345678910 |
import numpy as np
import matplotlib.pyplot as plt
# y = 2 + 3x + 4x^2
X
= np.arange(
0
,
5
,
0.1
)
Z
= [
2 + 3 * x
+ 4 * x
*
* 2 for x
in X]
Y
= np.array([np.random.normal(z,
3
)
for z
in Z])
plt.plot(X, Y,
‘ro‘
)
plt.show()
|
Sample points:
Fit curve
The equation for this curve is y = a0 + a1*x + a2*x^2, again, we solve the unknown quantity A0, A1, and A2 by means of the regular equation group.
?
123456789101112131415161718192021222324252627 |
# 生成系数矩阵A
def gen_coefficient_matrix(X, Y):
N
= len
(X)
m
= 3
A
= []
# 计算每一个方程的系数
for i
in range
(m):
a
= []
# 计算当前方程中的每一个系数
for j
in range
(m):
a.append(
sum
(X
*
* (i
+
j)))
A.append(a)
return A
# 计算方程组的右端向量b
def gen_right_vector(X, Y):
N
= len
(X)
m
= 3
b
= []
for i
in range
(m):
b.append(
sum
(X
*
*
i
* Y))
return b
A
= gen_coefficient_matrix(X, Y)
b
= gen_right_vector(X, Y)
a0, a1, a2
= np.linalg.solve(A, b)
|
Drawing curves
We draw an image of the curve according to the curve equation we obtained.
?
1234567 |
# 生成拟合曲线的绘制点
_X
= np.arange(
0
,
5
,
0.1
)
_Y
= np.array([a0
+ a1
*
x
+ a2
*
x
*
*
2 for x
in _X])
plt.plot(X, Y,
‘ro‘
, _X, _Y,
‘b‘
, linewidth
=
2
)
plt.title(
"y = {} + {}x + {}$x^2$ "
.
format
(a0, a1, a2))
plt.show()
|
The fit effect is as follows:
Summarize
The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring certain help, if there are questions you can message exchange, thank you for the script home support.
Articles you may be interested in:
- Implementation of least squares method and implementation code in Python
Original link: http://www.codebelief.com/article/2017/04/matplotlib-demonstrate-least-square-regression-process/
The process of matplotlib implementation of least squares fitting in python