簡體   English   中英

使用 scipy 對單個函數的多個輸出進行曲線擬合

[英]Curve fitting multiple outputs from a single function with scipy

好的,我有一個函數,它使用一系列參數來計算隨着時間的推移對兩個獨立變量的影響。 這些變量已經與一些現有數據進行了曲線匹配,以最大限度地減少變化(如下所示) 例子

我希望能夠檢查以前的工作,並匹配新數據。 我一直在嘗試使用scipy.optimize.curve_fit函數,通過堆疊由我的函數產生的 x 和 y 數據(如此處建議的: 使用 scipy 擬合多個參數曲線)。

這可能不是正確的方法,或者我可能只是誤解了,但是我的代碼一直遇到類型錯誤TypeError: Improper input: N=3 must not exceed M=2

我的簡化原型代碼最初取自這里: https : //docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

def func(x, a, b, c):
    result = ([],[])
    for i in x:
        #set up 2 example curves
        result[0].append(a * np.exp(-b * i) + c)
        result[1].append(a * np.exp(-b * i) + c**2)
    return result #as a tuple containing 2 lists

#Define the data to be fit with some noise:
xdata = list(np.arange(0, 10, 1))
y = func(xdata, 2.5, 5, 0.5)[0]
y2 = func(xdata, 1, 1, 2)[1]

#Add some noise
y_noise = 0.1 * np.random.normal(size=len(xdata))
y2_noise = 0.1 * np.random.normal(size=len(xdata))

ydata=[]
ydata2=[]

for i in range(len(y)): #clunky
    ydata.append(y[i] + y_noise[i])
    ydata2.append(y2[i] + y2_noise[i])

plt.scatter(xdata, ydata, label='data')
plt.scatter(xdata, ydata2, label='data2')
#plt.plot(xdata, y, 'k-', label='data (original function)')
#plt.plot(xdata, y2, 'k-', label='data2 (original function)')

#stack the data
xdat = xdata+xdata
ydat = ydata+ydata2

popt, pcov = curve_fit(func, xdat, ydat)

plt.plot(xdata, func(xdata, *popt), 'r-',
         label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

非常感謝任何幫助!

下面是用一個共享參數擬合兩個不同方程的圖形示例代碼,如果這看起來像您需要的那樣,它可以很容易地適應您的特定問題。

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

y1 = np.array([ 16.00,  18.42,  20.84,  23.26])
y2 = np.array([-20.00, -25.50, -31.00, -36.50, -42.00])
comboY = np.append(y1, y2)

x1 = np.array([5.0, 6.1, 7.2, 8.3])
x2 = np.array([15.0, 16.1, 17.2, 18.3, 19.4])
comboX = np.append(x1, x2)

if len(y1) != len(x1):
    raise(Exception('Unequal x1 and y1 data length'))
if len(y2) != len(x2):
    raise(Exception('Unequal x2 and y2 data length'))


def function1(data, a, b, c): # not all parameters are used here, c is shared
        return a * data + c

def function2(data, a, b, c): # not all parameters are used here, c is shared
        return b * data + c


def combinedFunction(comboData, a, b, c):
    # single data reference passed in, extract separate data
    extract1 = comboData[:len(x1)] # first data
    extract2 = comboData[len(x1):] # second data

    result1 = function1(extract1, a, b, c)
    result2 = function2(extract2, a, b, c)

    return np.append(result1, result2)


# some initial parameter values
initialParameters = np.array([1.0, 1.0, 1.0])

# curve fit the combined data to the combined function
fittedParameters, pcov = curve_fit(combinedFunction, comboX, comboY, initialParameters)

# values for display of fitted function
a, b, c = fittedParameters

y_fit_1 = function1(x1, a, b, c) # first data set, first equation
y_fit_2 = function2(x2, a, b, c) # second data set, second equation

plt.plot(comboX, comboY, 'D') # plot the raw data
plt.plot(x1, y_fit_1) # plot the equation using the fitted parameters
plt.plot(x2, y_fit_2) # plot the equation using the fitted parameters
plt.show()

print('a, b, c:', fittedParameters)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM