天天看点

python 最小二乘法_最小二乘法曲线拟合

关于最小二乘法的原理,可以去百度一下。它常用于解决线性回归问题。

1)一般式为y_hat=ax+b 。Loss=(y_hat - y)^2  。Loss最小得到的参数即为所求解---Loss对x求导。

2)具体推导 :先看懂这个博客

博客

https://www.cnblogs.com/wellp/p/9286240.html

关于矩阵形式的推导

python 最小二乘法_最小二乘法曲线拟合

关于矩阵类,如果无法理解,我们只需要记得最后的结论

C=(XTX)-1XTY

如果是2次曲线,C包含三个参数,(因为2次曲线方程y=ax^2+bx+c)

import numpy as npimport pandas as pdimport matplotlib.pyplot as plt# reads the data from excel using pandasdata_1= pd.read_csv("D:/python_train/RANSAC/ransac_sample/Reference/data_1.csv")# prints the number of rows and columns in the format(rows, format)print(data_1.shape)  # prints the first five rowsprint(data_1.head()) # calls all the values of x and y from the excelX = data_1['x'].valuesY = data_1['y'].valuesn = 250 # Total Number of values in X or Y# converts the data in the form of a matrix [1 X X^2] and [Y]x = np.column_stack([np.ones(n),X,X**2]) # 矩阵合并print(x.shape)y = np.transpose(np.array(Y))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      xt = np.transpose(x) #转置xtx = np.dot(xt,x) #求xt与x之间的点阵乘法xty = np.dot(xt,y) #求xt与y之间的点阵乘法#Solving for a in [xtx]*a = [xty]#求解线性方程组aX=b ,求Xa = np.linalg.solve(xtx,xty)print("a.shape:", a.shape)# print (a)# Calculating the values of x and yxp = np.linspace(0,500,50)yp = a[0] + a[1]*xp + a[2]*xp**2# print(xp)# print(yp)# Ploting the scatter pointsplt.plot(X,Y,'ko',label='data_1')plt.xlabel('X Axis')plt.ylabel('Y Axis')plt.title('Data 1')    # Plotting the regression non linear lineplt.plot(xp,yp,linewidth=5)plt.show()        
           
python 最小二乘法_最小二乘法曲线拟合