我正在学习如何使用pykalman package。我在浏览使用机器人数据的Kalman EM示例时遇到了错误。 EM示例代码为here。机器人示例附带了生成数据的描述和代码。
当我运行此部分时:
# Learn good values for parameters named in `em_vars` using the EM algorithm
loglikelihoods = np.zeros(10)
for i in range(len(loglikelihoods)):
kf = kf.em(X=data.observations, n_iter=1)
loglikelihoods[i] = kf.loglikelihood(data.observations)
我得到了kf.loglikelihood(data.observations)函数的下面的错误,该函数表示不支持Masked Arrays。我应该使用什么作为kf.loglikelihood的参数呢?
C:\Users\Jenny\Anaconda3\lib\site-packages\pykalman-0.9.5-py3.5.egg\pykalman\standard.py in loglikelihood(self, X)
1472 loglikelihoods = _loglikelihoods(
1473 observation_matrices, observation_offsets, observation_covariance,
-> 1474 predicted_state_means, predicted_state_covariances, Z
1475 )
1476
C:\Users\Jenny\Anaconda3\lib\site-packages\pykalman-0.9.5-py3.5.egg\pykalman\standard.py in _loglikelihoods(observation_matrices, observation_offsets, observation_covariance, predicted_state_means, predicted_state_covariances, observations)
168 observation[np.newaxis, :],
169 predicted_observation_mean[np.newaxis, :],
--> 170 predicted_observation_covariance[np.newaxis, :, :]
171 )
172 return loglikelihoods
C:\Users\Jenny\Anaconda3\lib\site-packages\pykalman-0.9.5-py3.5.egg\pykalman\utils.py in log_multivariate_normal_density(X, means, covars, min_covar)
71 lower=True)
72 cv_log_det = 2 * np.sum(np.log(np.diagonal(cv_chol)))
---> 73 cv_sol = solve_triangular(cv_chol, (X - mu).T, lower=True).T
74 log_prob[:, c] = - .5 * (np.sum(cv_sol ** 2, axis=1) + \
75 n_dim * np.log(2 * np.pi) + cv_log_det)
C:\Users\Jenny\Anaconda3\lib\site-packages\scipy\linalg\basic.py in solve_triangular(a, b, trans, lower, unit_diagonal, overwrite_b, debug, check_finite)
156 """
157 a1 = _asarray_validated(a, check_finite=check_finite)
--> 158 b1 = _asarray_validated(b, check_finite=check_finite)
159 if len(a1.shape) != 2 or a1.shape[0] != a1.shape[1]:
160 raise ValueError('expected square matrix')
C:\Users\Jenny\Anaconda3\lib\site-packages\scipy\_lib\_util.py in _asarray_validated(a, check_finite, sparse_ok, objects_ok, mask_ok, as_inexact)
183 if not mask_ok:
184 if np.ma.isMaskedArray(a):
--> 185 raise ValueError('masked arrays are not supported')
186 toarray = np.asarray_chkfinite if check_finite else np.asarray
187 a = toarray(a)
ValueError: masked arrays are not supported
答案 0 :(得分:1)
我添加了以下行
observation=observation.filled()
就在pykalman.standard.py包文件的第168行之前,其中包含:
loglikelihoods[t] = log_multivariate_normal_density(
observation[np.newaxis, :],
predicted_observation_mean[np.newaxis, :],
predicted_observation_covariance[np.newaxis, :, :]
)
假设您有完整的观察结果,这个简单的解决方法将使loglikelihood()方法起作用。希望这在包的后续迭代中不再有必要。
答案 1 :(得分:-1)
在Github上提到了pykalman的问题,solution对我有效。
链接中提到的解决方案基本上是将第112行中的standard.py中的代码修改为173:
def _loglikelihoods(observation_matrices, observation_offsets,
for t in range(n_timesteps):
observation = observations[t]
if not np.any(np.ma.getmask(observation)):
+ observation_data = np.ma.getdata(observation)
observation_matrix = _last_dims(observation_matrices, t)
observation_offset = _last_dims(observation_offsets, t, ndims=1)
predicted_state_mean = _last_dims(
loglikelihoods[t] = log_multivariate_normal_density(
- observation[np.newaxis, :],
+ observation_data[np.newaxis, :],
predicted_observation_mean[np.newaxis, :],
predicted_observation_covariance[np.newaxis, :, :]
)