Alessandro Peca
Alessandro Peca

Reputation: 923

curve_fit : 'numpy.float64' object cannot be interpreted as an integer

I'm trying to do a fit with scipy.optimize.curve_fit in this way:

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

def fitFunc(x, a, b, c, d):     
    return a + b*x + c*x*x + d*x*x*x

y4u = [max(HR5[1801:1820]), max(HR5[1821:1840]), max(HR5[1841:1860]), max(HR5[1861:1880]), \
       max(HR5[1881:1900]), max(HR5[1901:1920]), max(HR5[1921:1940]), max(HR5[1941:1960]), \
       max(HR5[1961:1980]), max(HR5[1981:2000]), max(HR5[2001:2020]), max(HR5[2021:2040]), \
       max(HR5[2041:2060]), max(HR5[2061:2080]), max(HR5[2081:2100])]
# y4u = [1.0, 1.0, 1.0, 0.33329999999999999, 0.33329999999999999, 0.0, -0.33329999999999999, -0.3, -0.6, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0]

zz4u = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0]

plt.plot(zz4u, y4u, marker='.', markersize=0, linewidth='0.5', color='navy')
popt, pcov = curve_fit(fitFunc, zz4u, y4u)
plt.plot(zz4u, fitFunc(zz4u, *popt), color='gold', linestyle='--')

But this give the error: TypeError: 'numpy.float64' object cannot be interpreted as an integer. I'm using anaconda3 python. How can i fix that?

Edit: fix indent of a code in fitFunc

Edit: the whole log:

/Users/Alessandro/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py:779: OptimizeWarning: Covariance of the parameters could not be estimated
      category=OptimizeWarning)
    Traceback (most recent call last):
      File "untitled.py", line 79, in <module>
        plt.plot(zz4u, fitFunc(zz4u, *popt4), color='gold', linestyle='--')
      File "untitled.py", line 38, in fitFunc
        return a + b*x + c*x*x + d*x*x*x
    TypeError: 'numpy.float64' object cannot be interpreted as an integer

Upvotes: 0

Views: 2868

Answers (1)

Dan
Dan

Reputation: 45752

your fitFunc needs to be vectorized so try using a numpy array for zz4u

zz4u = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0] 

or better yet

zz4u = np.arange(0, 7.5, 0.5).

Upvotes: 1

Related Questions