Search code examples
pythonscipynon-linear-regression

Python nonlinear regression error using curve_fit


I'm trying to obtain 3 unknown parameters of a function using scipy.optimize.curve_fit. I took the example code from the Scipy documentation found here : https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

I use simple data and plot it :

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

xdata = np.array([4.2, 8.5, 10.3, 17.2, 20.7, 38.2, 75.6, 850, 1550])     
ydata = np.array([83.3, 53.3, 44.8, 32.6, 28.1, 19.5, 11.5, 5.7, 5.3]) 

plt.plot(xdata, ydata, 'b-', label='data')

And this is the function and the rest of the code :

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

popt, pcov = curve_fit(func, xdata, ydata)
popt

plt.plot(xdata, func(xdata, *popt), 'r-',
         label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1., 0.5]))
popt

plt.plot(xdata, func(xdata, *popt), 'g--',
         label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

I get the error below.

TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'

And after all the error details :

error: Result from function call is not a proper array of floats.

I tried xdata = np.array( ... , dtype='float64'), and tried all solutions proposed on this thread with no success : Cannot cast array data from dtype('O') to dtype('float64')

Any advice and ideas to make this regression work ?


Solution

  • This code executes without error for me (note: I changed m.exp to np.exp):

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    
    xdata = np.array([4.2, 8.5, 10.3, 17.2, 20.7, 38.2, 75.6, 850, 1550])     
    ydata = np.array([83.3, 53.3, 44.8, 32.6, 28.1, 19.5, 11.5, 5.7, 5.3]) 
    fig, ax = plt.subplots()
    ax.plot(xdata, ydata, 'b-', label='data')
    def func(x, a, b, c):
        return x*(a*(1-np.exp(-b/x))+c*np.exp(-b/x))-x*c
    
    popt, pcov = curve_fit(func, xdata, ydata)
    
    plt.plot(xdata, func(xdata, *popt), 'r-',
             label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
    
    popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1., 0.5]))
    
    ax.plot(xdata, func(xdata, *popt), 'g--',
             label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
    
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.legend()
    plt.show()
    

    Albeit the fit is poor:

    Bad fit

    I'm using python 3.5.4:

    matplotlib                2.2.0
    numpy                     1.14.2
    scipy                     1.0.0