Programming Machine Learning: a tip and a gotcha

@wasshuber Hi Chris,
Interesting idea! It’s not something we have done before, but I’ll ask.

@wasshuber so the answer for now is that we agree that there would be some great benefits to publishing Jupyter notebooks, but it’s not something we’re currently set up to do. We will keep it in mind for the future though!

Here is a nice efficient numpy code for one-hot-encoding. It is a one-liner:

np.eye(n_classes)[Y.flatten()]

The identity matrix gives one all the different one-hot encoded vectors needed. After that we simply integer index into the array with the labels Y to assemble the full array. If Y is already an array we need to flatten it. That is all. I think this is probably the most efficient way to one-hot-encode in Python. Somehow I enjoy making the code more efficient and more compact.

1 Like

This is a very nice one-liner, vastly more elegant than the 6-7 lines I have in the book for this task. I don’t know if I’d put this in the book, because explaining the Numpy operations involved takes a short while–so I’d probably go for explicit over elegant. Nonetheless, this is really neat.

For a book, it will depend on where you want to put the focus, on understanding the algorithm, or on coding it efficiently. There is no right or wrong. But at least from the angle I view your book, which does put an emphasis on the coding with Python, I do think going deeper into Numpy and being able to effectively use it, is part of it. At least that has been my journey so far. The better I understand Numpy the easier it has become to translate machine learning algorithms into code.

Part of the reason why I am forced to code it as efficiently as possible is that I am working on a really old computer. I can’t afford to program it inefficiently otherwise running the code would take a lot longer. It is the old story that a lack of resources makes one more creative and innovative because the straight forward simple solution is not feasible. From a coding perspective it is a really interesting challenge to be resource-constrained and still make it work. That requires efficient implementation.

I believe, a shorter code also makes it easier to work with in the long run. For my little educational deep learning library, I am aiming for a Python implementation that is short and efficient, hopefully, an implementation that can’t be much improved on :slight_smile:

1 Like