3

Following my previous two posts (post1, post 2), I have now reached the point where I use scipy to find a curve fit. However, the code I have produces an error.

A sample of the .csv file I'm working with is located in post1. I tried to copy and substitute examples from the Internet, but it doesn't seem to be working.

Here's what I have (the .py file)

import pandas as pd
import numpy as np
from scipy import optimize

df = pd.read_csv("~/Truncated raw data hcl.csv", usecols=['time' , '1mnaoh trial 1']).dropna()
data1 = df

array1 = np.asarray(data1)
x , y = np.split(array1,[-1],axis=1)

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

popt, pcov = optimize.curve_fit(func, x , y , p0=[23.2, 30.1 , 1 , 1 , 1])

popt

From the limited research I've done, it might be a problem with the x and y arrays. The title states the error that is written. It is a minpack.error.

Edit: the error returned

ValueError: object too deep for desired array
Traceback (most recent call last):
  File "~/test2.py", line 15, in <module>
    popt, pcov = optimize.curve_fit(func, x , y , p0=[23.2, 30.1 , 1 , 1 , 1])
  File "~/'virtualenvname'/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 744, in curve_fit
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  File "~/'virtualenvname'/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 394, in leastsq
    gtol, maxfev, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.

Thank you.

Ilyankor
  • 171
  • 3
  • 8

1 Answers1

5

After the split, the shape of x and y is (..., 1). This means that each element of them itself are arrays of length one. You want to flatten the array first, i.e. via x = np.flatten(x). But I think you don't need the split at all. You can just do the following

array1 = np.asarray(data1).T
x , y = array1

You want x and y to be the first and second columns of array1. So an easy way to achieve this is to transpose the array first. You could also access them via [:,0] and [:,1].

Solvalou
  • 1,133
  • 1
  • 10
  • 19
  • Thank you for your suggestion! If I may ask another brief question here, I get a "RuntimeWarning: invalid value encountered in power return a + (b - a)/((1 + c*np.exp(-d*x))**(1/e))" when I run it. What's wrong with my expression. – Ilyankor Dec 26 '18 at 00:08
  • No problem. I think the warning happens during the fit. Since within the algorithm different values for the parameters are tested, there may be some cases which are mathematically not allowed. In your case this means that the expression within brackets might be negative (for c < 0). When performing the power function on this negative number, i.e. taking the root, this yields a warning since no complex numbers are taken into account. – Solvalou Dec 26 '18 at 00:17