导入matplotlib.pyplot时出现随机Nan错误 [英] Random nan errors when importing matplotlib.pyplot

查看:150
本文介绍了导入matplotlib.pyplot时出现随机Nan错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用numpy实现一个卡尔曼滤波器.它工作正常,但当我导入matplotlib.pyplot以可视化结果时除外:

I am implementing a Kalman filter in numpy. It works fine, except for when I import matplotlib.pyplot to visualize the result:

import numpy as np 
import matplotlib.pyplot as plt # adding this line breaks things
import sys

完整代码位于此处.让我强调一下,在导入之前,它似乎可以正常工作;它会打印出一个100 x 2的合理数字数组.添加导入后,即使不使用pyplot的任何内容,特定行之后的所有行均为nan.不是nan的数字与以前相同.

The complete code is here. Let me emphasize that it seems to work correctly before I do the import; it prints a 100 x 2 array of numbers that are plausible. After adding the import, even without using anything from pyplot, all rows after a specific row are nan. The numbers that are not nan are the same as before.

我首先想到的可能是名称冲突,但事实并非如此.您可以轻松地看到代码中没有任何名为"plt"的东西,此外,它没有与下面描述的行为保持一致.

My first thought was that it might be a name conflict, but it's not. You can easily see that the code doesn't have anything named "plt", besides, it's not consistent with the behavior described below.

当我从Sublime Text执行文件而不是从命令行执行文件时,或者在pyplot导入之前添加import matplotlib as mpl时,nan行的数量不同.同样,非nan数字与功能正常的版本相同.

The number of nan lines differs when I execute the file from Sublime Text instead of from the command line or when I add import matplotlib as mpl before the pyplot import. Again, non-nan numbers are the same as in the correctly functioning version.

仅尝试调试使我更加困惑.我将打印语句添加到有问题的主循环迭代中,该循环最初仅提供了nan矩阵.但是,当我再添加一条语句print yt时,打印为nan的矩阵突然没有-nan值.同样,在import numpy as np之前移动import sys语句将更改nan行的数量.在沿着这条线进行实验的过程中,我观察到多次执行同一文件时,发生了变化(变化很大,例如从77变为3.32686992e + 297),并在进一步重复执行后返回到原始值,在这两个输出之间随机振荡.没有保存状态,文件操作仅包含一次对np.loadtxt的调用.

Trying to debug only made me more confused. I added print statements to the problematic iteration of the main loop, which at first gave only nan matrices. When I added one more statement, print yt, however, printing the matrices that were nan suddenly had not-nan values. Also, moving the import sys statement before the import numpy as np one changes the number of nan rows. During experiments along these lines, I observed that when executing the same file several times, the values changed (by a lot, e.g. from 77 to 3.32686992e+297), and upon further repeated execution back to the original values, oscillating randomly between these two outputs. There is no saved state, file operations consist only of one call to np.loadtxt.

更多信息可能会有所帮助:我拥有Python 2.7.6和Ubuntu 14.04,尽管在其他人的使用Python 2.7.8和spyder的计算机上,行为类似.

Further information that might help: I have Python 2.7.6 and Ubuntu 14.04, although on someone else's computer with Python 2.7.8 and spyder the behavior is similar.

此行为的可能来源是什么?现在,我在考虑是巫术,两台计算机上偶然的神秘硬件故障,还是旨在挫败Python程序员的邪恶病毒.

What could be possible sources for this behavior? Right now, I'm thinking either witchcraft, coincidential mysterious hardware failures on both our computers, or an evil virus designed to frustrate Python programmers.

推荐答案

我无法重现您看到的错误*,因此我很难查明原因.话虽如此,代码中数字不稳定性的一个明显来源就是对

I can't reproduce the errors you're seeing*, so it's hard for me to pinpoint the cause. Having said that, one obvious source of numerical instability in your code is the matrix inversion operation on line 39.

在实践中,很少有需要反转矩阵的情况.特别是,您应该不要使用矩阵求逆来求解系统线性方程组-改为使用因式分解总是更快,并且在数值上更稳定.

In practice there are very few situations where you need to invert a matrix. In particular, you should never use matrix inversion to solve systems of linear equations - it is always faster and more numerically stable to use factorization instead.

您可以将np.linalg.inv的调用替换为 np.linalg.solve 像这样:

You can replace your call to np.linalg.inv with np.linalg.solve like this:

# aux_k = np.linalg.inv(psi.dot(sigmatt1[t]).dot(psi.T) + rmat)
# k[t] = sigmatt1[t].dot(psi.T).dot(aux_k)

A = psi.dot(sigmatt1[t]).dot(psi.T) + rmat
B = sigmatt1[t].dot(psi.T)
k[t] = np.linalg.solve(A, B.T).T

看看这是否有助于解决稳定性问题.

See if that helps with your stability issues.

您在上面的评论中提到您的numpy.__version__ == '1.8.2',但您的matplotlib.__version__numpy__ == '1.5'.这可能意味着matplotlib是针对较旧(且可能不兼容)的numpy构建的(您是如何安装这些库的?).

You mentioned in the comments above that your numpy.__version__ == '1.8.2', but your matplotlib.__version__numpy__ == '1.5'. This probably means that matplotlib was built against an older (and probably incompatible) version of numpy (how did you install these libraries?).

我建议您尝试删除并重新安装matplotlib.

I would recommend you try removing and reinstalling matplotlib.

*我尝试使用numpy v1.8.2和v1.10.0.dev-8bcb756,它们链接到从源代码编译的OpenBLAS 0.2.12或来自Ubuntu存储库的标准CBLAS lib.我还尝试了matplotlib v1.3.1和v1.5.x.

* I've tried using numpy v1.8.2 and v1.10.0.dev-8bcb756, linked against either OpenBLAS 0.2.12 compiled from source or the standard CBLAS lib from the Ubuntu repositories. I've also tried both matplotlib v1.3.1 and v1.5.x.

这篇关于导入matplotlib.pyplot时出现随机Nan错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆