我尝试使用卡尔曼滤波对我的一维数据。因此,假设我有以下数据集:
Variable
250.1
248.5
262.3
265.3
270.2我确实知道我的数据中存在噪声,因此,我想使用卡尔曼滤波来清除这些数据。哪种方法能给我带来最有效的结果?
我运行以下代码:
from pykalman import KalmanFilter
import numpy as np
kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]],
observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
measurements = np.asarray([(250.1),(248.5),(262.3),(265.3), (270.2)])
kf = kf.em(measurements, n_iter=5)
(filtered_state_means, filtered_state_covariances)=kf.filter(measurements)
(smoothed_state_means, smoothed_state_covariances)=kf.smooth(measurements)正如您所看到的,我尝试使用pykalman,但是我无法安装这个模块。我尝试使用easy_install pykalman方向,错误是无效语法。另一个问题是,我有一个庞大的数据集,所以我的变量列中有超过10万行。所以,我不能一个一个地写出所有的观察结果。
发布于 2018-05-08 15:46:46
为了安装pykalman,我使用了:
pip install pykalman --user--用户标志安装到我的主目录,避免使用sudo安装.有人告诉我枕叶不见了,所以我也安装了它。在项目github页面上有一个依赖库列表,因此可能会要求您安装其中任何一个库。
您正在使用单个值为您的每一个读数。大多数例子有更多的,例如每次阅读的位置和速度。为了得到你所提供的过渡矩阵和观察矩阵的图,我在你的每个度量中增加了第二个假读数'1‘。下面的木星笔记本脚本将产生一个图表,但输出很差,因为矩阵值需要为您的数据集进行调整。
%matplotlib inline
from pykalman import KalmanFilter
import numpy as np
kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]],
observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
# measurements = np.asarray([(250.1),(248.5),(262.3),(265.3), (270.2)])
measurements = np.array([[250.1,1],[248.5,1],[262.3,1],[265.3,1], [270.2,1]])
kf = kf.em(measurements, n_iter=5)
filtered_state_estimates = kf.filter(measurements)[0]
(smoothed_state_estimates, smoothed_state_covariances)=kf.smooth(measurements)
# draw estimates
pl.figure()
lines_true = pl.plot(measurements, color='b')
lines_filt = pl.plot(filtered_state_estimates, color='r')
lines_smooth = pl.plot(smoothed_state_estimates, color='g')
pl.legend((lines_true[0], lines_filt[0], lines_smooth[0]),
('true', 'filt', 'smooth'),
loc='lower right'
)
pl.show()对于您建议的数据集,一种快速和更简单的产生过滤输出的方法是使用一个减α滤波器。有关此类型筛选器的更多详细信息,请查看此链接:http://stats.stackexchange.com/questions/44650/a-simpler-way-to-calculate-exponentially-weighted-moving-average
https://stackoverflow.com/questions/50217553
复制相似问题