[python]如何随着时间的推移使用间歇性改装重现回溯测试的动画?
· 收录于 2023-09-24 05:36:07 · source URL
问题详情
我正在试验一维时间序列数据,并尝试通过动画在GoogleColab笔记本中对我自己的数据重现以下方法。
这是关于重现这种方法的动画:使用间歇性改装进行回测,已在skforecast package.
使用间歇改装进行回溯测试
模型每 n 次预测迭代重新训练一次。当再训练和预测的频率不同时,通常使用此方法。它可以使用固定或滚动原点实现,从而灵活地使模型适应新数据。
以下是我的代码:
我目前的输出是:
from matplotlib.animation import FuncAnimation, PillowWriter
ani.save("TLI.gif", dpi=100, writer=PillowWriter(fps=50))
预期产出:
最佳回答
您有几个问题:
- 测试补丁正在滑动bc,您将
帧=np.arange
传递到FuncAnimation
中 - 在
update()
中,逻辑是片状的。
请检查我介绍的变化,它将帮助您更好地了解进一步的发展方向。
# Define the delay for refitting the model
REFIT_DELAY = 5 # steps
TRAIN_WIDTH = 25
TEST_WIDTH = 10
Y_LIM = 300
def init():
rects = [Rectangle((0, 0), TRAIN_WIDTH, Y_LIM, alpha=0.3, facecolor='green'),
Rectangle((0 + TRAIN_WIDTH, 0), TEST_WIDTH, Y_LIM, alpha=0.3, facecolor='blue')]
patches = []
for rect in rects:
patches.append(ax.add_patch(rect))
return patches
# Initialize the start points for training and test data
train_data_start = 0
test_data_start = TRAIN_WIDTH
# Initialize the counter for refitting the model
refit_counter = 0
def update(x_start):
global test_data_start, refit_counter
patches[1].xy = (x_start + test_data_start, 0)
# Check if the model needs to be refitted
if refit_counter == REFIT_DELAY:
patches[0].set_width(x_start + test_data_start)
refit_counter = 0
# Increase the counter for refitting the model
refit_counter += 1
return patches
# Create "Train" and "Test" areas
patches = init()
ani = FuncAnimation(
fig,
update,
# frames=np.arange(0, df.shape[0] - TRAIN_WIDTH - TEST_WIDTH), # All starting points
frames=np.linspace(0, 250 - TEST_WIDTH, 15),
interval=300,
blit=True
)
HTML(ani.to_html5_video())