def main(): from src.utils import data_utils from keras import layers from matplotlib import pyplot as plt x, y = data_utils.get_data_in_shape('EA', (['CPI'], ['CPI']), 1) x_train, x_val, x_test, = data_utils.train_val_test_split(x, 12, 12) y_train, y_val, y_test = data_utils.train_val_test_split(y, 12, 12) input = Input(batch_shape=(1, 1,)) layer = layers.Reshape((1, 1))(input) layer = layers.LSTM(10, stateful=True)(layer) # layer = layers.LSTM(10, return_sequences=False, stateful=True)(layer) layer = layers.Dense(1, activation='linear')(layer) model = RecurrentModel(inputs=input, outputs=layer) model.compile(optimizer='adam', loss='mse') model.fit(x_train, y_train, epochs=200, batch_size=1) pred = model.predict(x_val, batch_size=1) fcast = model.forecast(x_val, y_val, batch_size=1) from keras.utils.vis_utils import plot_model plot_model(model) model.summary() plt.plot(y_val.values) plt.plot(pred) plt.show() plt.plot(y_val.values) plt.plot(fcast) plt.show()
def persistence_mse(series): columns = ['train pred', 'val pred', 'test pred'] lossess = OrderedDict([(i, 0) for i in columns]) y_pred, y_true = data_utils.get_xy_data(series, 1) true_data = data_utils.train_val_test_split(y_true, 12, 12) pred_data = data_utils.train_val_test_split(y_pred, 12, 12) for pred, true, col in zip(pred_data, true_data, columns): lossess[col] = mse(true, pred) return pd.DataFrame(lossess, index=[0])
def set_data_params(self, **data_params): self.x, self.y = data_utils.get_data_in_shape(**data_params) if self.difference: self.x, self.y = self.transformer.fit(self.x, self.y) self.x_train, self.x_val, self.x_test, = data_utils.train_val_test_split(self.x, 12, 12) self.y_train, self.y_val, self.y_test = data_utils.train_val_test_split(self.y, 12, 12) self.data_params = data_params self.is_multioutput = len(data_params['vars'][1]) > 1 self.variables = data_params['vars'][1] self.is_data_set = True self.is_fitted = False
def one_one(): from src.utils import data_utils from keras import layers x, y = data_utils.get_data_in_shape('EA', (['CPI'], ['CPI']), 1) x_train, x_val, x_test, = data_utils.train_val_test_split(x, 12, 12) y_train, y_val, y_test = data_utils.train_val_test_split(y, 12, 12) input = Input(shape=(1,)) layer = Dense(1)(input) model = ForecastModel(inputs=input, outputs=layer) model.compile(optimizer='adam', loss='mse') print model.forecast(x_val, y_val)
def persistence_prediction(series): y_pred, y_true = data_utils.get_xy_data(series, 1) y_train, y_val, y_test = data_utils.train_val_test_split(y_pred, 12, 12) predictions = pd.concat([y_true, y_train, y_val, y_test], axis=1) predictions.columns = [ 'true values', 'train prediction', 'val prediction', 'test prediction' ] return predictions
def many_one(): from src.utils import data_utils from keras import layers x, y = data_utils.get_data_in_shape('EA', (['CPI', 'GDP'], ['CPI']), 2) x_train, x_val, x_test, = data_utils.train_val_test_split(x, 12, 12) y_train, y_val, y_test = data_utils.train_val_test_split(y, 12, 12) input1 = Input(shape=(2,)) input2 = Input(shape=(2,)) layer = layers.concatenate([input1, input2]) layer = Dense(1)(layer) model = ForecastModel(inputs=[input1, input2], outputs=layer) model.compile(optimizer='adam', loss='mse') print model.forecast(x_val, y_val)
def main(): data_params = OrderedDict() data_params['country'] = 'EA' data_params['vars'] = (['CPI', 'GDP'], ['CPI', 'GDP']) data_params['lags'] = (1, 1) tr = DifferenceTransformer() x, y = data_utils.get_data_in_shape(**data_params) x, y = tr.fit(x, y) x_train, x_val, x_test = data_utils.train_val_test_split(x, 12, 12) y_train, y_val, y_test = data_utils.train_val_test_split(y, 12, 12) curr_x = x_val cuur_y = y_val x_trans, y_trans = tr.transform(curr_x, cuur_y) y_orig = tr.inverse_transform(y_trans.values, cuur_y) print np.concatenate([cuur_y, y_orig], axis=1) print y_orig