【问题标题】:curve_fit : 'numpy.float64' object cannot be interpreted as an integercurve_fit : 'numpy.float64' 对象不能解释为整数
【发布时间】:2018-05-15 16:49:53
【问题描述】:

我正在尝试以这种方式与 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

【问题讨论】:

  • 能否显示错误的整个回溯日志?
  • @GPhilo 是的,我已经编辑了帖子。
  • numpy 版本有bug。看到这个github.com/rbgirshick/py-faster-rcnn/issues/481
  • 报错前popt的值是多少?也不是y4u = [max(HR5[1801:1820]), max(HR...,而是做y4u = HR5.reshape(20,-1).max(axis=0)之类的事情,并使用rangearange作为zz4u
  • @Dan popt 值:[ 0.87397104 0.22422044 -0.11489561 0.0100413 ]

标签: python scipy curve-fitting curve data-fitting


【解决方案1】:

您的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).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2020-06-03
    相关资源
    最近更新 更多