def fit_one_category(self, dataset, category=None, verbose=False): train = self.scale(dataset) as_windows_train = get_windowed_ts(train, window_size=self.window_size) train_X, train_y = split_history_and_current(as_windows_train) train_X = train_X.reshape(train_X.shape[0], train_X.shape[1], 1) # Get model definition model = self.lstm_forecast_model(window_size=self.window_size, verbose=verbose) # Fit model start = time.time() history = model.fit( train_X, train_y, batch_size=max(self.batch_size, len(train_X)), epochs=self.epochs, validation_split=0.1, callbacks=[EarlyStopping(patience=4)], ) print("> Compilation Time : ", time.time() - start) self.model[category] = model
def predict_one_category(self, X, category): test = self.scale(X) as_windows_test = get_windowed_ts(test, window_size=self.window_size) test_X, test_y = split_history_and_current(as_windows_test) test_X = test_X.reshape(test_X.shape[0], test_X.shape[1], 1) preds = self.model[category].predict(test_X) from sklearn.metrics import mean_squared_error # clear session clear_session() del self.model actual = test_y prediction = preds mse = mean_squared_error(actual, prediction) print("MSE=" + str(mse)) results = pd.DataFrame({ "date": X.index[self.window_size:], "prediction": preds.squeeze(), "actual": test_y, "diff": test_y - preds.squeeze(), }) results["diff_rolling_std"] = ( results["diff"].rolling(self.lookback, min_periods=self.min_periods).std() ) # naive version where we look at the standard deviation of the # difference between predicted and actual in a previous window. results["prediction"] = np.where( results["diff"] > self.num_of_std * results["diff_rolling_std"], 1, 0) return results