使用 lmfit 进行高斯拟合

如何解决使用 lmfit 进行高斯拟合

我正在尝试拟合高斯分布。我尝试使用 OriginPro 和 Python 来适应。在 OriginPro 中的拟合比通过 Python 获得的要好,我想用 Python 来做。

OriginPro:

Python:

我使用的代码是:

import numpy as np
import matplotlib.pyplot as plt
import lmfit

data=data
mod=lmfit.models.GaussianModel()
x=np.arange(140,510,1)
y= np.array(np.log(data[140:510]),dtype=np.float32)
idx= (np.isfinite(y))
pars = mod.guess(y[idx],x=x[idx])
out = mod.fit(y[idx],pars,x=x[idx])
plt.plot(x[idx],out.best_fit,'r',label='Gaussian Model')
plt.plot(x,np.log(data[140:510]))
plt.show()

数据:

[ 0.,0.,34.,33.,30.,25.,47.,36.,37.,41.,42.,40.,49.,44.,35.,32.,50.,29.,39.,43.,27.,48.,38.,20.,53.,45.,62.,31.,46.,56.,57.,51.,55.,67.,68.,66.,60.,63.,58.,72.,64.,61.,71.,74.,69.,59.,70.,73.,65.,52.,81.,80.,84.,54.,23.,24.,28.,21.,1.]

fit_report 的输出:

In [37]:out.fit_report()
Out[37]: "[[Model]]\n    Model(gaussian)\n[[Fit Statistics]]\n    # fitting method   = leastsq\n    # function evals   = 17\n    # data points      = 333\n    # variables        = 3\n    chi-square         = 12.5923742\n    reduced chi-square = 0.03815871\n    Akaike info crit   = -1084.59201\n    Bayesian info crit = -1073.16758\n[[Variables]]\n    amplitude:  3590.97370 +/- 125.195985 (3.49%) (init = 781.4034)\n    center:     317.738344 +/- 3.57625327 (1.13%) (init = 322.4771)\n    sigma:      361.440601 +/- 13.6760525 (3.78%) (init = 181.5)\n    fwhm:       851.127556 +/- 32.2046420 (3.78%) == '2.3548200*sigma'\n    height:     3.96355944 +/- 0.01657428 (0.42%) == '0.3989423*amplitude/max(1e-15,sigma)'\n[[Correlations]] (unreported correlations are < 0.100)\n    C(amplitude,sigma) =  0.997"

跟我使用的猜测函数有关系吗?

解决方法

评论中给出的提示似乎不够。您需要在模型中包含偏移量。高斯函数在远离峰值强度的地方趋于 0 - 我不知道 OriginPro 在做什么,但很明显,它的建模不仅仅是高斯函数。

尝试制作一个高斯 + 常数的模型,如下所示:

import numpy as np
import matplotlib.pyplot as plt
import lmfit

data = np.array([ 0.,0.,34.,33.,30.,25.,47.,36.,37.,41.,42.,40.,49.,44.,35.,32.,50.,29.,39.,43.,27.,48.,38.,20.,53.,45.,62.,31.,46.,56.,57.,51.,55.,67.,68.,66.,60.,63.,58.,72.,64.,61.,71.,74.,69.,59.,70.,73.,65.,52.,81.,80.,84.,54.,23.,24.,28.,21.,1.])

data = data[140:510]
ipos = np.where(data>0)[0]
data = np.log(data[ipos])
x    = np.arange(140,510,1.0)[ipos]

mod  = lmfit.models.GaussianModel()  + lmfit.models.ConstantModel()

pars = mod.make_params(c=data.mean(),center=x.mean(),sigma=x.std(),amplitude=x.std()*data.ptp())

out = mod.fit(data,pars,x=x)

print(out.fit_report())

plt.plot(x,out.best_fit,'r',label='Gaussian Model')
plt.plot(x,data)
plt.show()

这将打印一份报告

[Model]]
    (Model(gaussian) + Model(constant))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 48
    # data points      = 333
    # variables        = 4
    chi-square         = 9.50683177
    reduced chi-square = 0.02889615
    Akaike info crit   = -1176.19189
    Bayesian info crit = -1160.95932
[[Variables]]
    amplitude:  58.7086539 +/- 5.27950257 (8.99%) (init = 153.376)
    center:     302.462920 +/- 2.79518589 (0.92%) (init = 322.6336)
    sigma:      48.1997878 +/- 3.66967747 (7.61%) (init = 106.8759)
    c:          3.64131210 +/- 0.01679060 (0.46%) (init = 3.796494)
    fwhm:       113.501824 +/- 8.64142989 (7.61%) == '2.3548200*sigma'
    height:     0.48592258 +/- 0.02641186 (5.44%) == '0.3989423*amplitude/max(1e-15,sigma)'
[[Correlations]] (unreported correlations are < 0.100)
    C(amplitude,c)     = -0.832
    C(amplitude,sigma) =  0.798
    C(sigma,c)         = -0.664

和一个情节

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res