Python trick : generate parameter grid for ML model fine tuning


2025, Jan 12 edited
tags: python ML code 


For a Machine Learning project, it's crucial step to fine tune the model parameters in order to otbain better performance.
Depending on the type of model and implementation library, parameters can affect differently the models' behaviour. It is often necessary to manually test each parameter with different values, which can lead to the identification of the best parameter configuration. The set of parameters that achieves a higher accuracy and a better generalization.

A few examples of python solutions aimed to help in this task, by simplifying and automating the generation of a parameter grid, taking into account all the combinations that should be tested.




Python trick : generate parameter grid for ML model fine tuning - the code



Here's the code for the grid parameter generator using the meshgrid function from the numpy python library. A not well known function that is quite helpful for this task.




Python trick : generate parameter grid for ML model fine tuning - part 2



A nice feature can be added to this code snippet, in the python Jupyter notebook. It's the ability to follow the progression of the script on the parameter grid.
For this I use the nice looking progress bar provided by the tqdm python library. As their website says "Instantly make your loops show a smart progress meter". See below an illustration of its use on a 16 steps iteration, even if it doesn't represent the usual size of the parameter grids I usually test (8000 in general) ?. Inside the tqdm loop (line 15 in the illustration below) you can add any processing you want for each iteration, and will be able to know how fast it's progressing. You'll no longer wonder whether the notebook is blocked or has crashed.




Python trick : generate parameter grid for ML model fine tuning - alternative code



Below the code for an alternative version of the parameter grid generator. This implementation relies on the itertools library and in particular its product function. Below the implementation of the alternative version.




Python trick : best option on performance



Next, a comparison is made to check which of the two implementations is the best in terms of performance. For this purpose, timeit library will be use to measure the time taken by each implementation launched separately multiple times. Each implementation will be launched 10 000 times, and the total execution time in seconds will be returned.




Python trick : best option on performance, the benchmark result



The observed difference between the 2 implementations is quite material. Running 10 000 times the implementation based on np.meshgrid takes 11 seconds, 10 times less than running 10 000 times the code that relies on itertools library.

The fuel of coding inspiration:music, travel, photography and whatever drives creativity to code.

You might also like