Ilyankor
Ilyankor

Reputation: 171

Result from function call is not an array of floats

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.

Upvotes: 3

Views: 4107

Answers (1)

Solvalou
Solvalou

Reputation: 1143

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

Upvotes: 6

Related Questions