Saya memiliki kelas yang menjelaskan fungsi matematika. Kelas harus mampu menyesuaikan kuadrat terkecil agar dapat meneruskan data. yaitu Anda dapat memanggil metode seperti ini:
classinstance.Fit(x,y)
dan menyesuaikan variabel internalnya agar sesuai dengan data. Saya mencoba menggunakan scipy.optimize.curve_fit untuk ini, dan saya perlu meneruskan fungsi model. Masalahnya adalah fungsi model berada di dalam kelas dan perlu mengakses variabel dan anggota kelas untuk menghitung data. Namun, curve_fit tidak dapat memanggil fungsi yang parameter pertamanya adalah self. Apakah ada cara untuk membuat curve_fit menggunakan metode kelas sebagai fungsi modelnya?
Berikut cuplikan minimum yang dapat dieksekusi untuk menunjukkan masalahnya:
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)
Dan ini menghasilkan grafik berikut dan kemudian crash karena fungsi model salah ketika mencoba melakukan penyesuaian.
Terima kasih sebelumnya!