2016-06-08 408 views
1

我知道有與此有關的線程,但我很困惑,我想讓我的數據適合我的數據。Python - 從記錄值擬合指數衰減曲線

我的數據被導入並繪製成這樣。

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' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')

有關如何解決此問題的任何想法?

+0

看看http://stackoverflow.com/questions/21420792/exponential-curve-fitting-in-scipy –

回答

3

你必須使用curve_fit從scipy.optimize: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

+0

涼。對於這種情況,我是否必須爲我的數據集應用for循環? – DarthLazar

+0

循環什麼? 'curve_fit'已經搜索了合適的擬合函數。 – Serenity

+0

當我將x和y的值定義爲數組時,我用這個返回,'ufunc'multiply'不包含具有簽名匹配類型的循環dtype('S32')dtype('S32')dtype('S32 ')' – DarthLazar

1

「我與此錯誤消息

TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32') 

如何任何想法返回解決這個問題?「

讀取CSV文件以創建FreqTime1DelayCount1的代碼正在創建字符串數組。你可以通過@StanleyR在評論中提出的建議來解決這個問題。一個更好的想法是將這段代碼:

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) 

有:

FreqTime1, DecayCount1 = np.loadtxt('Half_Life.csv', delimiter=',', usecols=(0, 3), unpack=True)