I have a machine learning algorithm which involves a series of steps, such as cleaning data, preparing training data etc. Each step is stored in a separate method of a python class. I'm wondering what the best practice way to structure my class is so that the steps are automatically executed upon class instantiation.
Here's what I've done (the code is illustrative, but this approach works on the real algorithm). It feels a little clunky. Is there a more elegant way?
class Kaggle():
"""
An algorithm
"""
def __init__( self ):
self.bar = 1
def step_one( self, some_text_data ):
self.bar = 1 ** 2
# Do some data cleaning
# return processed data
def step_two( self ):
foo = step_one(baz)
# do some more processing
def step_three( self ):
bar = step_two()
# output results
def run( self ):
self.step_one()
self.step_two()
self.step_three()
if __name__ == "__main__":
kaggle = Kaggle()
kaggle.run()
If your goal is for the object to be "automatically executed upon class instantiation", just put self.run()
in the init:
def __init__(self):
self.bar = 1
self.run()
Put all the calls in your __init__
method. Is this not what you wanted to achieve? You could add a flag with a default value, that allows you to not run the tests if you want.
def __init__( self, runtests=True ):
self.bar = 1
if runtests:
self.step_one()
self.step_two()
self.step_three()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With