curve-fitting

Restrict fitted regression line (abline) to range of data used in model

﹥>﹥吖頭↗ 提交于 2021-02-19 23:44:20
问题 Is it possible to draw an abline of a fit only in a certain range of x-values? I have a dataset with a linear fit of a subset of that dataset: # The dataset: daten <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2)) # make a linear fit for the datapoints 3, 4, 5 daten_fit <- lm(formula = y~x, data = daten, subset = 3:5) When I plot the data and draw a regression line: plot (y ~ x, data = daten) abline(reg = daten_fit) The line is drawn for the full range of x-values in the

Restrict fitted regression line (abline) to range of data used in model

拥有回忆 提交于 2021-02-19 23:39:06
问题 Is it possible to draw an abline of a fit only in a certain range of x-values? I have a dataset with a linear fit of a subset of that dataset: # The dataset: daten <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2)) # make a linear fit for the datapoints 3, 4, 5 daten_fit <- lm(formula = y~x, data = daten, subset = 3:5) When I plot the data and draw a regression line: plot (y ~ x, data = daten) abline(reg = daten_fit) The line is drawn for the full range of x-values in the

How to properly get the errors in lmfit

本秂侑毒 提交于 2021-02-11 17:42:21
问题 Hello I am trying to learn how to properly use lmfit and I think I am calculating the fit errors wrong. I have some data with errors on y and when I do the fit I call this (I tried for a simple linear fit): weight = 1/err out = line_fit.fit(y, pars, x=x, weights = weight) I assumed that this would calculate the chi square and use the errors in the denominator. However it seems to not work properly. The fit looks good and I am getting a reasonable value for the errors, but if I increase the

How to properly get the errors in lmfit

房东的猫 提交于 2021-02-11 17:41:02
问题 Hello I am trying to learn how to properly use lmfit and I think I am calculating the fit errors wrong. I have some data with errors on y and when I do the fit I call this (I tried for a simple linear fit): weight = 1/err out = line_fit.fit(y, pars, x=x, weights = weight) I assumed that this would calculate the chi square and use the errors in the denominator. However it seems to not work properly. The fit looks good and I am getting a reasonable value for the errors, but if I increase the

Fitting multiple Lorentzians to Brillouin Spectrum using Scipy in Python 3

醉酒当歌 提交于 2021-02-11 17:00:52
问题 I am trying to fit Brillouin Spectra (with several peaks) using scipy.optimize.curve_fit. I have have multiple spectra with several peaks and I am trying to fit them with lorentzian functions (one Lorentzian per peak). I am trying to automate the process for bulk analysis (i.e., using the peak finding algorithm of scipy to get peak positions, peak widths and peaks heights and use them as initial guesses for the fit). I am now working on one spectrum to see if the general idea works, then I

Fitting multiple Lorentzians to Brillouin Spectrum using Scipy in Python 3

杀马特。学长 韩版系。学妹 提交于 2021-02-11 16:58:45
问题 I am trying to fit Brillouin Spectra (with several peaks) using scipy.optimize.curve_fit. I have have multiple spectra with several peaks and I am trying to fit them with lorentzian functions (one Lorentzian per peak). I am trying to automate the process for bulk analysis (i.e., using the peak finding algorithm of scipy to get peak positions, peak widths and peaks heights and use them as initial guesses for the fit). I am now working on one spectrum to see if the general idea works, then I

How to set bounds for only one parameter

北城以北 提交于 2021-02-11 14:44:13
问题 I'm using curve_fit from scipy.optimize to fit my data. I have a function that fits three parameters (Z1, Z2, Z3). I wannt to provide bounds. However, I'd like to only provide a bound to Z2 (Z2 shall be below 40). I do not want to set bounds for Z1 and Z3. Is that possible? popt, pcov = curve_fit(func, xdata, ydata, p0 = [Z1, Z2, Z3], bounds = ((10, 20, 5), (100, 50, 100,))) # This way I provide bounds to Z1, Z2 and Z3 # I, however, only want to say that Z2 < 40 # Also interesting would be to

exponential decay with scipy just gives step function

微笑、不失礼 提交于 2021-02-11 14:26:35
问题 I'm trying to do an exponential fit with a set of data: import matplotlib.pyplot as plt import numpy as np import scipy.optimize as opt def func(x, a, b, c): return a * np.exp(x / -b) + c epr_data = np.loadtxt('T2_text', skiprows=1) time = epr_data[:, 1] intensity = epr_data[:, 2] optimizedParameters, pcov = opt.curve_fit(func, time, intensity) print(optimizedParameters) plt.plot(time, intensity, func(time, *optimizedParameters), label="fit") plt.show() but i just get this step function and

TypeError: Improper input: N=5 must not exceed M=2

霸气de小男生 提交于 2021-02-11 14:19:40
问题 I'm trying to use scipy.optimize.curve_fit with a custom fit function (roughly following this tutorial): # Fit function def fit_function(x, y, x0, y0, A, FWHM): return A*np.exp(1)*4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2*np.exp(-4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2) # Open image file img = Image.open('/home/user/image.tif') # xdata X, Y = img.size xRange = np.arange(1, X+1) yRange = np.arange(1, Y+1) xGrid, yGrid = np.meshgrid(xRange, yRange) xyGrid = np.vstack((xGrid.ravel(), yGrid

lmfit model fitting and then prediction

孤街浪徒 提交于 2021-02-10 21:54:19
问题 I was adopting lmfit to do a curve fitting and use that fitted model to do prediction. However, the following code did not achieve what I want. Could you please help? Thanks. import numpy as np from lmfit import Model def linearModel(x, a0, a1): return a0+a1*x #main code begin here X=[1,2,4] # data for fitting y=[2,4,6] # data for fitting gmodel = Model(linearModel) #select model params = gmodel.make_params(a0=1, a1=1) # initial params result = gmodel.fit(y, params, x=X) # curve fitting x1=[1