某些模型建模过程需要建立轨迹矩阵,代码如下:
import numpy as np
import matplotlib.pyplot as plt
N = 30
A = np.arange(0,N)
L = 10
K = N-L+1
#建立L*K的时滞矩阵
X = np.column_stack([A[i:i+L] for i in range(0,K)])
print(X)
#画出时滞矩阵图
ax = plt.matshow(X)
plt.xlabel("$L$-Lagged Vectors")
plt.ylabel("$K$-Lagged Vectors")
plt.colorbar(ax.colorbar, fraction=0.025)
ax.colorbar.set_label("$F(t)$")
plt.title("The Trajectory Matrix for A");
打印出的X结果为:
时滞矩阵图:
总结:用到了numpy的 column_stack功能,和matplotlib的matshow功能