One minute
Refactoring machine learning code - namedtuple
Instead of using sometimes confusing indexing in your code, use a namedtuple instead. It’s backwards compatible, so you can still use the index, but you can make your code much more readable.
This is especially helpful when you transform between PIL
and numpy
based code, where PIL
uses a column, row notation while numpy
uses a row, column notation.
Let’s consider this piece of code where we want to get the pixel locations of several points which are in the numpy
format:
To me this code is hard to read, because it’s not clear what these point[1]
and point[0]
refer to and also why is point[1]
before point[0]
?
Let’s use a namedtuple to make this much easier to read:
Note that you can still use point[0]
= point.row
and point[1]
= point.column
.
refactoring development python machinelearning
Development Machine Learning Refactoring
187 Words
May 13, 2021