Random nanom errors while importing matplotlib.pyplot

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

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

Full code here . Let me emphasize that it works correctly before I import; it prints an array of 100 x 2 numbers that are believable. After adding the import, without even using anything from pyplot, all the lines after the specific nan line. Figures that are not nan are the same as before.

My first thought was that it might be a name conflict, but it is not. You can easily see that the code does not have a name like โ€œpltโ€ , in addition, it is not consistent with the behavior described below.

The number of nan lines is different when executing a file from Sublime Text instead of the command line or when I add import matplotlib as mpl before importing pyplot. Again, non- nan numbers are the same as in a properly functioning version.

The debugging attempt only confused me. I added print statements to the problematic iteration of the main loop, which at first gave only nan matrices. However, when I added another expression, print yt , printing matrices with nan unexpectedly had not- nan values. In addition, moving the import sys statement before import numpy as np changes the number of nan lines. During experiments on these lines, I noticed that during the execution of the same file, the values โ€‹โ€‹changed several times (by a lot, for example, from 77 to 3.32686992e + 297), and with further re-execution, back to the original values, randomly oscillating between these two exits. There is no saved state, file operations consist of only one call to np.loadtxt .

Additional information that may help: I โ€‹โ€‹have Python 2.7.6 and Ubuntu 14.04, although the behavior is similar on someone else's machine with Python 2.7.8 and spyder.

What are the possible sources of this behavior? Right now, I think either witchcraft, matching mysterious hardware crashes on our computers, or an evil virus designed to disrupt Python programmers.

+5
source share
1 answer

I cannot reproduce the errors that you see *, so it is difficult for me to determine the cause. Having said that, one obvious source of numerical instability in your code is the matrix inverse 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.

You can replace your call with np.linalg.inv np.linalg.solve as follows:

 # 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, BT).T 

See if this helps your stability issues.


Update

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 created against an older (and probably incompatible) version of numpy (how did you install these libraries?).

I would recommend you try uninstalling and reinstalling matplotlib.


* I tried using numpy v1.8.2 and v1.10.0.dev-8bcb756 related to OpenBLAS 0.2.12 compiled from source code or CBLAS standard library from Ubuntu repositories. I also tried using matplotlib v1.3.1 and v1.5.x.

+2
source

Source: https://habr.com/ru/post/1212825/


All Articles