Rich S
Rich S

Reputation: 51

python curve_fit() error message only size 1 arrays can be converted to python scalars

I'm given the following equation and asked to find a few different things one of which is the least squares estimate.

function

I have the experimentally determined data and write the function.

xData = np.array([0, 2, 5, 10, 15, 20, 25, 30]) # hours
yData = np.array([ 0.398,  0.543,  1.56,  4.34,  7.22, 9.86 , 10.5, 10.6]) # biomass, g/L

def func1(t,x_infin,x_0,k):
    'nonlinear function in a and b to fit to data'
    g = x_infin/ (1+(((x_infin-x_0)/x_0)*math.exp(-k*t)))
    
    return g

I then set up curve_fit()

initial_guess = [10.5,0.0,16.5] # intial guesses for x_infin, x_0 and k
pars, pcov = curve_fit(func1, xData, yData, p0 = initial_guess)

and get

only size-1 arrays can be converted to Python scalars

I'm not sure where I am passing an array where it shouldn't be. My understanding of the curve_fit() from the documentation is that I'm passing in the function, xdata ydata and then my guesses for the parameters.

Could someone point out where i'm going wrong?

Upvotes: 3

Views: 789

Answers (1)

AJ Biffl
AJ Biffl

Reputation: 584

curve_fit uses arrays under the hood for the t parameter while it does its thing.

math.exp only works on scalar inputs; use numpy.exp to work with arrays

Upvotes: 3

Related Questions