20
CONTENTS
[0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
[0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
[0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
Here is one I haven’t covered yet. The function numpy.asarray() will convert its argument to an ndar-
ray if it isn’t already one. If it is, the data is unchanged. This is a handy way to write a function that can
accept either Python lists or ndarrays, and it is very efficient if the type is already ndarray as nothing new
is created.
In [20]: def one_tenth(x):
x = np.asarray(x)
return x / 10.
print(one_tenth([1, 2, 3])) # I work!
print(one_tenth(np.array([4, 5, 6]))) # so do I!
[0.1 0.2 0.3]
[0.4 0.5 0.6]
0.8 Companion Software
I am writing an open source Bayesian filtering Python library called FilterPy. Installation instructions are
given above.
FilterPy is hosted GitHub at (https://github.com/rlabbe/filterpy) but the pip installed version should
serve your needs.
Code that is specific to the book is stored with the book in the subdirectory /kf_book. It contains code
for formatting the book. It also contains python files with names like xxx_internal.py. I use these to store
functions that are useful for a specific chapter. This allows me to hide Python code that is not particularly
interesting to read - I may be generating a plot or chart, and I want you to focus on the contents of the chart,
not the mechanics of how I generate that chart with Python. If you are curious as to the mechanics of that,
just go and browse the source.
Some chapters introduce functions that are useful for the rest of the book. Those functions are initially
defined within the Notebook itself, but the code is also stored in a Python file in /kf_book that is imported
if needed in later chapters. I do document when I do this where the function is first defined, but this is still
a work in progress. I try to avoid this because then I always face the issue of code in the directory becoming
out of sync with the code in the book. However, Jupyter Notebook does not give us a way to refer to code
cells in other notebooks, so this is the only mechanism I know of to share functionality across notebooks.
There is an undocumented directory called /experiments. This is where I write and test code prior to
putting it in the book. There is some interesting stuff in there, and feel free to look at it. As the book evolves
I plan to create examples and projects, and a lot of this material will end up there. Small experiments will
eventually just be deleted. If you are just interested in reading the book you can safely ignore this directory.
The directory /kf_book contains a css file containing the style guide for the book. The default look and
feel of Jupyter Notebook is rather plain. I have followed the examples set by books such as Probabilistic
Programming and Bayesian Methods for Hackers [2]. I have also been very influenced by Professor Lorena
Barba’s fantastic work, available here [3]. I owe all of my look and feel to the work of these projects.
0.9 Thoughts on Python and Coding Math
Most Kalman filtering and other engineering texts are written by mathematicians or academics. When there
is software (rarely), it is not production quality. Take Paul Zarchan’s book Fundamentals of Kalman Filtering
as an example. This is a fantastic book which belongs in your library, and is one of the few books that