  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # scipy curve_fit fails on easy linear fit?  » python » scipy curve_fit fails on easy linear fit?

By : Victor Perez
Date : November 22 2020, 02:42 PM
To fix the issue you can do NaN values will produce meaningless results - you need to exclude them from your data before doing any fitting. You use boolean indexing to do this: code :
``````valid = ~(np.isnan(y1) | np.isnan(y2))
popt, pcov = scop.curve_fit(f, y2[valid], y1[valid])
`````` ## scipy curve_fit fails on exponential fit

By : A.B.
Date : March 29 2020, 07:55 AM
wish of those help When I try to do an exponential fit using curve_fit, scipy returns an error. Am I doing something wrong? Removing the negative sign from np.exp(-b * t) allows curve_fit to work, but the values it returns are way off. , change x and y to numpy arrays
code :
``````x = np.array([40,45,50,55,60])
y = np.array([0.99358851674641158, 0.79779904306220106, 0.60200956937799055, 0.49521531100478472, 0.38842105263157894])
`````` ## Linear fit with scipy.optimize.curve_fit

By : Minimum
Date : March 29 2020, 07:55 AM
this one helps. Python cannot multiply lists and scalars. That's why you imported from scipy import asarray as ar. You could have used numpy as well. So when you call func_lw you should give it an array, not a list.
code :
``````func_lw(ar(xval_list), fitted_params, fitted_params )
plt.plot(ar(xval_list), func_lw(ar(xval_list), *fitted_params))
plt.scatter(ar(xval_list), ar(yval_list))
``````
``````def func_lw(x_lw, slope, offset):
return slope*x_lw + offset
``````
``````xval_list = [(8/np.sqrt(3))*((2*pi*m_e*float(x)*10**9)/q_e) for x, y in freq_lw_tuple]
yval_list = [y*10**(-4)*4*pi*10**(-7) for x, y in freq_lw_tuple]
`````` ## multivariable non-linear curve_fit with scipy

By : Rick James
Date : March 29 2020, 07:55 AM
wish helps you I figured out the issue. The problem for some reason was the use of math.exp and cmath.exp in the fitting function func. In place of these functions I used np.exp(). I am not completely sure the reason why though. ## Python scipy.optimise.curve_fit gives linear fit

By : user3510246
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Here is example code using your data and equation, with the initial parameter estimates given by scipy's differential_evolution genetic algorithm module. That module uses the Latin Hypercube algorithm to ensure a thorough search of parameter space, which requires bounds within which to search. In this example those bounds are taken from the data maximum and minimum values. It is much easier to supply ranges for the initial parameter estimates rather than specific values.
code :
``````import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.optimize import differential_evolution
import warnings

def func(x, a, b, c):
return (a - c) * numpy.exp(-x / b) + c

xData = numpy.linspace(60, 3060, 200)
yData = func(xData, 100, 400, 20)

# noise
numpy.random.seed(1729)
yData = yData + numpy.random.normal(size=xData.size) * 0.2

# function for genetic algorithm to minimize (sum of squared error)
def sumOfSquaredError(parameterTuple):
warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
val = func(xData, *parameterTuple)
return numpy.sum((yData - val) ** 2.0)

def generate_Initial_Parameters():
# min and max used for bounds
maxX = max(xData)
minX = min(xData)
maxY = max(yData)
minY = min(yData)

parameterBounds = []
parameterBounds.append([minY, maxY]) # search bounds for a
parameterBounds.append([minX, maxX]) # search bounds for b
parameterBounds.append([minY, maxY]) # search bounds for c

# "seed" the numpy random number generator for repeatable results
result = differential_evolution(sumOfSquaredError, parameterBounds, seed=3)
return result.x

# by default, differential_evolution completes by calling curve_fit() using parameter bounds
geneticParameters = generate_Initial_Parameters()

# now call curve_fit without passing bounds from the genetic algorithm,
# just in case the best fit parameters are aoutside those bounds
fittedParameters, pcov = curve_fit(func, xData, yData, geneticParameters)
print('Fitted parameters:', fittedParameters)
print()

modelPredictions = func(xData, *fittedParameters)

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print()
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()

##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

# first the raw data as a scatter plot
axes.plot(xData, yData,  'D')

# create data for the fitted equation plot
xModel = numpy.linspace(min(xData), max(xData))
yModel = func(xModel, *fittedParameters)

# now the model as a line plot
axes.plot(xModel, yModel)

axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

plt.show()
plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)
`````` ## Performing a weighted linear fit with scipy.optimize.curve_fit

By : user3715423
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I want to perform a weighted linear fit to extract the parameters m and c in the equation y = mx+c. The data I want to perform the fit on is: , IIUC then what you are looking for is the sigma keyword argument.
code :
``````sigma: None or M-length sequence or MxM array, optional

Determines the uncertainty in ydata. If we define residuals as r = ydata - f(xdata, *popt),
then the interpretation of sigma depends on its number of dimensions:
A 1-d sigma should contain values of standard deviations of errors in ydata.
In this case, the optimized function is chisq = sum((r / sigma) ** 2).

None (default) is equivalent of 1-d sigma filled with ones.
``````
``````def func(x, m, c):
return m * x + c

curve_fit(func, xdata, ydata, sigma=yerr)
`````` 