- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 Scipy.Optimise Curve_fit 按照简单示例 here 对某些数据进行指数拟合。 .
脚本运行没有错误,但是配合很糟糕。当我在 curve_fit 的每一步查看 popt 的输出时,它似乎并没有很好地从初始参数跳到一系列 1.0 的迭代,尽管它似乎使第 3 个参数恢复到一个比较不错的值:
92.0 0.01 28.0
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0
1.00012207031 1.0 1.0
1.0 1.00012207031 1.0
1.0 1.0 1.00012207031
1.0 1.0 44.3112882656
1.00012207031 1.0 44.3112882656
1.0 1.00012207031 44.3112882656
1.0 1.0 44.3166973584
1.0 1.0 44.3112896048
1.0 1.0 44.3112882656
我不确定是什么导致了这种情况,除了模型可能与数据不太吻合,尽管我强烈怀疑它应该如此(物理学就是物理学)。有人有什么想法吗?我已经在下面发布了我的(非常简单的)脚本。谢谢。
#!/usr/bin/python
import matplotlib.pyplot as plt
import os
import numpy as np
from scipy.optimize import curve_fit
from matplotlib.ticker import*
from glob import glob
from matplotlib.backends.backend_pdf import PdfPages
import fileinput
path_src=os.getcwd()
dirlist= glob(path_src + '/Gel_Temp_Res.txt')
dirlist.sort()
plots_file='Temp_Curve.pdf'
plots= PdfPages(path_src+'/'+plots_file)
time=[]
temp=[]
for row in fileinput.input(path_src + '/Gel_Temp_Res.txt'):
time.append(row.split()[0])
temp.append(row.split()[1])
nptime=np.array(time, dtype='f')
nptemp=np.array(temp, dtype='f')
del time[:]
del temp[:]
# Newton cooling law fitting
def TEMP_FIT(t, T0, k, Troom):
print T0, k, Troom
return T0 * np.exp(-k*t) + Troom
y = TEMP_FIT(nptime[41:], nptemp[41]-nptemp[0], 1e-2, nptemp[0])
yn = y + 0.2*np.random.normal(size=len(nptime[41:]))
popt, pcov = curve_fit(TEMP_FIT, nptime[41:], yn)
# Plotting
ax1 = plt.subplot2grid((1,1),(0, 0))
ax1.set_position([0.1,0.1,0.6,0.8])
plt.plot(nptime[41:], nptemp[41:], 'bo--',label='Heater off', alpha=0.5)
plt.plot(nptime[41:], TEMP_FIT(nptime[41:], *popt), label='Newton Cooling Law Fit')
plt.xlim(-25, 250)
plt.xlabel('Time (min)')
plt.ylabel('Temperature ($^\circ$C)')
ax1.grid(True, which='both', axis='both')
plt.legend(numpoints=1, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig(plots, format='pdf',orientation='landscape')
plt.close()
plots.close()
此外,这是我尝试拟合的数据:
100 124
130 120
135 112
140 105
145 99
150 92
155 82
160 75
165 70
170 65
175 60
180 56
185 55
190 52
195 49
200 45
205 44
210 40
215 39
220 37
225 35
最佳答案
大的负指数使指数函数接近于零,从而使最小二乘算法对您的拟合参数不敏感。
因此,在根据时间戳用指数拟合指数函数时,最好的方法是通过排除第一个数据点的时间来调整时间指数,将其更改为:
f = exp(-x*t)
到:
t0 = t[0] # place this outside loops
f = exp(-x*(t - t0))
将此概念应用于您的代码会导致:
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
time, temp = np.loadtxt('test.txt', unpack=True)
t0 = time[0]
# Newton cooling law fitting
def TEMP_FIT(t, T0, k, Troom):
print(T0, k, Troom)
return T0 * np.exp(-k*(t - t0)) + Troom
popt, pcov = curve_fit(TEMP_FIT, time, temp)
# Plotting
plt.figure()
plt.plot(time, temp, 'bo--',label='Heater off', alpha=0.5)
plt.plot(time, TEMP_FIT(time, *popt), label='Newton Cooling Law Fit')
plt.xlim(-25, 250)
plt.xlabel('Time (min)')
plt.ylabel('Temperature ($^\circ$C)')
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.legend(fontsize=8)
plt.savefig('test.png', bbox_inches='tight')
结果是:
删除样本的第一个点:
关于python - Scipy.Optimise Curve_fit 的指数拟合不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23081913/
我正在尝试将数据放入 this file 中使用 Python 中 scipy 的 curve_fit。该文件包含以摄氏度和毫秒为单位的温度与时间的数据点。我将它们转换为开尔文和秒: thefile
我正在尝试将数据集拟合到这个庞大的方程中。我知道以前有人问过这个问题,但我不认为最初的猜测是我的问题,我也不能在拟合方程中添加更多项。 我的拟合方程。请注意,积分中的“u”与上面定义的 u 不同。 顺
我有一个包含多列的 pandas.DataFrame ,我想应用 curve_fit对他们每个人都有作用。我希望输出是一个数据框,其中具有适合列中数据的最佳值(目前,我对它们的协方差不感兴趣)。 df
我在这里定义了一个函数来返回任意数量的高斯分布的总和: import numpy from numpy import * import matplotlib.pyplot as plt from sc
我正在尝试将高斯拟合到光谱中,y 值大约为 10^(-19)。在我将整个数据乘以 10^(-19) 之前和之后,Curve_fit 给我的拟合结果都很差。附件是我的代码,它是一组相当简单的数据,只是值
我需要适应scipy.optimize.curve_fit一些看起来像图中点的数据。我使用函数 y(x) (见下面的 def)它给出了一个常量 y(x)=c对于 x p[0] '''
我是 Scipy.optimize curve_fit() 工具的新手。我试图找到一个最能模拟图中曲线(时间序列信号的一部分)的函数,但同时我只是将正弦曲线拟合到它并得到一条平坦的线。有人能发现我做错
the link of data from dropbox badfitting我尝试使用 curve_fit 来将数据与我在 python 中的预定义函数进行拟合,但结果远非完美。代码很简单,如下所
抱歉,我是 python 和堆栈流方面的新手。所以我无法发布图像。 我想用Python中的curve_fit函数进行幂律回归。但结果对我来说很奇怪。我用excel进一步检查了一下。看起来这两者差别很大
我正在尝试为拟合函数传递两个数组,该函数接受两个值。 数据文件: 第一栏:时间第 2 栏:温度第 3 栏:交易量第 4 栏:压力 0.000,0.946,4.668,0.981 0.050,0.946
对于我的学士论文,我正在研究一个项目,我想在其中对某些数据进行拟合。问题有点复杂,但我尽量减少这里的问题: 我们有三个数据点(可用的理论数据很少),但这些点高度相关。 使用 curve_fit 来拟合
Python 的 curve_fit计算具有单个自变量的函数的最佳拟合参数,但是有没有办法使用 curve_fit 或其他方法来拟合具有多个自变量的函数?例如: def func(x, y, a, b
本文实例讲述了Python图像处理之直线和曲线的拟合与绘制。分享给大家供大家参考,具体如下: 在数据处理和绘图中,我们通常会遇到直线或曲线的拟合问题,python中scipy模块的子模块optim
我正在尝试使用 curve_fit 来拟合一个简单的正弦波(甚至没有任何噪声)作为测试,然后再继续解决更复杂的问题。不幸的是,它甚至没有给出正确的答案。这是我的语法: x = linspace(0,1
我正在使用Scipy将数据适合一个函数。该函数为我提供了2个参数的值,在这种情况下, a 和 b 。我想使用绑定(bind)的参数来限制这些参数可以采用的值,每个参数都有自己的可接受值范围。 可接受的
我试图找到一条拟合我的数据的曲线,该曲线在视觉上似乎具有幂律分布。 我希望利用 scipy.optimize.curve_fit,但无论我尝试什么函数或数据规范化,我都会得到 RuntimeError
我尝试使用curve_fit拟合对数曲线,假设它遵循Y=a*ln(X)+b,但拟合的数据看起来仍然不正确. 现在我正在使用以下代码: from scipy.optimize import curve_
我正在尝试使用 scipy 的 curve_fit 拟合分布。我尝试拟合一个单分量指数函数,结果几乎是一条直线(见图)。我还尝试了两分量指数拟合,似乎效果很好。两个分量仅意味着方程的一部分使用不同的输
为了拟合双曲函数,我尝试使用以下代码: import numpy as np from scipy.optimize import curve_fit def hyperbola(x, s_1, s_
看起来它只适合第一个参数。 当我尝试使用 curve_fit 示例生成曲线时,这一切都很好,但当我使用自己的数据时却不然。 这是我的原始数据:https://pastebin.com/0hs2JVXL
我是一名优秀的程序员,十分优秀!