gpt4 book ai didi

Python - 从记录值拟合指数衰减曲线

转载 作者:太空宇宙 更新时间:2023-11-03 14:11:20 25 4
gpt4 key购买 nike

我知道有与此相关的线程,但我对我想要将数据适合的位置感到困惑。

我的数据是这样导入和绘制的。

import matplotlib.pyplot as plt
%matplotlib inline
import pylab as plb
import numpy as np
import scipy as sp
import csv

FreqTime1 = []
DecayCount1 = []
with open('Half_Life.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
FreqTime1.append(row[0])
DecayCount1.append(row[3])

FreqTime1 = np.array(FreqTime1)
DecayCount1 = np.array(DecayCount1)

fig1 = plt.figure(figsize=(15,6))
ax1 = fig1.add_subplot(111)
ax1.plot(FreqTime1,DecayCount1, ".", label = 'Run 1')
ax1.set_xlabel('Time (sec)')
ax1.set_ylabel('Count')
plt.legend()

enter image description here

问题是,我在设置一般指数衰减时遇到困难,我不确定如何从数据集中计算参数值。

如果可能的话,我还希望将拟合衰减方程的方程与图表一起显示。但如果能够产生合身性,则可以轻松应用。

编辑 -------------------------------------- ------------------------

所以在使用 Stanely R 提到的拟合函数时

def model_func(x, a, k, b):
return a * np.exp(-k*x) + b

x = FreqTime1
y = DecayCount1


p0 = (1.,1.e-5,1.)
opt, pcov = curve_fit(model_func, x, y, p0)
a, k, b = opt

我收到此错误消息

TypeError: ufunc 'multiply' 不包含签名匹配类型 dtype('S32') dtype('S32') dtype('S32') 的循环

关于如何解决这个问题有什么想法吗?

最佳答案

您必须使用 scipy.optimize 中的 curve_fit:http://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.optimize.curve_fit.html

from scipy.optimize import curve_fit
import numpy as np
# define type of function to search
def model_func(x, a, k, b):
return a * np.exp(-k*x) + b

# sample data
x = np.array([399.75, 989.25, 1578.75, 2168.25, 2757.75, 3347.25, 3936.75, 4526.25, 5115.75, 5705.25])
y = np.array([109,62,39,13,10,4,2,0,1,2])

# curve fit
p0 = (1.,1.e-5,1.) # starting search koefs
opt, pcov = curve_fit(model_func, x, y, p0)
a, k, b = opt
# test result
x2 = np.linspace(250, 6000, 250)
y2 = model_func(x2, a, k, b)
fig, ax = plt.subplots()
ax.plot(x2, y2, color='r', label='Fit. func: $f(x) = %.3f e^{%.3f x} %+.3f$' % (a,k,b))
ax.plot(x, y, 'bo', label='data with noise')
ax.legend(loc='best')
plt.show()

enter image description here

关于Python - 从记录值拟合指数衰减曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37713691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com