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

本文介绍了如何使用Python进行最小二乘法曲线拟合。内容包括最小二乘法的基本原理,线性回归的一般式以及矩阵形式的推导。通过计算损失函数的导数找到最优解,并给出了二次曲线拟合的示例。
摘要由CSDN通过智能技术生成

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

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

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

博客

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

关于矩阵形式的推导

dc479799cfe5c5eda8f4021c5e455a3b.png

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

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()        

416326100e6023a964442341f7affd05.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值