Exemple #1
0
 def test_lookback_window_forecast_valueerror_during_predict(self):
     # Assert that (for LSTMForecast) ValueError is raised
     # in fit method if lookback_window >= number of readings (rows of X)
     X = np.random.random(size=(5, 2))
     for lookback_window in [5, 6]:
         model = KerasLSTMForecast(kind=lstm_autoencoder.lstm_model,
                                   lookback_window=lookback_window)
         with self.assertRaises(ValueError):
             model.fit(X)
def test_lookback_window_forecast_valueerror_during_fit(lookback_window):
    """
    Assert (for LSTMForecast) ValueError is raised
    in fit method if lookback_window >= number of readings (rows of X)
    """
    model = KerasLSTMForecast(kind=lstm_autoencoder.lstm_model,
                              lookback_window=lookback_window)
    with pytest.raises(ValueError):
        X = np.random.random(size=(5, 2))
        y = X.copy()
        model.fit(X, y)
    def test_lstmforecast_transform_output(self):
        # test for KerasLSTMForecast
        #  - test dimension of output
        #  - test that first half of output is testing data

        X_train = np.random.random(size=(5, 3))
        lookback_window = 3
        model = KerasLSTMForecast(kind=lstm_autoencoder.lstm_model,
                                  lookback_window=lookback_window)
        model = model.fit(X_train)
        X_test = np.random.random(size=(6, 3))
        out = model.transform(X_test)
        self.assertEqual(out.shape, (3, 6))
        self.assertEqual(out[:, :3].tolist(),
                         X_test[lookback_window:, :].tolist())
    def test_generate_window_lstm_forecast_output(self):
        # Check that right output is generated from generate_window (for KerasLSTMForecast)
        X = np.random.random(size=(5, 2))

        lookback_window = 3
        model = KerasLSTMForecast(kind=lstm_autoencoder.lstm_model,
                                  lookback_window=lookback_window)
        gen = model.generate_window(X)
        gen_out_1 = next(gen)
        gen_out_2 = next(gen)
        self.assertEqual(gen_out_1[0].tolist(), X[0:3].reshape(1, 3,
                                                               2).tolist())
        self.assertEqual(gen_out_2[0].tolist(), X[1:4].reshape(1, 3,
                                                               2).tolist())
        self.assertEqual(gen_out_1[1].tolist(), X[3].reshape(1, 2).tolist())
        self.assertEqual(gen_out_2[1].tolist(), X[4].reshape(1, 2).tolist())

        X = np.random.random(size=(4, 2))
        lookback_window = 2
        model = KerasLSTMForecast(kind=lstm_autoencoder.lstm_model,
                                  lookback_window=lookback_window)
        gen_no_y = model.generate_window(X, output_y=False)
        gen_no_y_out_1 = next(gen_no_y)
        gen_no_y_out_2 = next(gen_no_y)
        self.assertEqual(gen_no_y_out_1.tolist(), X[0:2].reshape(1, 2,
                                                                 2).tolist())
        self.assertEqual(gen_no_y_out_2.tolist(), X[1:3].reshape(1, 2,
                                                                 2).tolist())
Exemple #5
0
 def test_keras_forecast_reshapes_array(self):
     # Asserts that KerasLSTMForecast accepts an array of elements, which it will
     # reshape into the matrix of single elements it needs
     model = KerasLSTMForecast(kind=lstm_autoencoder.lstm_model)
     X = np.random.rand(100)
     model.fit(X)
     X = np.random.rand(100)
     model.predict(X)
def test_keras_forecast_reshapes_array():
    """
    Asserts KerasLSTMForecast accepts an array of elements, which it will
    reshape into the matrix of single elements it needs
    """
    model = KerasLSTMForecast(kind=lstm_autoencoder.lstm_model)
    X, y = np.random.rand(100), np.random.rand(100)
    model.fit(X, y)
    model.predict(X)