ฉันมีชั้นเรียนที่อธิบายฟังก์ชันทางคณิตศาสตร์ คลาสจะต้องมีกำลังสองน้อยที่สุดที่พอดีกับตัวเองจึงจะส่งข้อมูลได้ นั่นคือคุณสามารถเรียกวิธีการดังนี้:
classinstance.Fit(x,y)
และจะปรับตัวแปรภายในให้เหมาะสมกับข้อมูลมากที่สุด ฉันกำลังพยายามใช้ scipy.optimize.curve_fit สำหรับสิ่งนี้ และมันต้องการให้ฉันผ่านฟังก์ชันโมเดล ปัญหาคือฟังก์ชันโมเดลอยู่ในคลาสและจำเป็นต้องเข้าถึงตัวแปรและสมาชิกของคลาสเพื่อคำนวณข้อมูล อย่างไรก็ตาม curve_fit ไม่สามารถเรียกใช้ฟังก์ชันที่มีพารามิเตอร์แรกเป็น self ได้ มีวิธีทำให้ curve_fit ใช้วิธีการของคลาสเนื่องจากเป็นฟังก์ชันโมเดลหรือไม่?
นี่คือตัวอย่างข้อมูลปฏิบัติการขั้นต่ำเพื่อแสดงปัญหา:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# This is a class which encapsulates a gaussian and fits itself to data.
class GaussianComponent():
# This is a formula string showing the exact code used to produce the gaussian. I
# It has to be printed for the user, and it can be used to compute values.
Formula = 'self.Amp*np.exp(-((x-self.Center)**2/(self.FWHM**2*np.sqrt(2))))'
# These parameters describe the gaussian.
Center = 0
Amp = 1
FWHM = 1
# HERE IS THE CONUNDRUM: IF I LEAVE SELF IN THE DECLARATION, CURVE_FIT
# CANNOT CALL IT SINCE IT REQUIRES THE WRONG NUMBER OF PARAMETERS.
# IF I REMOVE IT, FITFUNC CAN'T ACCESS THE CLASS VARIABLES.
def FitFunc(self, x, y, Center, Amp, FWHM):
eval('y - ' + self.Formula.replace('self.', ''))
# This uses curve_fit to adjust the gaussian parameters to best match the
# data passed in.
def Fit(self, x, y):
#FitFunc = lambda x, y, Center, Amp, FWHM: eval('y - ' + self.Formula.replace('self.', ''))
FitParams, FitCov = curve_fit(self.FitFunc, x, y, (self.Center, self.Amp, self.FWHM))
self.Center = FitParams[0]
self.Amp = FitParams[1]
self.FWHM = FitParams[2]
# Give back a vector which describes what this gaussian looks like.
def GetPlot(self, x):
y = eval(self.Formula)
return y
# Make a gausssian with default shape and position (height 1 at the origin, FWHM 1.
g = GaussianComponent()
# Make a space in which we can plot the gaussian.
x = np.linspace(-5,5,100)
y = g.GetPlot(x)
# Make some "experimental data" which is just the default shape, noisy, and
# moved up the y axis a tad so the best fit will be different.
ynoise = y + np.random.normal(loc=0.1, scale=0.1, size=len(x))
# Draw it
plt.plot(x,y, x,ynoise)
plt.show()
# Do the fit (but this doesn't work...)
g.Fit(x,y)
และสิ่งนี้จะสร้างกราฟต่อไปนี้แล้วหยุดทำงานเนื่องจากฟังก์ชันโมเดลไม่ถูกต้องเมื่อพยายามทำให้พอดี
ขอบคุณล่วงหน้า!