#!/usr/bin/env python # coding: utf-8 # In[1]: # Setup logging from timeseria import logger logger.setup(level='INFO') # Set default plot type as image import os os.environ["DEFAULT_PLOT_TYPE"] = "image" # ⚠️ In this notebook, plots are configured to be rendered as images because otherwise they won't display correctly on GitHub or when the notebook is loaded (usually because not [trusted](https://stackoverflow.com/questions/44943646/jupyter-notebook-not-trusted)). To get interactive plots, remove the line above (or change it to "interactive") and re-run the notebook. # # # Temperature and humidity forecasting with LSTM # # This notebook showcases the LSTM forecatser of [Timeseria](https://github.com/sarusso/Timeseria). # # Let's start by loading an example time series, and resample it to one hour: # In[2]: from timeseria import TEST_DATASETS_PATH from timeseria.datastructures import TimeSeries timeseries = TimeSeries.from_csv(TEST_DATASETS_PATH + 'humitemp_long.csv').resample('1h') # Have a look at the time series # In[3]: timeseries # Plot the time series # In[4]: timeseries.plot() # ## Model fit, usage and evaluation # # Instantiate and fit the model: # In[5]: from timeseria.models import LSTMForecaster forecaster = LSTMForecaster(window=24, neurons=64, features=['values', 'hours']) forecaster.fit(timeseries, epochs=30, verbose=True, reproducible=True) # Call the `predict()` function of the model. This returns key-value data with the prediction values # In[6]: forecaster.predict(timeseries) # The timeseries on which to call the predict has to be at least of 24 hours (the window length) # In[7]: try: forecaster.predict(timeseries[0:23]) except Exception as e: print(e) # The `forecats()` method returns not only data but also their data points, with the timestamp # In[8]: forecaster.forecast(timeseries) # The `apply()` method allows instead to directly apply the model on a time series (by combining the forecasts with the real observations). # In[9]: forecaster.apply(timeseries, steps=12)[-1000:].plot() # Evaluate the forecaster. By default, forecasters get evaluated with RMSE and MAE error metrics: # In[10]: forecaster.evaluate(timeseries) # For more advanced modes of evaluation, including using other error metrics, plotting the error distribution and/or the predictions, check out the "Forecasting - Advanced Evaluation" notebook.