PyKalman 是一个 Python 库,用于卡尔曼滤波和状态估计
pip install pykalman
import numpy as np
from pykalman import KalmanFilter
import matplotlib.pyplot as plt
kf = KalmanFilter(transition_matrices=[1],
observation_matrices=[1],
initial_state_mean=0,
initial_state_covariance=1,
observation_covariance=1,
transition_covariance=0.01)
# 假设你有一个名为 data 的 NumPy 数组,其中包含你的时间序列数据
states_pred, _ = kf.filter(data)
# 假设你想预测接下来的 10 个时间步
predictions = []
last_state = states_pred[-1]
for _ in range(10):
last_state, _ = kf.filter_update(last_state, None)
predictions.append(last_state[0])
plt.plot(data, label='Original Data')
plt.plot(states_pred, label='Filtered Data')
plt.plot(range(len(data), len(data) + len(predictions)), predictions, label='Predictions')
plt.legend()
plt.show()
领取专属 10元无门槛券
手把手带您无忧上云