【问题标题】:Keep Running into "TypeError: 'numpy.float64' object is not callable"继续遇到“TypeError:'numpy.float64'对象不可调用”
【发布时间】:2017-06-22 20:59:30
【问题描述】:

我不断收到错误

TypeError: 'numpy.float64' 对象不可调用"

我以为我一直小心不要在函数等中重复变量,但是每当我尝试执行时,这个错误就会不断弹出

scipy.integrate.odeint(doublepend,fnaught,t,args=(L,9.81,M))

我不确定发生了什么。其余代码见下文。

import scipy as scipy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math as mt
import numpy as np

def singlepend(thetarray,time,Length,gravity,mass):
    import numpy as np

    theta,delatheta=y
    dtheta=np.zeros_like(y)
    #theta solved
    dtheta[0]=deltatheta
    dtheta[1]=(g/L)*np.sin(theta)
    return dy

def doublepend(y,tim,Langles,g,Mangles):

    import numpy as np

    thetanaught,deltatheta,phinaught,deltaphi=y
    mtheta,mphi=Mangles
    print mtheta
    Ltheta,Lphi=Langles
    dy=np.zeros_like(y)
    #theta solved
    dy[0]=deltatheta
    dy[1]=-mphi*np.sin(thetanaught-phinaught)*((deltatheta**2)*np.cos(thetanaught-phinaught)+(deltaphi**2)*Lphi/Lphi)/(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught)))\
    +mphi*g*np.sin(phinaught)*np.cos(thetanaught-phinaught)/(Ltheta*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))\
    -(mtheta+mphi)*g*np.sin(thetanaught)/(Ltheta*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))
    #phi solved
    dy[2]=deltaphi
    dy[3]=mphi*(deltaphi**2)*np.sin(thetanaught-phinaught)/(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught)))\
    +(mtheta+mphi)*g*np.sin(thetanaught)*np.cos(thetanaught-phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))\
    (mtheta+mphi)*Ltheta*(deltatheta**2)*np.sin(thetanaught-phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos*(thetanaught-phinaught))))\
    -(mtheta+mphi)*g*np.sin(phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))

    return dy



#initial conditions
m1=1
m2=1
L1=1
L2=1

M=(m1,m2)
L=(L1,L2)
phi1naught=np.array(np.pi/2)
phi2naught=np.array(np.pi/2)
phi1dotnaught=np.array(0)
phi2dotnaught=np.array(0)

fnaught=(phi1naught,phi1dotnaught,phi2naught,phi2dotnaught)


t=np.linspace(0,20,100001)

f=scipy.integrate.odeint(doublepend,fnaught,t,args=(L,9.81,M))

我意识到我不需要多次导入 numpy。我正在将这些函数设计成一个小型函数库,以供以后用作示例。

【问题讨论】:

  • 您不需要在所有函数中都导入 numpy,您已经在模块级别导入了它。
  • 请在您的问题中包含您的整个堆栈跟踪。
  • 那是很多密集的代码。我们确实需要查看完整的堆栈跟踪,您可以将其放在代码块中以保留格式。如果代码是专注于您的问题的minimal reproducible example,您的问题会好得多。
  • 你真的应该自己寻找明显的错别字。在您的代码中查找此位 *np.cos*。看起来不太对,是吗?如果您查看从前一行到该行的中断,您会发现右括号紧跟在左括号之后。这对您有什么建议?
  • 将您的代码分解成小块,并确保每个小块都能正常工作。将复杂的公式分解为更简单的术语。将变量名称分配给更简单的术语,并根据这些变量来表达复杂的公式。编写一个运行的简单示例,然后逐步修改它(检查它是否在每次修改后运行)。通过这样做,您将避免当前看到的许多错误。

标签: python numpy scipy


【解决方案1】:

我们需要查看一些堆栈,但即便如此,也可能很难确定哪个变量出现问题。

该错误意味着某些代码正在尝试执行x(args) 之类的操作,即使用x 作为函数。但与预期相反x 是一个数字,而不是一个函数。

解决方案是确定导致问题的变量,并将其追溯到您的代码。

odeint 的输入看起来不错:

odeint(doublepend,fnaught,t,args=(L,9.81,M))

其中只有第一个,doublepend 必须是一个函数。似乎是这样,但我会在 odeint 通话之前检查一下。

fnaught 是初始条件。您似乎创建了一个值元组。我会创建一个数组,但我认为odeint 会自动完成。

t 是一个数组

args 得到一个数字元组。这些将传递给您的doublepend

doublepend(fnaught, 0, L, 9.81, M) 会运行吗?它返回什么?

另一个答案认为问题在于doublepend 函数。堆栈跟踪可以清楚地说明这一点,上面的测试计算也是如此。


In [13]: doublepend(fnaught, 0, L, 9.81, M)
1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-f8aa70c3f962> in <module>()
----> 1 doublepend(fnaught, 0, L, 9.81, M)

<ipython-input-10-3c95347f392b> in doublepend(y, tim, Langles, g, Mangles)
     13     #phi solved
     14     dy[2]=deltaphi
---> 15     dy[3]=mphi*(deltaphi**2)*np.sin(thetanaught-phinaught)/(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught)))    +(mtheta+mphi)*g*np.sin(thetanaught)*np.cos(thetanaught-phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))    (mtheta+mphi)*Ltheta*(deltatheta**2)*np.sin(thetanaught-phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos*(thetanaught-phinaught))))    -(mtheta+mphi)*g*np.sin(phinaught)/(Lphi*(mtheta+mphi*(1-np.cos(thetanaught-phinaught)*np.cos(thetanaught-phinaught))))
     16 
     17     return dy

TypeError: 'numpy.float64' object is not callable

好的 - 错误出现在那个又长又丑的 dy[3] 表达式中。分解它,找出你在哪里使用 () 来表示数字而不是函数。

这看起来很可疑

np.cos(thetanaught-phinaught))))   (mtheta+mphi)

np.cos() 计算为一个数字,而下一个 (methata...) 看起来像一个函数调用。如果我删除dy[3] 的最后两行,它就会运行。该行继续处缺少某些内容。

【讨论】:

  • 你完全正确。虽然还有一个错别字,但最大的问题是我在dy[3]的第二行开头疏忽了+
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 2020-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多