curve_fit : 'numpy.float64' 对象不能解释为整数

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

我正在尝试以这种方式与 scipy.optimize.curve_fit 配合:

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='--')

但这给出了错误: TypeError: 'numpy.float64' object cannot be interpreted as an integer。 我正在使用 anaconda3 python。我该如何解决?

编辑:修复 fitFunc

中代码的缩进

编辑:整个日志:

/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

您的 fitFunc 需要矢量化,因此请尝试对 zz4u

使用 numpy 数组
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] 

或更好

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