我可以强制 lmfit 高斯模型都具有正振幅吗?

如何解决我可以强制 lmfit 高斯模型都具有正振幅吗?

我正在对一些数据拟合高斯曲线,虽然“拟合”非常令人印象深刻,但有时会使用负振幅来实现它。下面的代码生成数据、拟合和组成高斯曲线的图。您会看到一条幅度为 577 的压倒性高斯曲线和另一条幅度为 -570 的高斯曲线,这有助于拟合曲线。

我更喜欢会牺牲一点“精度”来强制所有振幅为正的拟合。有什么办法可以强制模型的参数保持振幅为正?

import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy import signal
import math
from lmfit import models

perc = [0,0.000019,0.000294,0.001946,0.007003,0.017276,0.033345,0.055974,0.086622,0.126049,0.173105,0.226678,0.286038,0.351443,0.42241,0.498498,0.578561,0.661742,0.746826,0.83285,0.919105,1.00482,1.08944,1.17218,1.25306,1.33216,1.40968,1.48517,1.55839,1.62919,1.69746,1.7625,1.82327,1.87912,1.93017,1.97694,2.01944,2.05721,2.08996,2.11797,2.14111,2.15795,2.16612,2.16457,2.15517,2.14131,2.125,2.10617,2.08647,2.07406,2.08323,2.12419,2.19053,2.25702,2.29272,2.28612,2.25781,2.24839,2.29069,2.38262,2.48227,2.52351,2.44913,2.24458,1.94567,1.61752,1.32053,1.08224,0.895022,0.730309,0.559126,0.375057,0.205875,0.099411,0.06067,0.063161,0.079791,0.089018,0.073056,0.041268,0.014748,0.002668,0.000197,0]
gsize = [0.04,0.04391,0.048203,0.052916,0.058089,0.063768,0.070002,0.076845,0.084358,0.092605,0.101658,0.111597,0.122507,0.134483,0.147631,0.162064,0.177907,0.1953,0.214393,0.235353,0.258361,0.283619,0.311346,0.341784,0.375198,0.411878,0.452145,0.496347,0.544872,0.59814,0.656615,0.720807,0.791275,0.868632,0.953552,1.04677,1.14911,1.26145,1.38477,1.52015,1.66876,1.8319,2.011,2.2076,2.42342,2.66033,2.92042,3.20592,3.51934,3.8634,4.2411,4.65572,5.11087,5.61052,6.15902,6.76114,7.42212,8.14773,8.94427,9.81869,10.7786,11.8323,12.9891,14.2589,15.6529,17.1832,18.863,20.7071,22.7315,24.9538,27.3934,30.0714,33.0113,36.2385,39.7813,43.6704,47.9397,52.6264,57.7713,63.4192,69.6192,76.4253,83.8969,92.0988,101.103,110.987,121.837,133.748,146.824,161.177,176.935,194.232,213.221,234.066,256.948,282.068,309.644,339.916,373.147,409.626,449.672,493.633,541.892,594.869,653.025,716.866,786.949,863.883,948.338,1041.05,1142.83,1254.55,1377.2,1511.84,1659.64,1821.89]

xinit = gsize
xlog = [math.log(xval) for xval in xinit]
x = np.array(xlog)
y = np.array(perc)

peaks = signal.find_peaks_cwt(y,(2.5,25))             
xstep = x.ptp() / len(x)

model,params = None,None

for i,peak_index in enumerate(peaks):
    this_model = models.GaussianModel(prefix=f'p{1+i:d}_')
    this_params = this_model.make_params(amplitude=y[peak_index],center=x[peak_index],sigma=2*xstep)
    if model is None:
        model = this_model
        params = this_params
    else:
        model += this_model
        params.update(this_params)

result = model.fit(y,params,x=x)
print(result.fit_report())

params_list = ['p1_','p2_','p3_','p4_','p5_','p6_']
plt.plot(figsize=(60,50))
plt.ylim(-4.0,4.0)
for param in params_list:
   try:
                
       center = result.params[param + 'center'].value
       sigma = result.params[param + 'sigma'].value
       mean = center
       standard_deviation = sigma
       amplitude = result.params[param + 'amplitude'].value
       x_values = np.arange(-2,10,0.1)
       y_values = scipy.stats.norm(mean,standard_deviation)
       plt.plot(x_values,(amplitude*y_values.pdf(x_values)))

   except KeyError:
       continue

plt.plot(x,y,label='data')
plt.plot(x,result.best_fit,label='fit')
plt.legend()
plt.show()

解决方法

这里的问题是你有太多的自由度,它会过拟合。

如果你拟合一个只有三个高斯分布的模型

for i,peak_index in enumerate(peaks[:3]):
    this_model = models.GaussianModel(prefix=f'p{1+i:d}_')
    this_params = this_model.make_params(amplitude=y[peak_index],center=x[peak_index],sigma=2*xstep)
    if model is None:
        model = this_model
        params = this_params
    else:
        model += this_model
        params.update(this_params)

它不会使用负振幅

3 gaussians

,

是的,您可以将每个“振幅”参数设置为正值。类似的东西:

for i,peak_index in enumerate(peaks):
    this_model = models.GaussianModel(prefix=f'p{1+i:d}_')
    this_params = this_model.make_params(amplitude=y[peak_index],sigma=2*xstep)
    this_params[f'p{1+i:d}_amplitude'].min = 0.  # <--- set min value here!

    if model is None:
        model = this_model
        params = this_params
    else:
        model += this_model
        params.update(this_params)

应该这样做。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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