Ejemplo n.º 1
0
 def _get_or_create_model(self, ticker):
     log = logging.getLogger()
     model_filename = self.get_model_filename_from_ticker(ticker)
     model = get_model_from_bucket(model_filename, self.get_bucket_name())
     if model is None:
         log.warning(f'training model for {ticker}')
         model = self._model_creator.fit(ticker)
         with open(model_filename, 'wb') as f:
             joblib.dump(model, f)
         upload_file_to_bucket(model_filename, self.get_bucket_name())
     return model
 def _get_or_create_model(self, ticker):
     log = logging.getLogger()
     #Sets the filename of the model as ticker.pkl
     model_filename = self.get_model_filename_from_ticker(ticker)
     #Sets model to the saved model or none if it doesn't exist
     model = get_model_from_bucket(model_filename, self.get_bucket_name())
     if model is None:
         log.warning(f'training model for {ticker}')
         #Here is where we fit the model if it doesn't already exist
         model = self._model_creator.fit()
         with open(model_filename, 'wb') as f:
             joblib.dump(model, f)
         #Where we upload and save the new model
         upload_file_to_bucket(model_filename, self.get_bucket_name())
     return model
Ejemplo n.º 3
0
def get_or_build_model(X_train, y_train, model_name, rebuild_model=False):
    """
        Get or build the model of the specific sector name with all the data of all tickers
    """
    model_filename = f'{model_name}.pkl'
    model = get_model_from_bucket(model_filename, bucket_name)

    if rebuild_model or not model:

        model = RandomForestClassifier(n_estimators=para_estimator,
                                       max_depth=para_max_depth,
                                       min_samples_leaf=para_min_leaf,
                                       oob_score=True,
                                       random_state=para_random_state)

        model.fit(X_train, y_train)

        with open(model_filename, 'wb') as f:
            joblib.dump(model, f)
        upload_file_to_bucket(model_filename, bucket_name)

    return model