【发布时间】:2019-05-26 23:43:48
【问题描述】:
我正在使用Kalman Filters 进行实验。我已经创建了一个非常小的时间序列数据,其中包含如下格式的三列。由于我无法在 stackoverflow 上附加文件,因此在此处附加了完整的数据集以实现可重复性:
time X Y
0.040662 1.041667 1
0.139757 1.760417 2
0.144357 1.190104 1
0.145341 1.047526 1
0.145401 1.011882 1
0.148465 1.002970 1
.... ..... .
我已经阅读了Kalman Filter 中的the documetation 并设法进行了简单的线性预测,这是我的代码
import matplotlib.pyplot as plt
from pykalman import KalmanFilter
import numpy as np
import pandas as pd
df = pd.read_csv('testdata.csv')
print(df)
pd.set_option('use_inf_as_null', True)
df.dropna(inplace=True)
X = df.drop('Y', axis=1)
y = df['Y']
estimated_value= np.array(X)
real_value = np.array(y)
measurements = np.asarray(estimated_value)
kf = KalmanFilter(n_dim_obs=1, n_dim_state=1,
transition_matrices=[1],
observation_matrices=[1],
initial_state_mean=measurements[0,1],
initial_state_covariance=1,
observation_covariance=5,
transition_covariance=1)
state_means, state_covariances = kf.filter(measurements[:,1])
state_std = np.sqrt(state_covariances[:,0])
print (state_std)
print (state_means)
print (state_covariances)
fig, ax = plt.subplots()
ax.margins(x=0, y=0.05)
plt.plot(measurements[:,0], measurements[:,1], '-r', label='Real Value Input')
plt.plot(measurements[:,0], state_means, '-b', label='Kalman-Filter')
plt.legend(loc='best')
ax.set_xlabel("Time")
ax.set_ylabel("Value")
plt.show()
输出如下图
正如我们在情节中看到的那样,模式似乎被很好地捕捉到了。我们如何统计测量均方根误差 (RMSE)(上图中红线和蓝线之间的误差距离)?任何帮助,将不胜感激。
【问题讨论】:
-
要在
x和y两个列表之间找到 RMSE,您可以使用np.sqrt(np.mean((x-y)**2))。 -
@TylerChen,这给出了
NaN值先生。 -
是数组中的所有条目的常规数字,还是有一些
inf或NaN? -
@TylerChen,是的,他们是常规号码,先生。为了可重复性,我在帖子中包含了小数据集。它只有大约 400 行,它不会花费您太多时间重新运行并检查它是否适合您。谢谢。
-
你能贴出你想找到 RMSE 的两个数组吗?我没有安装
pykalman。
标签: python-3.x numpy kalman-filter mean-square-error pykalman