Easier Experimenting in Python

When we work on a machine learning project, we quite often need to experiment with multiple alternatives. Some features in Python allow us to try out different options without much effort. In this tutorial, we are going to see some tips to make our experiments faster.

After finishing this tutorial, you will learn:

  • How to leverage a duck-typing feature to easily swap functions and objects
  • How making components into drop-in replacements for  each other can help experiments run faster

Kick-start your project with my new book Python for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

Easier experimenting in Python. Photo by Jake Givens. Some rights reserved

Overview

This tutorial is in three parts; they are:

  • Workflow of a machine learning project
  • Functions as objects
  • Caveats

Workflow of a Machine Learning Project

Consider a very simple machine learning project as follows:

This is a typical machine learning project workflow. We have a stage of preprocessing the data, then training a model, and afterward, evaluating our result. But in each step, we may want to try something different. For example, we may wonder if normalizing the data would make it better. So we may rewrite the code above into the following:

So far, so good. But what if we keep experimenting with different datasets, different models, or different score functions? Each time, we keep flipping between using a scaler and not would mean a lot of code change, and it would be quite easy to make mistakes.

Because Python supports duck typing, we can see that the following two classifier models implemented the same interface:

Therefore, we can simply select between these two version and keep everything intact. We can say these two models are drop-in replacements for each other.

Making use of this property, we can create a toggle variable to control the design choice we make:

By toggling the variable USE_SCALER between True and False, we can select whether a scaler should be applied. A more complex example would be to select among different scaler and the classifier models, such as:

A complete example is as follows:

If you go one step further, you may even skip the toggle variable and use a string directly for a quick experiment:

Functions as Objects

In Python, functions are first-class citizens. You can assign functions to a variable. Indeed, functions are objects in Python, as are classes (the classes themselves, not only incarnations of classes). Therefore, we can use the same technique as above to experiment with similar functions.

The above is similar to calling np.random.normal(size=(10,5)), but we hold the function in a variable for the convenience of swapping one function with another. Note that since we call the functions with the same argument, we have to make sure all variations will accept it. In case it is not, we may need some additional lines of code to make a wrapper. For example, in the case of generating Student’s t distribution, we need an additional parameter for the degree of freedom:

This works because in the above, np.random.normalnp.random.uniform, and t_wrapper as we defined, are all drop-in replacements of each other.

Want to Get Started With Python for Machine Learning?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Caveats

Machine learning differs from other programming projects because there are more uncertainties in the workflow. When you build a web page or build a game, you have a picture in your mind of what to achieve. But there is some exploratory work in machine learning projects.

You will probably use some source code control system like git or Mercurial to manage your source code development history in other projects. In machine learning projects, however, we are trying out different combinations of many steps. Using git to manage the different variations may not fit, not to say sometimes may be overkill. Therefore, using a toggle variable to control the flow should allow us to try out different things faster. This is especially handy when we are working on our projects in Jupyter notebooks.

However, as we put multiple versions of code together, we made the program clumsy and less readable. It is better to do some clean-up after we confirm what to do. This will help us with maintenance in the future.

Further reading

This section provides more resources on the topic if you are looking to go deeper.

Books

Summary

In this tutorial, you’ve seen how the duck typing property in Python helps us create drop-in replacements. Specifically, you learned:

  • Duck typing can help us switch between alternatives easily in a machine learning workflow
  • We can make use of a toggle variable to experiment among alternatives

Get a Handle on Python for Machine Learning!

Python For Machine Learning

Be More Confident to Code in Python

...from learning the practical Python tricks

Discover how in my new Ebook:
Python for Machine Learning

It provides self-study tutorials with hundreds of working code to equip you with skills including:
debugging, profiling, duck typing, decorators, deployment, and much more...

Showing You the Python Toolbox at a High Level for
Your Projects


See What's Inside

2 Responses to Easier Experimenting in Python

  1. Avatar
    Amol Goel February 25, 2022 at 2:38 pm #

    i will surely give it a try. Tahnk you

    • Avatar
      James Carmichael February 26, 2022 at 12:33 pm #

      You are very welcome Amol!

Leave a Reply