Display inline images in a Jupyter notebook with Matplotlib

Today I was working with the MNIST handwritten digits data and wanted to display a few images in a Jupyter notebook. After looking at PIL, then Pillow, I found the easiest way is to just use Matplotlib. Here’s a code snippet that let’s you do it. from matplotlib.pyplot import imshow %matplotlib inline w, h = 20, 20 image = X[0].reshape(w,h).T #assuming X[0] is of shape (400,) imshow(image, cmap='gray') Note: You may still need Pillow when working with Matplotlib. See explanation and requirements here: Image tutorial.

May 7, 2017 · David Hamann

Debugging Jupyter notebooks

While searching for ways to debug code in a Jupyter notebook, I found a lot of outdated posts. So I decided to quickly write up my findings. (Just show me the answer…) Let’s say we have this piece of code and we want to set a breakpoint between the original answer to the ultimate question of life, the universe and everything stored in answer and the addition to that answer: def add_to_life_universe_everything(x): answer = 42 # we want a breakpoint here answer += x return answer add_to_life_universe_everything(12) pdb The built-in Python debugger pdb works just fine in a Jupyter notebook. With import pdb; pdb.set_trace() we can enter the debugger and get a little interactive prompt. ...

April 22, 2017 · David Hamann

Sharing a VPN connection with another device on macOS Sierra/El Capitan

There are multiple reasons why you would want to share a VPN connection from your Mac with another device. Maybe you have to install a proprietary VPN client which does not run on your main computer or you just don’t want to run/install it there. Maybe your main computer doesn’t “comply” (i.e. you would need to install additional AV software – who wants that?). Or you simply need it for streaming video to some TV box. ...

April 19, 2017 · David Hamann

Resolving import issues when deploying Python code to AWS Lambda

AWS Lambda is Amazon’s “serverless” compute platform that basically lets you run code without thinking (too much) of servers. I used Lambda in the past, though only in the Node.js environment. Wanting to deploy my first Python function, I ran into a couple of problems. Deployment scenarios There are two deployment scenarios: Simple scenario Advanced scenario The simple scenario applies to you when your function code only requires the AWS SDK library (Boto 3) and no other external resources. Just use Lambda’s inline code editor and you are good to go. No need to read the rest of this article :-) ...

January 27, 2017 · David Hamann

Indexing: A few handy ways to access NumPy arrays

The following code snippets should serve as an (incomplete) cheat sheet for accessing NumPy arrays. All examples expect an import numpy as np. Basic access NumPy arrays can be accessed just like lists with array[start:stop:step] a = np.array([1,2,3,4], int) # => array([1, 2, 3, 4]) a[2] # => 3 a[:2] # => array([1, 2]) a[::2] # => array([1, 3]) When working with multidimensional arrays, a comma can be used to access values for the different axes: ...

January 16, 2017 · David Hamann