mardi 1 janvier 2019

Incremental number of instantiated objects in incremental number of conditions

Describing my problem is a bit hard. Hope you will bear with me.

I know it sounds very specific but I tried so many ways but could not come up with anything.

I have this class LSTMClassifier() which has a class variable, instance variables and instance methods (such as fit and evaluate which return some numbers).

class LSTMClassifier():
    prev_params = {}
    def __init__(*some variables*):

        ** PUT SOME CODE HERE** 

    def fit(self, year = year):

        ** PUT SOME CODE HERE**

    def evaluate(self, test_year):

        ** PUT SOME CODE HERE**

In every year, .fit() and .evaluate() method evaluate the algorithm on that current year, when I call those methods. However, objects can be created in previous years.

In what I want to do, I always have three paths to take based on some conditions (Statement_1_1, Statement_1_2 and Statement_2). Those conditions are decided based on the results of .fit() and .evaluate() functions.

Because of that, every year, the number of possible paths I need to take increases by three times. I need to choose one of those paths by using a if statement in every year. Therefore, incrementing the number of instantiated objects messes things up because I have I have 10 years, starting from 2008 to 2017.

For example, until 2013, algorithm can always choose Statement_2 which means, until year = 2012, I will have objects named Classifier2008 and Classifier2009, meaning that my Classifier objects are instantiated in year = 2008 and year =2009, respectively. Therefore, I will compare the results of Classifier2008.evaluate() and Classifier2009.fit() for each year until 2013 and when the algorithm reaches to year = 2013 and chooses Statement 1_2, I need to compare the results of Classifier2009.evaluate() and Classifier2010.fit().

Unfortunately, the code I have cannot handle here, even though I tried to create a dictionary and save the objects to call them back later.

Classifier = {}

#2008
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("YEAR: 2008")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")

First_Classifier = LSTMClassifier(*some variables*)
Classifier[2008] = First_Classifier
Classifier.fit(year=2008)

#2009
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("YEAR: 2009")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")

print('~~~~~ Evaluate model on 2009 data ~~~~~')
Classifier[2008].evaluate(test_year=2009)

print("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \n")
Classifier[2009] = LSTMClassifier(*some variables*)
Classifier[2009].fit(year=2009)


all_years = list(range(2009,2017,1))
year=2009

while year in all_years:

    if Statement1

        if Statement_1_1:

            print('~~~~~ Evaluate model on {} data ~~~~~'.format(year + 1))
            Classifier[year - 1].evaluate(test_year = year + 1)

            print("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")

            Classifier[year] = LSTMClassifier(*some variables*)
            Classifier[year].fit(year=year + 1)   

        else: #Statement_1_2

            print('~~~~~ Evaluate model on {} data ~~~~~'.format(year + 1))
            Classifier[year].evaluate(test_year = year + 1)

            print("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")

            Classifier[year + 1] = LSTMClassifier(*some variables*)
            Classifier[year + 1].fit(year=year + 1)

    else: # Statement_2

        print('~~~~~ Evaluate model on {} data ~~~~~'.format(year + 1))
        Classifier[year - 1].evaluate(test_year = year + 1)

        print("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")

        Classifier[year] = LSTMClassifier(*some variables*)
        Classifier[year].fit(year=year + 1)

    year += 1

In order to make myself more clear, I will show a drawing of all possible paths to take:

enter image description here

Aucun commentaire:

Enregistrer un commentaire