我正在尝试(在 python 中)将一系列任意数量的高斯函数(由仍在改进的简单算法确定)拟合到数据集。对于我当前的样本数据集,我有 174 个高斯函数。我有一个适合的程序,但它基本上是复杂的猜测和检查,并且消耗了所有 4GB 的可用内存。
有没有办法使用 scipy 或 numpy 中的东西来实现这一点?
这是我要使用的,其中波长 [] 是 x 坐标列表,而通量c [] 是 y 坐标列表:
#选择一个高斯
在范围(0,2)中重复:
对于范围内的 f(0,len(质心)):
#遍历所有其他高斯
对于我在范围内(0,len(质心)):
如果我!= f:
#对于每个波长,
对于 w 波长:
#将每个的值附加到一个列表中,称为其他
others.append(height[i]*math.exp(-(w-centroid[i])**2/(2*width[i]**2)))
#优化当前高斯的质心
上一页 = 质心 [f]
最佳 = 质心 [f]
#选择一个数量级
对于范围内的 p (int(round(math.log10(centroid[i]))-3-repeat),int(round(math.log10(centroid[i])))-6-repeat,-1):
#选择那个数量级的值
对于范围内的 m (-5,9):
#改变当前项的值
质心[f] = prev + m * 10 **(p)
#在所有波长上递增,列出新值
变异 = 0
残差 = 0
测试 = []
#在每个波长上增加并评估此变化是否使 R^2 更大
对于范围内的k(0,len(波长)):
test.append(height[i]*math.exp(-(wavelength[k]-centroid[f])**2/(2*width[i]**2)))
残差 += (test[k]+others[k]-cflux[k])**2
变异性 += (test[k]+others[k]-avgcflux)**2
rsquare = 1-(残差/变异)
#检查这个新拟合的 R^2 值
如果 rsquare > bestr:
bestr = rsquare
最佳 = 质心 [f]
质心[f] = 最佳
#优化当前高斯的高度
上一页 = 高度[f]
最佳 = 高度 [f]
#选择一个数量级
对于范围内的 p (int(round(math.log10(height[i]))-repeat),int(round(math.log10(height[i])))-3-repeat,-1):
#选择那个数量级的值
对于范围内的 m (-5,9):
#改变当前项的值
高度[f] = prev + m * 10 **(p)
#在所有波长上递增,列出新值
变异 = 0
残差 = 0
测试 = []
#在每个波长上增加并评估此变化是否使 R^2 更大
对于范围内的k(0,len(波长)):
test.append(height[f]*math.exp(-(wavelength[k]-centroid[i])**2/(2*width[i]**2)))
残差 += (test[k]+others[k]-cflux[k])**2
变异性 += (test[k]+others[k]-avgcflux)**2
rsquare = 1-(残差/变异)
#检查这个新拟合的 R^2 值
如果 rsquare > bestr:
bestr = rsquare
最佳 = 高度 [f]
高度[f] = 最佳
#优化当前高斯的宽度
上一页 = 宽度[f]
最佳 = 宽度 [f]
#选择一个数量级
对于范围内的 p (int(round(math.log10(width[i]))-repeat),int(round(math.log10(width[i])))-3-repeat,-1):
#选择那个数量级的值
对于范围内的 m (-5,9):
如果 prev + m * 10**(p) == 0:
m+=1
#改变当前项的值
宽度[f] = prev + m * 10 **(p)
#在所有波长上递增,列出新值
变异 = 0
残差 = 0
测试 = []
#在每个波长上增加并评估此变化是否使 R^2 更大
对于范围内的k(0,len(波长)):
test.append(height[i]*math.exp(-(wavelength[k]-centroid[i])**2/(2*width[f]**2)))
残差 += (test[k]+others[k]-cflux[k])**2
变异性 += (test[k]+others[k]-avgcflux)**2
rsquare = 1-(残差/变异)
#检查这个新拟合的 R^2 值
如果 rsquare > bestr:
bestr = rsquare
最佳 = 宽度 [f]
宽度[f] = 最佳
计数 += 1
#print '{} of {} peaks 优化,迭代 {} of {}'.format(f+1,len(centroid),repeat+1,2)
完成=圆形(100 *(计数/(浮点数(len(质心))* 2)),2)
print '{}% 完成'.format(完成)
print 'New R^2 = {}'.format(bestr)