The simple elegance of `.get_new_params()`

Reading through documentation for a programming function to find what arguments it likes can be tedious, especially since many of the possible options either don’t work together or aren’t documented. Something I have been including on most of my newer classes is a static method called get_new_params(method='random') which does pretty much exactly as it says, producing a dictionary of keyword arguments that are valid.

In basic functions, there is usually only one right parameter set to use, so having such a function is probably not particularly helpful. But in data science, where a model can have many possible arguments, it is invaluable, setting up the basis for a grid search, which is what I first started adding this function for a few years ago. But I don’t think this function is limited to data science. It can be very helpful to know what a function is capable of doing, so running a loop through a randomly generated selection of parameters is arguably the single best way to get a practical feel for the outcome. Having the programmer build in functionality to make this easier is as important as writing documentation – more so perhaps, as it more directly shows rather than tells.

Another advantage of this approach is that the get_new_params outputs can be readily updated as the function has new parameters added or removed. Less worry about hard-coding options, it will always produce valid function calls. The method arg can be used to get_new_params if some variation is needed. Most commonly I have something like “fast” (for fast options only) or “deep” (for all possible options) available.

Leave a Comment

Your email address will not be published. Required fields are marked *