Exemple #1
0
def retrain(session):
    '''
    Retrains a model using validated samples and original training data if retrain_check() evaluates
    to True.
    '''
    retrain = retrain_check(session)
    if retrain:
        logger.info("Smartie is retraining a model!")
        attachments = fetch_validated_attachments(session)
        X, y = train.prepare_samples(attachments)
        results, score, best_estimator, params = train.train(
            X,
            y,
            weight_classes=True,
            n_iter_search=150,
            score='roc_auc',
            random_state=123)
        logger.info("Smartie is done retraining a model!")
        last_score = fetch_last_score(session)
        better_model = True if last_score < score else False
        if better_model:
            train.pickle_model(best_estimator)
            logger.info("Smartie has pickled the new model!")
        else:
            pass
        insert_model(session, results, params, score)
    else:
        logger.info("Smartie decided not to retrain a new model!")
 def test_fetch_last_score(self):
     results = {'c': 'd'}
     params = {'a': 'b'}
     score = .99
     with session_scope(self.dal) as session:
         insert_model(session, results=results, params=params, score=score)
     with session_scope(self.dal) as session:
         score = fetch_last_score(session)
     result = score
     expected = .99
     self.assertEqual(result, expected)
 def test_insert_model(self):
     results = {'c': 'd'}
     params = {'a': 'b'}
     score = .99
     with session_scope(self.dal) as session:
         insert_model(session, results=results, params=params, score=score)
     result = []
     with session_scope(self.dal) as session:
         models = session.query(Model).all()
         for m in models:
             model = object_as_dict(m)
             model.pop('create_date')
             result.append(model)
     expected = [{
         'id': 1,
         'results': results,
         'params': params,
         'score': score
     }]
     self.assertCountEqual(result, expected)