Q2: AttributeError: 'builtin_function_or_method' object has no attribute 'size'

笑着哭i 提交于 2019-12-12 01:37:47

问题


Could anyone tell me why I'm getting the error type: AttributeError: 'builtin_function_or_method' object has no attribute

'size' in like 57?
for this synthax: 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()

回答1:


x, y = x.squeeze, y.squeeze

Should be

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

or you're trying to take the size of a function.



来源:https://stackoverflow.com/questions/42554270/q2-attributeerror-builtin-function-or-method-object-has-no-attribute-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!