if y>0.0 and x -y>=-Q1: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
if y>0.0 and x -y>=-Q1: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我一直在努力让它工作一段时间,但仍然没有找到方法。我正在尝试计算分段高斯函数的前瞻估计密度。我正在尝试估计分段正态分布函数的平稳分布。有没有办法避免错误类型:
Error-type: the truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
例如 y=np.linspace(-200.0,200.0,100)
和 x = np,linspace(-200.0,200.0,100)
。然后验证下面代码中所述的条件?
import numpy as np
import sympy as sp
from numpy import exp,sqrt,pi
from sympy import Integral, log, exp, sqrt, pi
import math
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(x, y):
# x, y = np.array(x, dtype=float), np.array(y, dtype=float)
Positive_RG = norm.pdf(x-y+Q1, mu, sigma)
print('Positive_R = ', Positive_RG)
Negative_RG = norm.pdf(x-y+Q2, mu, sigma)
print('Negative_RG = ', Negative_RG)
pdf_0= (1/(2*math.sqrt(2*math.pi)))*(erf((x+Q2-mu)/(sigma*np.sqrt(2)))-erf((x+Q1-mu)/(sigma*np.sqrt(2))))
Zero_RG =norm.pdf
print('Zero_RG',Zero_RG)
print ('y',y)
if y>0.0 and x -y>=-Q1:
#print('printA', Positive_RG)
return Positive_RG
elif y<0.0 and x -y>=-Q2:
#print('printC', Negative_RG)
return Negative_RG
elif y==0.0 and x >=-Q1:
#print('printB', Zero_RG)
return Zero_RG
return 0.0
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(p, 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()
查看边栏中的所有 ValueError
个问题????
当在标量布尔上下文中使用布尔数组时会产生此错误,例如 if
或 or/and
。
在此测试中尝试您的 y
或 x
,或者更简单的测试。在交互式 shell.
中进行实验
if y>0.0 and x -y>=-Q1: ....
if y>0:
(y>0.0) and (x-y>=10)
你的x
和y
都会产生这个错误。
另请注意,为了清楚起见,我编辑了您的问题。
错误以 quantecon.LAE(p, X)
开头,需要 向量化 函数 p
。您的函数未矢量化,这就是为什么其他一切都不起作用的原因。您复制了一些矢量化代码,但留下了很多东西作为 sympy
样式函数,这就是为什么 numpy 的人对您想要的东西感到困惑。
在这种情况下,"vectorized" 表示将两个长度为 n
的一维数组转换为二维 n x n
数组。在这种情况下,您不想 return 0.0
,您想要 return out
一个 2d ndArray,它在位置 out[i,j]
中具有值 0.0
,其中布尔掩码基于x[i], y[j]
的功能是错误的。
你可以通过广播来做到这一点:
def sum_function(x,y):
return x[:, None] + y[None, :] # or however you want to add them, broadcasted to 2D
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), sum_function(x, y), out) # unless the sum functions are different
out=np.where(np.bitwise_and(y[None, :] < 0.0, xyDiff >= -Q2), sum_function(x, y), out)
return out
我一直在努力让它工作一段时间,但仍然没有找到方法。我正在尝试计算分段高斯函数的前瞻估计密度。我正在尝试估计分段正态分布函数的平稳分布。有没有办法避免错误类型:
Error-type: the truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
例如 y=np.linspace(-200.0,200.0,100)
和 x = np,linspace(-200.0,200.0,100)
。然后验证下面代码中所述的条件?
import numpy as np
import sympy as sp
from numpy import exp,sqrt,pi
from sympy import Integral, log, exp, sqrt, pi
import math
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(x, y):
# x, y = np.array(x, dtype=float), np.array(y, dtype=float)
Positive_RG = norm.pdf(x-y+Q1, mu, sigma)
print('Positive_R = ', Positive_RG)
Negative_RG = norm.pdf(x-y+Q2, mu, sigma)
print('Negative_RG = ', Negative_RG)
pdf_0= (1/(2*math.sqrt(2*math.pi)))*(erf((x+Q2-mu)/(sigma*np.sqrt(2)))-erf((x+Q1-mu)/(sigma*np.sqrt(2))))
Zero_RG =norm.pdf
print('Zero_RG',Zero_RG)
print ('y',y)
if y>0.0 and x -y>=-Q1:
#print('printA', Positive_RG)
return Positive_RG
elif y<0.0 and x -y>=-Q2:
#print('printC', Negative_RG)
return Negative_RG
elif y==0.0 and x >=-Q1:
#print('printB', Zero_RG)
return Zero_RG
return 0.0
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(p, 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()
查看边栏中的所有 ValueError
个问题????
当在标量布尔上下文中使用布尔数组时会产生此错误,例如 if
或 or/and
。
在此测试中尝试您的 y
或 x
,或者更简单的测试。在交互式 shell.
if y>0.0 and x -y>=-Q1: ....
if y>0:
(y>0.0) and (x-y>=10)
你的x
和y
都会产生这个错误。
另请注意,为了清楚起见,我编辑了您的问题。
错误以 quantecon.LAE(p, X)
开头,需要 向量化 函数 p
。您的函数未矢量化,这就是为什么其他一切都不起作用的原因。您复制了一些矢量化代码,但留下了很多东西作为 sympy
样式函数,这就是为什么 numpy 的人对您想要的东西感到困惑。
在这种情况下,"vectorized" 表示将两个长度为 n
的一维数组转换为二维 n x n
数组。在这种情况下,您不想 return 0.0
,您想要 return out
一个 2d ndArray,它在位置 out[i,j]
中具有值 0.0
,其中布尔掩码基于x[i], y[j]
的功能是错误的。
你可以通过广播来做到这一点:
def sum_function(x,y):
return x[:, None] + y[None, :] # or however you want to add them, broadcasted to 2D
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), sum_function(x, y), out) # unless the sum functions are different
out=np.where(np.bitwise_and(y[None, :] < 0.0, xyDiff >= -Q2), sum_function(x, y), out)
return out