【问题标题】:Kalman Filtering_1-dimensional_Python卡尔曼滤波_一维_Python
【发布时间】:2018-10-17 10:58:48
【问题描述】:

我尝试对我的一维数据使用 卡尔曼滤波。所以,假设我有以下数据集:

 Variable
 250.1
 248.5
 262.3
 265.3
 270.2

我确实知道我的数据中有噪音,因此我想通过使用 Kalman 过滤来清理这些数据。哪种方式可以为我产生最有效的结果?

我运行以下代码:

 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方向,错误是invalid syntax。另一个问题是,我有一个庞大的数据集,所以我的变量列中有超过十万行。所以,我不能一一写下所有的观察结果。

【问题讨论】:

  • 那么你的问题是什么?同时显示一些代码。
  • 我想通过使用 python 的卡尔曼滤波从我的数据中去除噪声。我的问题是如何通过使用 python 对我的数据应用卡尔曼滤波。我遵循上述链接中的代码:stackoverflow.com/questions/43377626/…。我无法得到任何结果。首先,我无法安装 pykalman。其次,我有大量的行。我不能一一包含这些值。
  • 所以你根本没有python代码?
  • 我有。正如我所说,我使用了上述问题中的代码。我在我的问题中解释说我尝试使用 pykalman 并获得无效的语法。如果我没有代码,我怎么会得到这个错误?
  • 如果你有代码,为什么不能发布呢?

标签: python pandas numpy kalman-filter


【解决方案1】:

要安装我使用的 pykalman:

pip install pykalman --user

--user 标志安装到我的主目录,避免使用 sudo 进行安装。有人告诉我 scipy 不见了,所以我也安装了它。在项目 github 页面上有一个依赖库列表,因此可能会要求您安装其中任何一个。

您对每个读数都使用单个值。大多数示例不止于此,例如每个读数的位置和速度。为了用您提供的转换和观察矩阵绘制一些东西,我在您的每个测量值中添加了第二个虚假读数“1”。以下 Jupyter 笔记本脚本将生成一个绘图,但输出很差,因为需要针对您的数据集调整矩阵值。

%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()

对于您建议的数据集,产生过滤输出的一种快速且更简单的方法是使用减一 alpha 过滤器。有关此类过滤器的更多详细信息,请查看此链接: http://stats.stackexchange.com/questions/44650/a-simpler-way-to-calculate-exponentially-weighted-moving-average

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多