Unsupported Operand Type Error with scipy.optimize.curve_fit

kilgoretrout

I am trying to use scipy.optimize.curve_fit to fit a model function, but the following code gives me the following error:

from scipy.optimize import curve_fit
from math import log10

def my_func(x, alpha):
return [10*log10(alpha*y*y) for y in x]

known_x = [1039.885254, 2256.833008, 6428.667969, 30602.62891] #known x-values
known_y = [31.87999916, 33.63000107, 35, 36.74000168]

popt, pcov = curve_fit(my_func, known_x, known_y)

The error I get is:

TypeError: unsupported operand type(s) for -: 'list' and 'list'

I know related questions have been asked here and here but I wasn't able to solve my problem from those answers.

I did double check the type of of the arguments that curve_fit sends to my function and I saw that alpha comes in as numpy.float64 and x as list

Thanks for your help.

Here is the traceback error:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    popt, pcov = curve_fit(my_func, known_x, known_y)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/minpack.py", line 506, in curve_fit
    res = leastsq(func, p0, args=args, full_output=1, **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/minpack.py", line 348, in leastsq
    m = _check_func('leastsq', 'func', func, x0, args, n)[0]
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/minpack.py", line 14, in _check_func
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/minpack.py", line 418, in _general_function
    return function(xdata, *params) - ydata
TypeError: unsupported operand type(s) for -: 'list' and 'list'

Here is _general_function:

def _general_function(params, xdata, ydata, function):
    return function(xdata, *params) - ydata
CT Zhu

You need to convert you lists to np.array:

def my_func(x, alpha):
    return np.array([10*np.log10(alpha*y*y) for y in x])

known_x = np.array([1039.885254, 2256.833008, 6428.667969, 30602.62891]) #known x-values
known_y = np.array([31.87999916, 33.63000107, 35, 36.74000168])

Result:

(array([ 0.00012562]), array([[  2.38452809e-08]]))

The reason is quite evident as indicated by this message:

TypeError: unsupported operand type(s) for -: 'list' and 'list'

Sure, list can not be subtracted by a list. In order to do so, we need them to be in numpy.array

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unsupported Operand Type Error with scipy.optimize.curve_fit

From Dev

Type error: unsupported operand

From Dev

Python Type Error Unsupported Operand

From Dev

Unsupported operand type(s) for +: 'float' and 'str' error

From Dev

Euclidean Distance Error: unsupported operand type

From Dev

Error message: unsupported operand type(s) for ___

From Dev

scipy curve_fit error: divide by zero encountered

From Dev

scipy curve_fit error: divide by zero encountered

From Dev

scipy curve_fit returns error for keyword absolute_sigma

From Dev

scipy.optimize.curve_fit raises a runtime error

From Dev

scipy.optimize.curve_fit raises a runtime error

From Dev

Unsupported operand type in Python

From Dev

type error: unsupported operand type(s) for +: 'WebElement' and 'WebElement' for selenium

From Dev

Python Error TypeError: unsupported operand type(s) for +: 'function' and 'function'

From Dev

Solving "error: Unsupported left operand type for + ("Iterable[str]") [operator]"

From Dev

Python Error: unsupported operand type(s) for +: 'int' and 'NoneType'

From Dev

Django-cms error "unsupported operand type(s) for +: 'set' and 'tuple'"

From Dev

Python Error : unsupported operand type(s) for +: 'int' and 'datetime.timedelta'

From Dev

Unsupported operand type error when trying to multiply function parameter by an integer

From Dev

Solving "error: Unsupported left operand type for + ("Iterable[str]") [operator]"

From Dev

python error; unsupported operand type(s) for +: 'int' and 'str'

From Dev

Python Error: unsupported operand type(s) for +: 'int' and 'NoneType'

From Dev

Django-cms error "unsupported operand type(s) for +: 'set' and 'tuple'"

From Dev

getting error unsupported operand type(s) for +: 'int' and 'str'

From Dev

Python error: unsupported operand type(s) for -: 'float' and 'NoneType'

From Dev

Error in pip: unsupported operand type(s) for -=: 'Retry' and 'int'

From Dev

Why does exp**2 produce the error "unsupported operand type(s) for **"?

From Dev

How to debug the error "unsupported operand type" in my student grading script?

From Dev

TypeError: unsupported operand type in Python

Related Related

  1. 1

    Unsupported Operand Type Error with scipy.optimize.curve_fit

  2. 2

    Type error: unsupported operand

  3. 3

    Python Type Error Unsupported Operand

  4. 4

    Unsupported operand type(s) for +: 'float' and 'str' error

  5. 5

    Euclidean Distance Error: unsupported operand type

  6. 6

    Error message: unsupported operand type(s) for ___

  7. 7

    scipy curve_fit error: divide by zero encountered

  8. 8

    scipy curve_fit error: divide by zero encountered

  9. 9

    scipy curve_fit returns error for keyword absolute_sigma

  10. 10

    scipy.optimize.curve_fit raises a runtime error

  11. 11

    scipy.optimize.curve_fit raises a runtime error

  12. 12

    Unsupported operand type in Python

  13. 13

    type error: unsupported operand type(s) for +: 'WebElement' and 'WebElement' for selenium

  14. 14

    Python Error TypeError: unsupported operand type(s) for +: 'function' and 'function'

  15. 15

    Solving "error: Unsupported left operand type for + ("Iterable[str]") [operator]"

  16. 16

    Python Error: unsupported operand type(s) for +: 'int' and 'NoneType'

  17. 17

    Django-cms error "unsupported operand type(s) for +: 'set' and 'tuple'"

  18. 18

    Python Error : unsupported operand type(s) for +: 'int' and 'datetime.timedelta'

  19. 19

    Unsupported operand type error when trying to multiply function parameter by an integer

  20. 20

    Solving "error: Unsupported left operand type for + ("Iterable[str]") [operator]"

  21. 21

    python error; unsupported operand type(s) for +: 'int' and 'str'

  22. 22

    Python Error: unsupported operand type(s) for +: 'int' and 'NoneType'

  23. 23

    Django-cms error "unsupported operand type(s) for +: 'set' and 'tuple'"

  24. 24

    getting error unsupported operand type(s) for +: 'int' and 'str'

  25. 25

    Python error: unsupported operand type(s) for -: 'float' and 'NoneType'

  26. 26

    Error in pip: unsupported operand type(s) for -=: 'Retry' and 'int'

  27. 27

    Why does exp**2 produce the error "unsupported operand type(s) for **"?

  28. 28

    How to debug the error "unsupported operand type" in my student grading script?

  29. 29

    TypeError: unsupported operand type in Python

HotTag

Archive