python - 从 scipy.optimize.leastsq() 输出所有猜测
问题描述
我希望制作一个关于 scipy.optimize.leastsq() 提供的最小二乘回归分析如何收敛于特定结果的动画。有没有办法让函数,比如,将每次迭代的猜测值元组附加到列表中,直到函数收敛到局部最小值?或者,是否有其他包含此功能的库?
以下是我所拥有的:
# initial guess for gaussian distributions to optimize [height, position, width].
# if more than 2 distributions required, add a new set of [h,p,w] initial parameters to 'initials' for each new distribution.
# new parameters should be of the same format for consistency; i.e. [h,p,w],[h,p,w],[h,p,w]... etc.
# A 'w' guess of 1 is typically a sufficient estimation.
initials = [6.5,13,1],[4.5,19,1]
# determines the number of gaussian functions to compute from the initial guesses
n = len(initials)
# formats initials into a 1D array
var = np.concatenate(initials)
# data matrix
M = np.array(master)
# defines a typical gaussian function, of independent variable x,
# amplitude a, position b, and width parameter c.
def gaussian(x,a,b,c):
return a*np.exp((-(x-b)**2.0)/c**2.0)
# defines the expected resultant as a sum of intrinsic gaussian functions
def GaussSum(x, p):
return sum(gaussian(x, p[3*k], p[3*k+1], p[3*k+2]) for k in range(n))
# defines condition of minimization, reducing the square of the difference between the data (y) and the function 'func(x,p)'
def residuals(p, y, x):
return (y - GaussSum(x,p))**2
# executes least-squares regression analysis to optimize initial parameters
cnsts = leastsq(residuals, var, args=(M[:,1],M[:,0]))[0]
我最终希望'cnsts'成为从最初猜测到最终猜测的每个猜测的元组列表。
解决方案
如果我正确理解您的问题,您想在拟合线性回归线时对每个不同的系数进行猜测,然后列出所有已猜测的系数?类似于神经网络如何反向传播错误以更好地拟合模型?
线性回归不是在猜测不同的系数。它只是在计算它们...... https://www.statisticshowto.datasciencecentral.com/probability-and-statistics/regression-analysis/find-a-linear-regression-equation/#FindaLinear
推荐阅读
- c++ - 如何在 lambda C++ 中模拟结构化绑定初始化捕获?
- python - 反转熊猫数据框的列顺序中的错误
- github - 无法在 GitHub 中打开文件夹
- c++ - 如何检测 C++20 协程中的堆栈展开?
- php - 为什么我在这个 php endif 语句中出现错误?
- binary - N个十六进制数字对应多少位?
- c# - 数据网格中带有复选框的组合框
- javascript - 单击数据表中的行按钮后如何防止页面刷新?
- firebase - 请求api https://fcmdata.googleapis.com/v1beta1/{parent=projects/*/androidApps/*}/deliveryData 出现错误信息
- c# - 如何在wpf中绘制具有不规则点的垂直堆叠阶梯线图?