def forecast(self): from openweatherdata import series_to_list from sktime.forecasting.theta import ThetaForecaster import numpy forecaster = ThetaForecaster(sp=48) forecaster.fit(self.pd_past5days) self.pd_predictions = forecaster.predict(numpy.arange(1, 48)) self.predictions = series_to_list(self.pd_predictions) self.next(self.plot)
def test_forecaster_with_initial_level(): y = np.log1p(load_airline()) y_train, y_test = temporal_train_test_split(y) fh = np.arange(len(y_test)) + 1 f = ThetaForecaster(initial_level=0.1, sp=12) f.fit(y_train) y_pred = f.predict(fh=fh) np.testing.assert_allclose(y_pred, y_test, rtol=0.05)
def test_predictive_performance_on_airline(): y = np.log1p(load_airline()) y_train, y_test = temporal_train_test_split(y) fh = np.arange(len(y_test)) + 1 f = ThetaForecaster(sp=12) f.fit(y_train) y_pred = f.predict(fh=fh) # Performance on this particular dataset should be reasonably good. np.testing.assert_allclose(y_pred, y_test, rtol=0.05)
def test_pred_errors_against_y_test(fh): y = load_airline() y_train, y_test = temporal_train_test_split(y) f = ThetaForecaster() f.fit(y_train, fh) y_pred = f.predict(return_pred_int=False) errors = f._compute_pred_errors(alpha=0.1) if isinstance(errors, pd.Series): errors = [errors] # make iterable y_test = y_test.iloc[check_fh(fh) - 1] for error in errors: assert np.all(y_test > y_pred - error) assert np.all(y_test < y_pred + error)
def test_pred_errors_against_y_test(fh): y = load_airline() y_train, y_test = temporal_train_test_split(y) f = ThetaForecaster() f.fit(y_train, fh) y_pred = f.predict(return_pred_int=False) intervals = f.compute_pred_int(y_pred, [0.1]) y_test = y_test.iloc[check_fh(fh) - 1] # Performance should be good enough that all point forecasts lie within the # prediction intervals. for ints in intervals: assert np.all(y_test > ints["lower"]) assert np.all(y_test < ints["upper"])
def test_forecaster_with_initial_level(): """Check prediction performance on airline dataset. Performance on this dataset should be reasonably good. Raises ------ AssertionError - if point forecasts do not lie close to the test data """ y = np.log1p(load_airline()) y_train, y_test = temporal_train_test_split(y) fh = np.arange(len(y_test)) + 1 f = ThetaForecaster(initial_level=0.1, sp=12) f.fit(y_train) y_pred = f.predict(fh=fh) np.testing.assert_allclose(y_pred, y_test, rtol=0.05)
def main(): df = datasets.load_airline( ) #Univariate, monthly records from 1949 to 60 (144 records) y_train, y_test = temporal_train_test_split( df, test_size=36) #36 months for testing forecaster = NaiveForecaster( strategy='seasonal_last', sp=12 ) #model strategy: last, mean, seasonal_last. sp=12months (yearly season) forecaster.fit(y_train) #fit fh = np.arange(1, len(y_test) + 1) #forecast horizon: array with the same lenght of y_test y_pred = forecaster.predict(fh) #pred forecaster2 = AutoARIMA(sp=12, suppress_warnings=True, trace=1) forecaster2.fit(y_train) y_pred2 = forecaster2.predict(fh) forecaster3 = ExponentialSmoothing(trend='add', damped='True', seasonal='multiplicative', sp=12) forecaster3.fit(y_train) y_pred3 = forecaster3.predict(fh) forecaster4 = ThetaForecaster(sp=12) forecaster4.fit(y_train) y_pred4 = forecaster4.predict(fh) forecaster5 = EnsembleForecaster([ ('NaiveForecaster', NaiveForecaster(strategy='seasonal_last', sp=12)), ('AutoARIMA', AutoARIMA(sp=12, suppress_warnings=True)), ('Exp Smoothing', ExponentialSmoothing(trend='add', damped='True', seasonal='multiplicative', sp=12)), ('Theta', ThetaForecaster(sp=12)) ]) forecaster5.fit(y_train) y_pred5 = forecaster5.predict(fh) plot_ys(y_train, y_test, y_pred, y_pred2, y_pred3, y_pred4, y_pred5, labels=[ 'Train', 'Test', 'Naive Forecaster', 'AutoARIMA', 'Exp Smoothing', 'Theta', 'Ensemble' ]) plt.xlabel('Months') plt.ylabel('Number of flights') plt.title( 'Time series of the number of international flights in function of time' ) plt.show() print('SMAPE Error for NaiveForecaster is:', 100 * round(smape_loss(y_test, y_pred), 3), '%') print('SMAPE Error for AutoARIMA is:', 100 * round(smape_loss(y_test, y_pred2), 3), '%') print('SMAPE Error for Exp Smoothing is:', 100 * round(smape_loss(y_test, y_pred3), 3), '%') print('SMAPE Error for Theta is:', 100 * round(smape_loss(y_test, y_pred4), 3), '%') print('SMAPE Error for Ensemble is:', 100 * round(smape_loss(y_test, y_pred5), 3), '%')
#pip install sktime import numpy as np from sktime.datasets import load_airline from sktime.forecasting.theta import ThetaForecaster from sktime.forecasting.model_selection import temporal_train_test_split from sktime.performance_metrics.forecasting import smape_loss y = load_airline() y_train, y_test = temporal_train_test_split(y) fh = np.arange(1, len(y_test) + 1) # forecasting horizon forecaster = ThetaForecaster(sp=12) # monthly seasonal periodicity forecaster.fit(y_train) y_pred = forecaster.predict(fh) smape_loss(y_test, y_pred)