- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对一个困惑的函数进行高斯拟合。我只想适合外部外壳(这些不仅仅是每个 x 处的最大值,因为一些最大值也会太低,因为样本量很小)。
from scipy.optimize import curve_fit
def Gauss(x, a, x0, sigma, offset):
return a * np.exp(-np.power(x - x0,2) / (2 * np.power(sigma,2))) + offset
def fitNormal(x, y):
popt, pcov = curve_fit(Gauss, x, y, p0=[np.max(y), np.median(x), np.std(x), np.min(y)])
return popt
plt.plot(xPlot,yPlot, 'k.')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Y(x)')
x,y = xPlot,yPlot
popt = fitNormal(x, y)
minx, maxx = np.min(x), np.max(x)
xFit = np.arange(start=minx, stop=maxx, step=(maxx-minx)/1000)
yFitTest = Gauss(xPlot, popt[0], popt[1], popt[2], popt[3])
print('max fit test: ',np.max(yFitTest))
print('max y: ',np.max(yPlot))
maxIndex = np.where(yPlot==np.max(yPlot))[0][0]
factor = yPlot[maxIndex]/yFitTest[maxIndex]
yFit = Gauss(xPlot, popt[0], popt[1], popt[2], popt[3]) * factor
plt.plot(xFit,yFit,'r')
最佳答案
这是一种类似于 this post 的迭代方法。它的不同之处在于图的形状不允许使用凸包。因此,我们的想法是创建一个成本函数,尝试最小化图形的面积,同时如果某个点位于图形上方则付出高昂的成本。根据 OP 中图形的类型,需要调整成本函数。还必须检查最终结果中所有点是否确实位于图表下方。在这里,人们可以修改成本函数的细节。例如,我可以在 tanh
中包含一个偏移量,例如 tanh(slope * ( x - offset) )
以使解远离数据。
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import leastsq
def g( x, a, s ):
return a * np.exp(-x**2 / s**2 )
def cost_function( params, xData, yData, slope, val ):
a,s = params
area = 0.5 * np.sqrt( np.pi ) * a * s
diff = np.fromiter ( ( y - g( x, a, s) for x, y in zip( xData, yData ) ), np.float )
cDiff = np.fromiter( ( val * ( 1 + np.tanh( slope * d ) ) for d in diff ), np.float )
out = np.concatenate( [ [area] , cDiff ] )
return out
xData = np.linspace( -5, 5, 500 )
yData = np.fromiter( ( g( x, .77, 2 ) * np.sin( 257.7 * x )**2 for x in xData ), np.float )
sol=[ [ 1, 2.2 ] ]
for i in range( 1, 6 ):
solN, err = leastsq( cost_function, sol[-1] , args=( xData, yData, 10**i, 1 ) )
sol += [ solN ]
print sol
fig = plt.figure()
ax = fig.add_subplot( 1, 1, 1)
ax.scatter( xData, yData, s=1 )
for solN in sol:
solY = np.fromiter( ( g( x, *solN ) for x in xData ), np.float )
ax.plot( xData, solY )
plt.show()
给予
>> [0.8627445 3.55774814]
>> [0.77758636 2.52613376]
>> [0.76712184 2.1181137 ]
>> [0.76874125 2.01910211]
>> [0.7695663 2.00262339]
和
关于python - 如何适应函数的外壳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56027276/
快速简单的问题: 如何制作多种颜色的开关/外壳。 我能做的是: if(color == Color.Red) color = Color.Green; else { if(color
我有这个 switch/case 结构: public void onClick(View arg0) { // TODO Auto-generated method stub swi
在我的程序中,我想使用不同的进程访问多个 linux shell。 目前我正在使用子进程我目前没有 linux 机器来测试它所以你能告诉我这是否有效。 子进程是否在一个终端上工作?如果是这样,还有其他
我在后面的代码中有三次与此示例非常相似的代码。每次开关关闭发送给它的选项。每个time case里面的代码除了一个参数是完全一样的基于案例。使用 switch/case 和方法是最好的方法去做这个?我
阅读 LTP shell code它使用奇怪的 for 循环语法: for arg; do TCID="${TCID}_$arg" done 它如何接受参数?我希望它循环遍历 $arg,用 $
这不是最有见地的问题,但我真的希望能够按 Control-L 并让 shell iex 清除屏幕。知道如何做到这一点,或者是否有另一个带有此内置功能的 Elixir shell ? 最佳答案 clea
我已经切换到 Enthought 的 Canopy 1.0.0,但我想念独立的 QT shell 和 QT notebook。我不想使用 IDE 中的内置 shell。我在哪里可以找到 QT shel
我在PyCharm中创建了新的flask项目,但看不到如何在集成的PyCharm python控制台窗口中运行flask shell。 启动控制台时未定义名称应用程序: 我仍然可以在集成的PyChar
尝试创建Prism shell 时,出现以下异常: An exception of type 'Microsoft.Practices.ServiceLocation.ActivationExcept
这个问题在这里已经有了答案: How to use the switch statement in R functions? (4 个回答) 4年前关闭。 我有一系列嵌套 if..else我想用 ca
cat < file.txt < file2.txt 我正在编写一个简单的 shell,我正在思考上面的表达式的含义。我很清楚这是什么意思:cat < file.txt,但双“<”让我困惑。 实现重定
我正在考虑使用 CompositeWPF ( http://www.codeplex.com/CompositeWPF ) - 又名 Prism,来构建我正在处理的应用程序。 该应用程序不是传统的 L
我有一个没有图形用户界面的操作类。该类主要是做数据管理。该类是从我的主 GUI 中的方法调用的。我遇到的问题是在出现故障时向用户显示消息。我正在使用 MessageDialog,但它在运行时一直失败。
我在教程中看到了两种在 BASH shell 中对 if 语句进行语法处理的方法: 除非我在变量周围加上引号并添加额外的 [ 和 ],否则这个不会工作: if [[ "$step" -eq 0 ]]
我的脚本接受键值对作为命令行参数。我测试给定数量的参数是否偶数。如果偶数,我想遍历数组,将 args[n] & args[n+1] 视为一对 (n = 0 to $# - 1) 并执行另一个将这两个作
全部!我的 CKEditor 有问题!我需要用 div 将 ul、ol 列表括起来。因此,当我按下面板上的列表按钮时,我会得到这样的结果: 最佳答
我在更改我的 Android 外壳中的所有者时遇到了一些问题。我正在尝试更改文件的所有者和组以匹配目录中的另一个文件。当我 ls -l 其他文件拥有 u0_a49 的所有者时,我尝试 su chown
平台:RHEL7 情况: JMeter 报告文件每 5 分钟通过 crontab 脚本附加新结果 另一个 awk 脚本查找响应时间大于 500 毫秒并发送电子邮件警报 问题陈述: 要求是只扫描报告文件
对于 Python,我使用 PyCrust,这是一个很好的图形外壳,感觉就像一个很好的 Java IDE(具有自动完成、内联文档并可以保存历史记录)。 Ruby 有类似的东西吗? 最佳答案 如果不需要
以下出现在我的 WinProc 中: if(message == WM_CREATE) { //Do WM_CREATE stuff } else { switch(message)
我是一名优秀的程序员,十分优秀!