【问题标题】:TypeError: unbound method deriv() must be called with NormalGraph instance as first argument (got ndarray instance instead)TypeError:必须使用 NormalGraph 实例作为第一个参数调用未绑定的方法 deriv()(改为获取 ndarray 实例)
【发布时间】:2018-12-16 03:28:08
【问题描述】:

我不断收到此错误。我不确定出了什么问题,有人可以帮忙吗?

TypeError: unbound method deriv() must be called with NormalGraph instance as 
first argument (got ndarray instance instead) 

这是产生错误的代码。当程序不在一个类中时它可以工作,但是当它被放入一个类时它似乎不起作用。这是不断给出错误的类:

class NormalGraph:
def __init__(self, N, R0, I0, S0, beta, gamma, mu):
    self.N=N
    self.R0=R0
    self.I0=I0
    self.S0=S0
    self.beta=beta
    self.gamma=gamma
    self.mu=mu

def deriv(y, t, N, beta, gamma, mu):
    S, I, R = y
    dSdt = mu * N -(beta * S * I) / N - mu * S
    dIdt = beta * S * I / N - gamma * I - mu * I
    dRdt = gamma * I- mu*R
    return dSdt, dIdt, dRdt

def plotGraph(self, S0, I0, R0, beta, gamma, N, mu):

    y0 = S0, I0, R0
    t = np.linspace(0, 160, 160*2)

    ret = odeint(NormalGraph.deriv, y0, t, args=( N, beta, gamma, mu))
    S, I, R = ret.T

    fig = plt.figure(facecolor='w')
    ax = fig.add_subplot(111, axis_bgcolor='#dddddd', axisbelow=True)
    ax.plot(t, S/1000, 'b', alpha=0.5, lw=2, label='Susceptible')
    ax.plot(t, I/1000, 'r', alpha=0.5, lw=2, label='Infected')
    ax.plot(t, R/1000, 'g', alpha=0.5, lw=2, label='Recovered with immunity')
    ax.set_xlabel('Time /days')
    ax.set_ylabel('Number (1000s)')
    ax.set_ylim(0,1.2)
    ax.yaxis.set_tick_params(length=0)
    ax.xaxis.set_tick_params(length=0)
    ax.grid(b=True, which='major', c='w', lw=2, ls='-')
    legend = ax.legend()
    legend.get_frame().set_alpha(0.5)
    for spine in ('top', 'right', 'bottom', 'left'):
        ax.spines[spine].set_visible(False)
    plt.show()

N = 1000
R0=0
I0=1
S0 = N - I0 - R0
beta=0.2
gamma = 0.1
mu=6
P=0.3
p=0.3
graph=NormalGraph(N, R0, I0, S0, beta, gamma, mu)
graph.plotGraph( S0, I0, R0, beta, gamma, N, mu)

【问题讨论】:

  • 您能分享一下derivNormalGraphVaccination 的定义吗?这段代码不足以理解问题。
  • 很抱歉。

标签: python


【解决方案1】:

odeint(NormalGraph.deriv, ... 调用错误。您没有在 NormalGraph 中将 deriv 声明为静态方法。从您的复制/粘贴中不确定派生词是在 NormalGraph 类中还是在它之外。确保您知道在哪里调用 deriv 函数以及它是如何定义的。

【讨论】:

  • 谢谢,这很有用:)
猜你喜欢
  • 2017-12-09
  • 2015-12-01
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 2018-05-10
  • 2017-04-03
相关资源
最近更新 更多