scipy curve_fit fails on easy linear fit?

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])

Share : facebook icon twitter icon
scipy curve_fit fails on exponential fit

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

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[0], fitted_params[1] )
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

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

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
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)

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('RMSE:', RMSE)
print('R-squared:', Rsquared)


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

    # 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.close('all') # clean up after using pyplot

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

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)
Related Posts Related Posts :
  • Extracting multiple rows from pandas dataframe and converting to columns
  • Pinging a remote PC with Flask, causing server to block
  • Making a fractal graph using a 2D array?
  • Replacing a word in a list with a value from a dict
  • Savefig as eps yields a non-usable eps
  • Crispy-forms InlineRadios don't show my model state
  • Getting Title of a text
  • Python with numpy: How to delete an element from each row of a 2-D array according to a specific index
  • Sending and Receive data from a web page - Selenium
  • KeyError with Pyro4
  • Python module error
  • Python :: Attribute in superclass not available in inheriting subclass
  • Why does greater than and unequal operators work even though only less than and equal operator has been overloaded
  • Input length mismatch scikit
  • Print String in decreasing length
  • Overloading the [] operator in python class to refer to a numpy.array data member
  • Sympy - altering the range of the y axis for a plot
  • How do I programmatically list a DLL's dependencies in C++ or Python?
  • How do I lock window resizing in a Python matplotlib window?
  • Word boundary RegEx search using PyMongo
  • Iterating over a string by only changing one element in the string
  • classification of data where attribute values are strings
  • Validate user input using regular expressions
  • Synchronizing and Resampling two timeseries with non-uniform millisecond intraday data
  • determing the number of sentences, words and letters in a text file
  • Deploying impure Python packages to AWS
  • Navigating between multiple Tkinter GUI frames
  • Python - Do I need to remove instances from a dictionary?
  • How can I get the edited values corresponding to the keys of a dictionary in views.py POST method, passed as a context v
  • differentiate between python function and class function
  • From array create tuples on if condition python
  • Looping over a text file list with python
  • Monitoring a real-time data stream with a flask web-app
  • Bad quality after multiple fade effect with pydub
  • delete rows in numpy array in python
  • What are the possible numpy value format strings?
  • Conditional Selecting of child elements in pdfquery
  • Python: split string by closing bracket and write in new line
  • SyntaxWarning: import * only allowed at module level
  • theano ~ use an index matrix and embeddings matrix to produce a 3D tensor?
  • Django background infinite loop process management
  • How can I use Pandas or Numpy to infer a datatype from a list of values?
  • How to add the sum of cubes using a function in python?
  • django registration redux URL's being effected by url with multiple query parameters
  • python - how can I generate a WAV file with beeps?
  • How can I implement a custom RNN (specifically an ESN) in Tensorflow?
  • Python modulo result differs from wolfram alpha?
  • Django on App Engine Managed VM
  • Python - CSV Reading with dictionary
  • Python script works in librarys examples folder, but not in parent directory
  • Dealing with Nested Loops in Python - Options?
  • Get indices of roughly equal sized chunks
  • python - creating dictionary from excel using specific columns
  • SQLAlchemy Determine If Unique Constraint Exists
  • Can I stop rendering variables in Django?
  • Scrapy: traversing a document
  • Common logger settings in Python logging dictConfig
  • Should I pass the object in or build it in the constructor?
  • 3d and 2d subplots in plotly
  • Apache Spark CombineByKey with list of elements in Python
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co