Exemple #1
0
    train_x, train_y, test_x, test_y = train_test_split(X, Y)
    # Getting our command line parameters
    name, epochs, batches, plot=get_params()
    # Do the training
    model, name, mp, history=train_model(name, train_x, train_y, epochs, batches)
    # Save models and the training history for later use
    mname='models/model-%s-%d-%d' % (name, epochs, batches)
    model.save(mname+'.h5')
    with open(mname+'-history.pickle', 'wb') as ms:
        pickle.dump(history.history, ms)
    print()
    print('Model and its history saved in %s*' % mname)
    title='%s (epochs=%d, batch_size=%d)' % (name, epochs, batches)
    # Test our model on both data that has been seen
    # (training data set) and unseen (test data set)
    print('Scores for %s' % title)
    train_score = model.evaluate(train_x, train_y, verbose=0)
    trscore='RMSE: %s MAPE: %.0f%%' % ("{:,.0f}".format(math.sqrt(train_score[0])), train_score[2])
    print('Train Score: %s' % trscore)
    test_score = model.evaluate(test_x, test_y, verbose=0)
    tscore='RMSE: %s MAPE: %.0f%%' % ("{:,.0f}".format(math.sqrt(test_score[0])), test_score[2])
    print('Test Score: %s' % tscore)
    # Get our baseline prediction
    # Our baseline is simply a mse of the difference between values from X and Y
    baseline=get_baseline()
    if plot == 'plot':
        train_plot, test_plot=prep_tt_for_plot(model, years, train_x, train_y, test_x, test_y)
        plotme(values, years, train_x=train_plot, test_x=test_plot, baseline=baseline[1], bttscore=baseline[3], trscore=trscore, tscore=tscore, title=title)
    elif plot == 'ploth':
        plothist(history)
def get_baseline(all=True):
    years, past_values, values = get_data()
    train_x, train_y, test_x, test_y = train_test_split(past_values, values)

    pred = train_x
    train_score = mean_squared_error(train_y, pred)
    print('Baseline Training Score: RMSE: %s' %
          '{:,.0f}'.format(math.sqrt(train_score)))

    pred = test_x
    test_score = mean_squared_error(test_y, pred)
    print('Baseline Test Score: RMSE: %s' %
          '{:,.0f}'.format(math.sqrt(test_score)))

    bttscore = 'RMSE: %s/%s' % ('{:,.0f}'.format(
        math.sqrt(train_score)), '{:,.0f}'.format(math.sqrt(test_score)))

    if all:
        plot_y = [i for i in train_y] + [x for x in test_y]
        plot_pred = [i for i in train_x] + [x for x in test_x]
    else:
        plot_y = [None for i in train_y] + [x for x in test_y]
        plot_pred = [None for i in train_x] + [x for x in test_x]
    return np.array(plot_y), np.array(plot_pred), np.array(years), bttscore


if __name__ == '__main__':
    py, pp, y = get_baseline(all=True)
    plotme(years=y, values=py, baseline=pp)