【问题标题】:Q2: AttributeError: 'builtin_function_or_method' object has no attribute 'size'Q2: AttributeError: 'builtin_function_or_method' 对象没有属性 'size'
【发布时间】:2017-07-22 02:39:45
【问题描述】:
Could anyone tell me why I'm getting the error type: AttributeError: 'builtin_function_or_method' object has no attribute

“大小”是 57 吗?
对于这个合成器:out=np.zeros((x.size,y.size))

import numpy as np
import sympy as sp
from numpy import exp,sqrt,pi
from sympy import Integral, log, exp, sqrt, pi
import math
from numpy import array
import matplotlib.pyplot as plt 
import scipy.integrate
from scipy.special import erf
from scipy.stats import norm, gaussian_kde
from quantecon import LAE
from sympy.abc import q
#from sympy import symbols
#var('q')
#q= symbols('q')

## == Define parameters == #
mu=80
sigma=20
b=0.2
Q=80
Q1=Q*(1-b)
Q2=Q*(1+b)
d = (sigma*np.sqrt(2*np.pi))
phi = norm()
n = 500

#Phi(z) = 1/2[1 + erf(z/sqrt(2))].

def p_k_positive(x, y):
   # x, y = np.array(x, dtype=float), np.array(y, dtype=float)
    Positive_RG = norm.pdf(x[:, None] - y[None, :]+Q1, mu, sigma)
    print('Positive_R = ', Positive_RG)
    return Positive_RG 

def p_k_negative(x, y):
   # x, y = np.array(x, dtype=float), np.array(y, dtype=float)
    Negative_RG = norm.pdf(x[:, None] - y[None, :]+Q2, mu, sigma) 
    print('Negative_RG = ', Negative_RG)
    return Negative_RG 

def p_k_zero(x, y):
   # x, y = np.array(x, dtype=float), np.array(y, dtype=float)
    Zero_RG = (1/(2*math.sqrt(2*math.pi)))*(erf((x[:, None]+Q2-mu)/(sigma*math.sqrt(2)))-erf((x[:, None]+Q1-mu)/(sigma*math.sqrt(2))))
    #Zero_RG =norm.pdf
    print('Zero_RG',Zero_RG)
    return Zero_RG

def myFilter(x,y):
    x, y = x.squeeze, y.squeeze
    out=np.zeros((x.size,y.size))
    xyDiff = x[:, None] - y[None, :]
    out=np.where(np.bitwise_and(y[None, :] > 0.0, xyDiff >= -Q1), p_k_positive(x, y), out) # unless the sum functions are different
    out=np.where(np.bitwise_and(y[None, :] < 0.0, x[:, None] >= -Q1), p_k_negative(x, y), out)
    out=np.where(np.bitwise_and(y[None, :] ==0.0, xyDiff >= -Q1), p_k_zero(x, y), out)
    return out


Z = phi.rvs(n)
X = np.empty(n)
for t in range(n-1):
    X[t+1] = X[t] + Z[t]
    #X[t+1] = np.abs(X[t]) + Z[t]
psi_est = LAE(myFilter, X)
k_est = gaussian_kde(X)

fig, ax = plt.subplots(figsize=(10,7))
ys = np.linspace(-200.0, 200.0, 200)
ax.plot(ys, psi_est(ys), 'g-', lw=2, alpha=0.6, label='look ahead estimate')
ax.plot(ys, k_est(ys), 'k-', lw=2, alpha=0.6, label='kernel based estimate')
ax.legend(loc='upper left')
plt.show()

【问题讨论】:

  • 你仍然没有调用myFilter,因为你没有给它任何参数。您将整个函数传递给LAE,除非这是预期的 - 谁知道它在做什么。
  • 前瞻估计器是 quentecon 的一个类。除非我理解错误,否则它只期望 (p,X) 构造。这是课程以及在几行内所做的事情。 quanteconpy.readthedocs.io/en/latest/_modules/quantecon/…。这里 p=`myFilter'
  • phi.rvs 来自哪里?它的输出是什么?
  • 我搞砸了。应该是x, y = x.squeeze(), y.squeeze()
  • 首先,非常感谢@DanielForsman 的持续帮助。我的代码是对此处提供的代码和此链接nbviewer.jupyter.org/github/QuantEcon/QuantEcon.applications/… 的练习 1 中的结果的改编。除了我有一个分段函数,我正在处理 /statd_solutions_py.ipynb .... phi = norm() 和 object .rvs 用于 python 中的连续随机变量。

标签: arrays numpy boolean compare logical-operators


【解决方案1】:

x, y = x.squeeze, y.squeeze

应该是

x, y = x.squeeze(), y.squeeze()

或者您正在尝试获取函数的size

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 2018-03-27
    • 2020-11-27
    • 2016-01-31
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多