亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何執行多條曲線的聯合擬合(在 Python 中)?

如何執行多條曲線的聯合擬合(在 Python 中)?

神不在的星期二 2021-09-14 15:54:47
假設我通過簡單的線性回歸擬合一些數據點。現在我想對幾組數據點執行幾個聯合線性回歸。更具體地說,我希望所有擬合中的一個參數相等,此處示意性地描繪了 y 軸交點。在谷歌搜索了一段時間后,我既找不到任何 Python (Scipy) 例程可以做到這一點,也找不到任何一般文獻,說明如何實現這一點。理想情況下,我不僅要在簡單線性回歸的情況下執行這些聯合擬合,而且還要針對更一般的擬合函數(例如,冪律擬合與聯合指數)。
查看完整描述

2 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

我認為這個圖形代碼示例可以滿足您的需求,使用單個共享參數擬合兩個數據集。請注意,如果數據集的長度不等,則可以有效地加權對具有更多單個點的數據集的擬合。此示例將初始參數值顯式設置為 1,0 - curve_fit() 默認值 - 并且不使用 scipy 的遺傳算法來幫助查找初始參數估計值。


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)



查看完整回答
反對 回復 2021-09-14
  • 2 回答
  • 0 關注
  • 749 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號