This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Class Optimization | |
| # ... | |
| def forecast_with_lag_features(self, test_loader, batch_size=1, n_features=1, n_steps=100): | |
| test_loader_iter = iter(test_loader) | |
| predictions = [] | |
| *_, (X, y) = test_loader_iter | |
| y = y.to(device).detach().numpy() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def plot_dataset_with_forecast(df, df_forecast, title): | |
| data = [] | |
| value = go.Scatter( | |
| x=df.index, | |
| y=df.value, | |
| mode="lines", | |
| name="values", | |
| marker=dict(), | |
| text=df.index, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def format_forecasts(forecasts, index, scaler): | |
| preds = np.concatenate(forecasts, axis=0).ravel() | |
| df_forecast = pd.DataFrame(data={"prediction": preds}, index=index) | |
| df_result = df_forecast.sort_index() | |
| df_result = inverse_transform(scaler, df_result, [["prediction"]]) | |
| return df_result | |
| df_forecast = format_forecasts(forecasts, index, scaler) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| forecasts = opt.forecast_with_predictors(forecast_loader, | |
| batch_size=1, | |
| n_features=input_dim, | |
| n_steps=100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| X_forecast, y_forecast = feature_label_split(df_forecast, 'value') | |
| scaler = get_scaler('minmax') | |
| X_train_arr = scaler.fit_transform(X_train) | |
| X_forecast_arr = scaler.transform(X_forecast) | |
| y_train_arr = scaler.fit_transform(y_train) | |
| y_forecast_arr = scaler.transform(y_forecast) | |
| forecast_dataset = TensorDataset(torch.Tensor(X_forecast_arr), | |
| torch.Tensor(y_forecast_arr)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| df_forecast['value'] = 0 | |
| df_forecast= (df_forecast | |
| .assign(hour = df_forecast.index.hour) | |
| .assign(day = df_forecast.index.day) | |
| .assign(month = df_forecast.index.month) | |
| .assign(day_of_week = df_forecast.index.dayofweek) | |
| .assign(week_of_year = df_forecast.index.week) | |
| ) | |
| df_forecast = onehot_encode_pd(df_forecast, ['month','day','day_of_week','week_of_year']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_datetime_index(df): | |
| return ( | |
| pd.to_datetime(df.index[-1]) | |
| + (pd.to_datetime(df.index[-1]) - pd.to_datetime(df.index[-2])), | |
| pd.to_datetime(df.index[-1]) - pd.to_datetime(df.index[-2]), | |
| ) | |
| start_date, freq = get_datetime_index(y_test) | |
| index = pd.date_range(start=start_date, freq=freq, periods=100) | |
| df_forecast = pd.DataFrame(index=index) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Optimization: | |
| # ... | |
| def forecast_with_predictors( | |
| self, forecast_loader, batch_size=1, n_features=1, n_steps=100 | |
| ): | |
| """Forecasts values for RNNs with predictors and one-dimensional output | |
| The method takes DataLoader for the test dataset, batch size for mini-batch testing, | |
| number of features and number of steps to predict as inputs. Then it generates the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.linear_model import LinearRegression | |
| def build_baseline_model(df, test_ratio, target_col): | |
| X, y = feature_label_split(df, target_col) | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| X, y, test_size=test_ratio, shuffle=False | |
| ) | |
| model = LinearRegression() | |
| model.fit(X_train, y_train) | |
| prediction = model.predict(X_test) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import plotly.offline as pyo | |
| def plot_predictions(df_result, df_baseline): | |
| data = [] | |
| value = go.Scatter( | |
| x=df_result.index, | |
| y=df_result.value, | |
| mode="lines", | |
| name="values", |
NewerOlder