我在图表中显示数据有问题。出现了图形框架,但没有看到任何图形。你能帮忙吗?
我确保了x轴的尺寸和数据是一样的.我根本找不出为什么我没有得到一个图形作为回报。
先谢谢你。
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
n = 1000
theta = 0.8
d = np.sqrt(1-theta**2)
def p(x,y):
"Stochastic kernel for the TAR model"
return norm().pdf((y-theta*np.abs(x))/d)/d
Z = norm().rvs(n)
X = np.empty(n)
for t in range(n-1):
X[t+1] = theta*np.abs(X[t])+d*Z[t+1]
n = len(X)
X = X.reshape((n, 1))
ys = np.linspace(-3,3,200)
k = len(ys)
ys = ys.reshape((1,k))
v = p(X,ys)
kernel = np.mean(v, axis=0)
h = len(kernel)
kernel = kernel.reshape((1,h))
fig, ax = plt.subplots(figsize=(10,7))
ax.plot(ys,kernel, 'b-', lw=2,alpha=0.6, label='look ahead estimate')
plt.show()发布于 2017-11-11 16:27:47
问题是,通过将两个一维数组( ys和kernel )分别重塑为1xk或1xh数组,就可以得到二维数组,其中一维数组是1,plot函数显然只遍历第一维,这就是为什么图中没有显示任何内容。我可以想出两个简单的方法来解决这个问题:
kernel和ys..。继续你的代码..。ys = np.linspace(-3,3,200) k= len(ys) #ys =ys.reshape(1,k)v= p(X,ys)核= np.mean(v,axis=0) h=len(核)#核=kernel.reshape(1,h)无花果,ax = plt.subplots(figsize=(10,7)) ax.plot(ys,内核,'b-',lw=2,alpha=0.6,label=‘前瞻性估计’) plt.show()我希望这能解决你的问题。
来理解为什么你仍然要重塑X:
让我们首先从维度的角度理解您的函数p(x,y):
def p(x,y):
"Stochastic kernel for the TAR model"
"""If x is not reshaped, you substract two one-dimensional arrays from each other,
which have not the same dimensions (dim(x) == 1000, dim(y) == 200 in your case).
This throws an error.
If you reshape X before passing to this function, the y array is substracted
element-wise by each of the values of X, which gives you a matrix with dimension
dim(x) x dim(y).
"""
return norm().pdf((y-theta*np.abs(x))/d)/d为了说明这里发生了什么,在维度上:
>>> X = np.array([[1], [2], [3], [4]])
>>> Y = np.array([1, 2, 3])
>>> Y-X
array([[ 0, 1, 2],
[-1, 0, 1],
[-2, -1, 0],
[-3, -2, -1]])现在我们来看看p(x,y)返回的矩阵会发生什么
kernel与np.mean(v, axis=0)的计算(其中v是从p(X,ys)返回的矩阵)的工作原理是,np.mean迭代矩阵v的直线并计算矩阵中每个“线向量”的平均值。这为您提供了一个一维数组(ys维度),您可以在ys上绘制该数组。
https://stackoverflow.com/questions/47238870
复制相似问题