CINXE.COM
Timeseries forecasting for weather prediction
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="Keras documentation"> <meta name="author" content="Keras Team"> <link rel="shortcut icon" href="https://keras.io/img/favicon.ico"> <link rel="canonical" href="https://keras.io/examples/timeseries/timeseries_weather_forecasting/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: Timeseries forecasting for weather prediction"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: Timeseries forecasting for weather prediction"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>Timeseries forecasting for weather prediction</title> <!-- Bootstrap core CSS --> <link href="/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom fonts for this template --> <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700;800&display=swap" rel="stylesheet"> <!-- Custom styles for this template --> <link href="/css/docs.css" rel="stylesheet"> <link href="/css/monokai.css" rel="stylesheet"> <!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-5DNGF4N'); </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-175165319-128', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Tag Manager --> <script async defer src="https://buttons.github.io/buttons.js"></script> </head> <body> <!-- Google Tag Manager (noscript) --> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5DNGF4N" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <!-- End Google Tag Manager (noscript) --> <div class='k-page'> <div class="k-nav" id="nav-menu"> <a href='/'><img src='/img/logo-small.png' class='logo-small' /></a> <div class="nav flex-column nav-pills" role="tablist" aria-orientation="vertical"> <a class="nav-link" href="/about/" role="tab" aria-selected="">About Keras</a> <a class="nav-link" href="/getting_started/" role="tab" aria-selected="">Getting started</a> <a class="nav-link" href="/guides/" role="tab" aria-selected="">Developer guides</a> <a class="nav-link" href="/api/" role="tab" aria-selected="">Keras 3 API documentation</a> <a class="nav-link" href="/2.18/api/" role="tab" aria-selected="">Keras 2 API documentation</a> <a class="nav-link active" href="/examples/" role="tab" aria-selected="">Code examples</a> <a class="nav-sublink" href="/examples/vision/">Computer Vision</a> <a class="nav-sublink" href="/examples/nlp/">Natural Language Processing</a> <a class="nav-sublink" href="/examples/structured_data/">Structured Data</a> <a class="nav-sublink active" href="/examples/timeseries/">Timeseries</a> <a class="nav-sublink2" href="/examples/timeseries/timeseries_classification_from_scratch/">Timeseries classification from scratch</a> <a class="nav-sublink2" href="/examples/timeseries/timeseries_classification_transformer/">Timeseries classification with a Transformer model</a> <a class="nav-sublink2" href="/examples/timeseries/eeg_signal_classification/">Electroencephalogram Signal Classification for action identification</a> <a class="nav-sublink2" href="/examples/timeseries/event_classification_for_payment_card_fraud_detection/">Event classification for payment card fraud detection</a> <a class="nav-sublink2" href="/examples/timeseries/timeseries_anomaly_detection/">Timeseries anomaly detection using an Autoencoder</a> <a class="nav-sublink2" href="/examples/timeseries/timeseries_traffic_forecasting/">Traffic forecasting using graph neural networks and LSTM</a> <a class="nav-sublink2 active" href="/examples/timeseries/timeseries_weather_forecasting/">Timeseries forecasting for weather prediction</a> <a class="nav-sublink" href="/examples/generative/">Generative Deep Learning</a> <a class="nav-sublink" href="/examples/audio/">Audio Data</a> <a class="nav-sublink" href="/examples/rl/">Reinforcement Learning</a> <a class="nav-sublink" href="/examples/graph/">Graph Data</a> <a class="nav-sublink" href="/examples/keras_recipes/">Quick Keras Recipes</a> <a class="nav-link" href="/keras_tuner/" role="tab" aria-selected="">KerasTuner: Hyperparameter Tuning</a> <a class="nav-link" href="/keras_hub/" role="tab" aria-selected="">KerasHub: Pretrained Models</a> <a class="nav-link" href="/keras_cv/" role="tab" aria-selected="">KerasCV: Computer Vision Workflows</a> <a class="nav-link" href="/keras_nlp/" role="tab" aria-selected="">KerasNLP: Natural Language Workflows</a> </div> </div> <div class='k-main'> <div class='k-main-top'> <script> function displayDropdownMenu() { e = document.getElementById("nav-menu"); if (e.style.display == "block") { e.style.display = "none"; } else { e.style.display = "block"; document.getElementById("dropdown-nav").style.display = "block"; } } function resetMobileUI() { if (window.innerWidth <= 840) { document.getElementById("nav-menu").style.display = "none"; document.getElementById("dropdown-nav").style.display = "block"; } else { document.getElementById("nav-menu").style.display = "block"; document.getElementById("dropdown-nav").style.display = "none"; } var navmenu = document.getElementById("nav-menu"); var menuheight = navmenu.clientHeight; var kmain = document.getElementById("k-main-id"); kmain.style.minHeight = (menuheight + 100) + 'px'; } window.onresize = resetMobileUI; window.addEventListener("load", (event) => { resetMobileUI() }); </script> <div id='dropdown-nav' onclick="displayDropdownMenu();"> <svg viewBox="-20 -20 120 120" width="60" height="60"> <rect width="100" height="20"></rect> <rect y="30" width="100" height="20"></rect> <rect y="60" width="100" height="20"></rect> </svg> </div> <form class="bd-search d-flex align-items-center k-search-form" id="search-form"> <input type="search" class="k-search-input" id="search-input" placeholder="Search Keras documentation..." aria-label="Search Keras documentation..." autocomplete="off"> <button class="k-search-btn"> <svg width="13" height="13" viewBox="0 0 13 13"><title>search</title><path d="m4.8495 7.8226c0.82666 0 1.5262-0.29146 2.0985-0.87438 0.57232-0.58292 0.86378-1.2877 0.87438-2.1144 0.010599-0.82666-0.28086-1.5262-0.87438-2.0985-0.59352-0.57232-1.293-0.86378-2.0985-0.87438-0.8055-0.010599-1.5103 0.28086-2.1144 0.87438-0.60414 0.59352-0.8956 1.293-0.87438 2.0985 0.021197 0.8055 0.31266 1.5103 0.87438 2.1144 0.56172 0.60414 1.2665 0.8956 2.1144 0.87438zm4.4695 0.2115 3.681 3.6819-1.259 1.284-3.6817-3.7 0.0019784-0.69479-0.090043-0.098846c-0.87973 0.76087-1.92 1.1413-3.1207 1.1413-1.3553 0-2.5025-0.46363-3.4417-1.3909s-1.4088-2.0686-1.4088-3.4239c0-1.3553 0.4696-2.4966 1.4088-3.4239 0.9392-0.92727 2.0864-1.3969 3.4417-1.4088 1.3553-0.011889 2.4906 0.45771 3.406 1.4088 0.9154 0.95107 1.379 2.0924 1.3909 3.4239 0 1.2126-0.38043 2.2588-1.1413 3.1385l0.098834 0.090049z"></path></svg> </button> </form> <script> var form = document.getElementById('search-form'); form.onsubmit = function(e) { e.preventDefault(); var query = document.getElementById('search-input').value; window.location.href = '/search.html?query=' + query; return False } </script> </div> <div class='k-main-inner' id='k-main-id'> <div class='k-location-slug'> <span class="k-location-slug-pointer">►</span> <a href='/examples/'>Code examples</a> / <a href='/examples/timeseries/'>Timeseries</a> / Timeseries forecasting for weather prediction </div> <div class='k-content'> <h1 id="timeseries-forecasting-for-weather-prediction">Timeseries forecasting for weather prediction</h1> <p><strong>Authors:</strong> <a href="https://prabhanshu.com/github">Prabhanshu Attri</a>, <a href="https://github.com/yashika51">Yashika Sharma</a>, <a href="https://github.com/ktakattack">Kristi Takach</a>, <a href="https://github.com/falaktheoptimist">Falak Shah</a><br> <strong>Date created:</strong> 2020/06/23<br> <strong>Last modified:</strong> 2023/11/22<br> <strong>Description:</strong> This notebook demonstrates how to do timeseries forecasting using a LSTM model.</p> <div class='example_version_banner keras_3'>ⓘ This example uses Keras 3</div> <p><img class="k-inline-icon" src="https://colab.research.google.com/img/colab_favicon.ico"/> <a href="https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/timeseries/ipynb/timeseries_weather_forecasting.ipynb"><strong>View in Colab</strong></a> <span class="k-dot">•</span><img class="k-inline-icon" src="https://github.com/favicon.ico"/> <a href="https://github.com/keras-team/keras-io/blob/master/examples/timeseries/timeseries_weather_forecasting.py"><strong>GitHub source</strong></a></p> <hr /> <h2 id="setup">Setup</h2> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span> <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span> <span class="kn">import</span> <span class="nn">keras</span> </code></pre></div> <hr /> <h2 id="climate-data-timeseries">Climate Data Time-Series</h2> <p>We will be using Jena Climate dataset recorded by the <a href="https://www.bgc-jena.mpg.de/wetter/">Max Planck Institute for Biogeochemistry</a>. The dataset consists of 14 features such as temperature, pressure, humidity etc, recorded once per 10 minutes.</p> <p><strong>Location</strong>: Weather Station, Max Planck Institute for Biogeochemistry in Jena, Germany</p> <p><strong>Time-frame Considered</strong>: Jan 10, 2009 - December 31, 2016</p> <p>The table below shows the column names, their value formats, and their description.</p> <table> <thead> <tr> <th>Index</th> <th>Features</th> <th>Format</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Date Time</td> <td>01.01.2009 00:10:00</td> <td>Date-time reference</td> </tr> <tr> <td>2</td> <td>p (mbar)</td> <td>996.52</td> <td>The pascal SI derived unit of pressure used to quantify internal pressure. Meteorological reports typically state atmospheric pressure in millibars.</td> </tr> <tr> <td>3</td> <td>T (degC)</td> <td>-8.02</td> <td>Temperature in Celsius</td> </tr> <tr> <td>4</td> <td>Tpot (K)</td> <td>265.4</td> <td>Temperature in Kelvin</td> </tr> <tr> <td>5</td> <td>Tdew (degC)</td> <td>-8.9</td> <td>Temperature in Celsius relative to humidity. Dew Point is a measure of the absolute amount of water in the air, the DP is the temperature at which the air cannot hold all the moisture in it and water condenses.</td> </tr> <tr> <td>6</td> <td>rh (%)</td> <td>93.3</td> <td>Relative Humidity is a measure of how saturated the air is with water vapor, the %RH determines the amount of water contained within collection objects.</td> </tr> <tr> <td>7</td> <td>VPmax (mbar)</td> <td>3.33</td> <td>Saturation vapor pressure</td> </tr> <tr> <td>8</td> <td>VPact (mbar)</td> <td>3.11</td> <td>Vapor pressure</td> </tr> <tr> <td>9</td> <td>VPdef (mbar)</td> <td>0.22</td> <td>Vapor pressure deficit</td> </tr> <tr> <td>10</td> <td>sh (g/kg)</td> <td>1.94</td> <td>Specific humidity</td> </tr> <tr> <td>11</td> <td>H2OC (mmol/mol)</td> <td>3.12</td> <td>Water vapor concentration</td> </tr> <tr> <td>12</td> <td>rho (g/m ** 3)</td> <td>1307.75</td> <td>Airtight</td> </tr> <tr> <td>13</td> <td>wv (m/s)</td> <td>1.03</td> <td>Wind speed</td> </tr> <tr> <td>14</td> <td>max. wv (m/s)</td> <td>1.75</td> <td>Maximum wind speed</td> </tr> <tr> <td>15</td> <td>wd (deg)</td> <td>152.3</td> <td>Wind direction in degrees</td> </tr> </tbody> </table> <div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">zipfile</span> <span class="kn">import</span> <span class="n">ZipFile</span> <span class="n">uri</span> <span class="o">=</span> <span class="s2">"https://storage.googleapis.com/tensorflow/tf-keras-datasets/jena_climate_2009_2016.csv.zip"</span> <span class="n">zip_path</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">get_file</span><span class="p">(</span><span class="n">origin</span><span class="o">=</span><span class="n">uri</span><span class="p">,</span> <span class="n">fname</span><span class="o">=</span><span class="s2">"jena_climate_2009_2016.csv.zip"</span><span class="p">)</span> <span class="n">zip_file</span> <span class="o">=</span> <span class="n">ZipFile</span><span class="p">(</span><span class="n">zip_path</span><span class="p">)</span> <span class="n">zip_file</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span> <span class="n">csv_path</span> <span class="o">=</span> <span class="s2">"jena_climate_2009_2016.csv"</span> <span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">csv_path</span><span class="p">)</span> </code></pre></div> <hr /> <h2 id="raw-data-visualization">Raw Data Visualization</h2> <p>To give us a sense of the data we are working with, each feature has been plotted below. This shows the distinct pattern of each feature over the time period from 2009 to 2016. It also shows where anomalies are present, which will be addressed during normalization.</p> <div class="codehilite"><pre><span></span><code><span class="n">titles</span> <span class="o">=</span> <span class="p">[</span> <span class="s2">"Pressure"</span><span class="p">,</span> <span class="s2">"Temperature"</span><span class="p">,</span> <span class="s2">"Temperature in Kelvin"</span><span class="p">,</span> <span class="s2">"Temperature (dew point)"</span><span class="p">,</span> <span class="s2">"Relative Humidity"</span><span class="p">,</span> <span class="s2">"Saturation vapor pressure"</span><span class="p">,</span> <span class="s2">"Vapor pressure"</span><span class="p">,</span> <span class="s2">"Vapor pressure deficit"</span><span class="p">,</span> <span class="s2">"Specific humidity"</span><span class="p">,</span> <span class="s2">"Water vapor concentration"</span><span class="p">,</span> <span class="s2">"Airtight"</span><span class="p">,</span> <span class="s2">"Wind speed"</span><span class="p">,</span> <span class="s2">"Maximum wind speed"</span><span class="p">,</span> <span class="s2">"Wind direction in degrees"</span><span class="p">,</span> <span class="p">]</span> <span class="n">feature_keys</span> <span class="o">=</span> <span class="p">[</span> <span class="s2">"p (mbar)"</span><span class="p">,</span> <span class="s2">"T (degC)"</span><span class="p">,</span> <span class="s2">"Tpot (K)"</span><span class="p">,</span> <span class="s2">"Tdew (degC)"</span><span class="p">,</span> <span class="s2">"rh (%)"</span><span class="p">,</span> <span class="s2">"VPmax (mbar)"</span><span class="p">,</span> <span class="s2">"VPact (mbar)"</span><span class="p">,</span> <span class="s2">"VPdef (mbar)"</span><span class="p">,</span> <span class="s2">"sh (g/kg)"</span><span class="p">,</span> <span class="s2">"H2OC (mmol/mol)"</span><span class="p">,</span> <span class="s2">"rho (g/m**3)"</span><span class="p">,</span> <span class="s2">"wv (m/s)"</span><span class="p">,</span> <span class="s2">"max. wv (m/s)"</span><span class="p">,</span> <span class="s2">"wd (deg)"</span><span class="p">,</span> <span class="p">]</span> <span class="n">colors</span> <span class="o">=</span> <span class="p">[</span> <span class="s2">"blue"</span><span class="p">,</span> <span class="s2">"orange"</span><span class="p">,</span> <span class="s2">"green"</span><span class="p">,</span> <span class="s2">"red"</span><span class="p">,</span> <span class="s2">"purple"</span><span class="p">,</span> <span class="s2">"brown"</span><span class="p">,</span> <span class="s2">"pink"</span><span class="p">,</span> <span class="s2">"gray"</span><span class="p">,</span> <span class="s2">"olive"</span><span class="p">,</span> <span class="s2">"cyan"</span><span class="p">,</span> <span class="p">]</span> <span class="n">date_time_key</span> <span class="o">=</span> <span class="s2">"Date Time"</span> <span class="k">def</span> <span class="nf">show_raw_visualization</span><span class="p">(</span><span class="n">data</span><span class="p">):</span> <span class="n">time_data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">date_time_key</span><span class="p">]</span> <span class="n">fig</span><span class="p">,</span> <span class="n">axes</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span> <span class="n">nrows</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">ncols</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="n">dpi</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s2">"w"</span><span class="p">,</span> <span class="n">edgecolor</span><span class="o">=</span><span class="s2">"k"</span> <span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">feature_keys</span><span class="p">)):</span> <span class="n">key</span> <span class="o">=</span> <span class="n">feature_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="n">c</span> <span class="o">=</span> <span class="n">colors</span><span class="p">[</span><span class="n">i</span> <span class="o">%</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">colors</span><span class="p">))]</span> <span class="n">t_data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="n">t_data</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">time_data</span> <span class="n">t_data</span><span class="o">.</span><span class="n">head</span><span class="p">()</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">t_data</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span> <span class="n">ax</span><span class="o">=</span><span class="n">axes</span><span class="p">[</span><span class="n">i</span> <span class="o">//</span> <span class="mi">2</span><span class="p">,</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="p">],</span> <span class="n">color</span><span class="o">=</span><span class="n">c</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s2">"</span><span class="si">{}</span><span class="s2"> - </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">titles</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">key</span><span class="p">),</span> <span class="n">rot</span><span class="o">=</span><span class="mi">25</span><span class="p">,</span> <span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">([</span><span class="n">titles</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span> <span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span> <span class="n">show_raw_visualization</span><span class="p">(</span><span class="n">df</span><span class="p">)</span> </code></pre></div> <p><img alt="png" src="/img/examples/timeseries/timeseries_weather_forecasting/timeseries_weather_forecasting_6_0.png" /></p> <hr /> <h2 id="data-preprocessing">Data Preprocessing</h2> <p>Here we are picking ~300,000 data points for training. Observation is recorded every 10 mins, that means 6 times per hour. We will resample one point per hour since no drastic change is expected within 60 minutes. We do this via the <code>sampling_rate</code> argument in <code>timeseries_dataset_from_array</code> utility.</p> <p>We are tracking data from past 720 timestamps (720/6=120 hours). This data will be used to predict the temperature after 72 timestamps (72/6=12 hours).</p> <p>Since every feature has values with varying ranges, we do normalization to confine feature values to a range of <code>[0, 1]</code> before training a neural network. We do this by subtracting the mean and dividing by the standard deviation of each feature.</p> <p>71.5 % of the data will be used to train the model, i.e. 300,693 rows. <code>split_fraction</code> can be changed to alter this percentage.</p> <p>The model is shown data for first 5 days i.e. 720 observations, that are sampled every hour. The temperature after 72 (12 hours * 6 observation per hour) observation will be used as a label.</p> <div class="codehilite"><pre><span></span><code><span class="n">split_fraction</span> <span class="o">=</span> <span class="mf">0.715</span> <span class="n">train_split</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">split_fraction</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span> <span class="n">step</span> <span class="o">=</span> <span class="mi">6</span> <span class="n">past</span> <span class="o">=</span> <span class="mi">720</span> <span class="n">future</span> <span class="o">=</span> <span class="mi">72</span> <span class="n">learning_rate</span> <span class="o">=</span> <span class="mf">0.001</span> <span class="n">batch_size</span> <span class="o">=</span> <span class="mi">256</span> <span class="n">epochs</span> <span class="o">=</span> <span class="mi">10</span> <span class="k">def</span> <span class="nf">normalize</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">train_split</span><span class="p">):</span> <span class="n">data_mean</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="n">train_split</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="n">data_std</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="n">train_split</span><span class="p">]</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="p">(</span><span class="n">data</span> <span class="o">-</span> <span class="n">data_mean</span><span class="p">)</span> <span class="o">/</span> <span class="n">data_std</span> </code></pre></div> <p>We can see from the correlation heatmap, few parameters like Relative Humidity and Specific Humidity are redundant. Hence we will be using select features, not all.</p> <div class="codehilite"><pre><span></span><code><span class="nb">print</span><span class="p">(</span> <span class="s2">"The selected parameters are:"</span><span class="p">,</span> <span class="s2">", "</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">titles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">]]),</span> <span class="p">)</span> <span class="n">selected_features</span> <span class="o">=</span> <span class="p">[</span><span class="n">feature_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">]]</span> <span class="n">features</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">selected_features</span><span class="p">]</span> <span class="n">features</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">date_time_key</span><span class="p">]</span> <span class="n">features</span><span class="o">.</span><span class="n">head</span><span class="p">()</span> <span class="n">features</span> <span class="o">=</span> <span class="n">normalize</span><span class="p">(</span><span class="n">features</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">train_split</span><span class="p">)</span> <span class="n">features</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">features</span><span class="p">)</span> <span class="n">features</span><span class="o">.</span><span class="n">head</span><span class="p">()</span> <span class="n">train_data</span> <span class="o">=</span> <span class="n">features</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">0</span> <span class="p">:</span> <span class="n">train_split</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="n">val_data</span> <span class="o">=</span> <span class="n">features</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">train_split</span><span class="p">:]</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>The selected parameters are: Pressure, Temperature, Saturation vapor pressure, Vapor pressure deficit, Specific humidity, Airtight, Wind speed </code></pre></div> </div> <h1 id="training-dataset">Training dataset</h1> <p>The training dataset labels starts from the 792nd observation (720 + 72).</p> <div class="codehilite"><pre><span></span><code><span class="n">start</span> <span class="o">=</span> <span class="n">past</span> <span class="o">+</span> <span class="n">future</span> <span class="n">end</span> <span class="o">=</span> <span class="n">start</span> <span class="o">+</span> <span class="n">train_split</span> <span class="n">x_train</span> <span class="o">=</span> <span class="n">train_data</span><span class="p">[[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">)]]</span><span class="o">.</span><span class="n">values</span> <span class="n">y_train</span> <span class="o">=</span> <span class="n">features</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">][[</span><span class="mi">1</span><span class="p">]]</span> <span class="n">sequence_length</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">past</span> <span class="o">/</span> <span class="n">step</span><span class="p">)</span> </code></pre></div> <p>The <code>timeseries_dataset_from_array</code> function takes in a sequence of data-points gathered at equal intervals, along with time series parameters such as length of the sequences/windows, spacing between two sequence/windows, etc., to produce batches of sub-timeseries inputs and targets sampled from the main timeseries.</p> <div class="codehilite"><pre><span></span><code><span class="n">dataset_train</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">preprocessing</span><span class="o">.</span><span class="n">timeseries_dataset_from_array</span><span class="p">(</span> <span class="n">x_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">sequence_length</span><span class="o">=</span><span class="n">sequence_length</span><span class="p">,</span> <span class="n">sampling_rate</span><span class="o">=</span><span class="n">step</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <hr /> <h2 id="validation-dataset">Validation dataset</h2> <p>The validation dataset must not contain the last 792 rows as we won't have label data for those records, hence 792 must be subtracted from the end of the data.</p> <p>The validation label dataset must start from 792 after train_split, hence we must add past + future (792) to label_start.</p> <div class="codehilite"><pre><span></span><code><span class="n">x_end</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">val_data</span><span class="p">)</span> <span class="o">-</span> <span class="n">past</span> <span class="o">-</span> <span class="n">future</span> <span class="n">label_start</span> <span class="o">=</span> <span class="n">train_split</span> <span class="o">+</span> <span class="n">past</span> <span class="o">+</span> <span class="n">future</span> <span class="n">x_val</span> <span class="o">=</span> <span class="n">val_data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="n">x_end</span><span class="p">][[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">)]]</span><span class="o">.</span><span class="n">values</span> <span class="n">y_val</span> <span class="o">=</span> <span class="n">features</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">label_start</span><span class="p">:][[</span><span class="mi">1</span><span class="p">]]</span> <span class="n">dataset_val</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">preprocessing</span><span class="o">.</span><span class="n">timeseries_dataset_from_array</span><span class="p">(</span> <span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span><span class="p">,</span> <span class="n">sequence_length</span><span class="o">=</span><span class="n">sequence_length</span><span class="p">,</span> <span class="n">sampling_rate</span><span class="o">=</span><span class="n">step</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">,</span> <span class="p">)</span> <span class="k">for</span> <span class="n">batch</span> <span class="ow">in</span> <span class="n">dataset_train</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">targets</span> <span class="o">=</span> <span class="n">batch</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Input shape:"</span><span class="p">,</span> <span class="n">inputs</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Target shape:"</span><span class="p">,</span> <span class="n">targets</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Input shape: (256, 120, 7) Target shape: (256, 1) </code></pre></div> </div> <hr /> <h2 id="training">Training</h2> <div class="codehilite"><pre><span></span><code><span class="n">inputs</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">Input</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="n">inputs</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">inputs</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]))</span> <span class="n">lstm_out</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">LSTM</span><span class="p">(</span><span class="mi">32</span><span class="p">)(</span><span class="n">inputs</span><span class="p">)</span> <span class="n">outputs</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">1</span><span class="p">)(</span><span class="n">lstm_out</span><span class="p">)</span> <span class="n">model</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">Model</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="n">inputs</span><span class="p">,</span> <span class="n">outputs</span><span class="o">=</span><span class="n">outputs</span><span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">optimizer</span><span class="o">=</span><span class="n">keras</span><span class="o">.</span><span class="n">optimizers</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">learning_rate</span><span class="o">=</span><span class="n">learning_rate</span><span class="p">),</span> <span class="n">loss</span><span class="o">=</span><span class="s2">"mse"</span><span class="p">)</span> <span class="n">model</span><span class="o">.</span><span class="n">summary</span><span class="p">()</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>CUDA backend failed to initialize: Found cuSOLVER version 11405, but JAX was built against version 11502, which is newer. The copy of cuSOLVER that is installed must be at least as new as the version against which JAX was built. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.) </code></pre></div> </div> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">Model: "functional_1"</span> </pre> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃<span style="font-weight: bold"> Layer (type) </span>┃<span style="font-weight: bold"> Output Shape </span>┃<span style="font-weight: bold"> Param # </span>┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩ │ input_layer (<span style="color: #0087ff; text-decoration-color: #0087ff">InputLayer</span>) │ (<span style="color: #00d7ff; text-decoration-color: #00d7ff">None</span>, <span style="color: #00af00; text-decoration-color: #00af00">120</span>, <span style="color: #00af00; text-decoration-color: #00af00">7</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">0</span> │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ lstm (<span style="color: #0087ff; text-decoration-color: #0087ff">LSTM</span>) │ (<span style="color: #00d7ff; text-decoration-color: #00d7ff">None</span>, <span style="color: #00af00; text-decoration-color: #00af00">32</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">5,120</span> │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dense (<span style="color: #0087ff; text-decoration-color: #0087ff">Dense</span>) │ (<span style="color: #00d7ff; text-decoration-color: #00d7ff">None</span>, <span style="color: #00af00; text-decoration-color: #00af00">1</span>) │ <span style="color: #00af00; text-decoration-color: #00af00">33</span> │ └─────────────────────────────────┴───────────────────────────┴────────────┘ </pre> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold"> Total params: </span><span style="color: #00af00; text-decoration-color: #00af00">5,153</span> (20.13 KB) </pre> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold"> Trainable params: </span><span style="color: #00af00; text-decoration-color: #00af00">5,153</span> (20.13 KB) </pre> <pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold"> Non-trainable params: </span><span style="color: #00af00; text-decoration-color: #00af00">0</span> (0.00 B) </pre> <p>We'll use the <code>ModelCheckpoint</code> callback to regularly save checkpoints, and the <code>EarlyStopping</code> callback to interrupt training when the validation loss is not longer improving.</p> <div class="codehilite"><pre><span></span><code><span class="n">path_checkpoint</span> <span class="o">=</span> <span class="s2">"model_checkpoint.weights.h5"</span> <span class="n">es_callback</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">callbacks</span><span class="o">.</span><span class="n">EarlyStopping</span><span class="p">(</span><span class="n">monitor</span><span class="o">=</span><span class="s2">"val_loss"</span><span class="p">,</span> <span class="n">min_delta</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">patience</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span> <span class="n">modelckpt_callback</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">callbacks</span><span class="o">.</span><span class="n">ModelCheckpoint</span><span class="p">(</span> <span class="n">monitor</span><span class="o">=</span><span class="s2">"val_loss"</span><span class="p">,</span> <span class="n">filepath</span><span class="o">=</span><span class="n">path_checkpoint</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">save_weights_only</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">save_best_only</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="p">)</span> <span class="n">history</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span> <span class="n">dataset_train</span><span class="p">,</span> <span class="n">epochs</span><span class="o">=</span><span class="n">epochs</span><span class="p">,</span> <span class="n">validation_data</span><span class="o">=</span><span class="n">dataset_val</span><span class="p">,</span> <span class="n">callbacks</span><span class="o">=</span><span class="p">[</span><span class="n">es_callback</span><span class="p">,</span> <span class="n">modelckpt_callback</span><span class="p">],</span> <span class="p">)</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code>Epoch 1/10 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 0s 70ms/step - loss: 0.3008 Epoch 1: val_loss improved from inf to 0.15039, saving model to model_checkpoint.weights.h5 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 104s 88ms/step - loss: 0.3007 - val_loss: 0.1504 Epoch 2/10 1171/1172 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 66ms/step - loss: 0.1397 Epoch 2: val_loss improved from 0.15039 to 0.14231, saving model to model_checkpoint.weights.h5 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 97s 83ms/step - loss: 0.1396 - val_loss: 0.1423 Epoch 3/10 1171/1172 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 69ms/step - loss: 0.1242 Epoch 3: val_loss did not improve from 0.14231 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 101s 86ms/step - loss: 0.1242 - val_loss: 0.1513 Epoch 4/10 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 0s 68ms/step - loss: 0.1182 Epoch 4: val_loss did not improve from 0.14231 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 102s 87ms/step - loss: 0.1182 - val_loss: 0.1503 Epoch 5/10 1171/1172 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 67ms/step - loss: 0.1160 Epoch 5: val_loss did not improve from 0.14231 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 100s 85ms/step - loss: 0.1160 - val_loss: 0.1500 Epoch 6/10 1171/1172 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 69ms/step - loss: 0.1130 Epoch 6: val_loss did not improve from 0.14231 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 100s 86ms/step - loss: 0.1130 - val_loss: 0.1469 Epoch 7/10 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 0s 70ms/step - loss: 0.1106 Epoch 7: val_loss improved from 0.14231 to 0.13916, saving model to model_checkpoint.weights.h5 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 104s 89ms/step - loss: 0.1106 - val_loss: 0.1392 Epoch 8/10 1171/1172 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 66ms/step - loss: 0.1097 Epoch 8: val_loss improved from 0.13916 to 0.13257, saving model to model_checkpoint.weights.h5 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 98s 84ms/step - loss: 0.1097 - val_loss: 0.1326 Epoch 9/10 1171/1172 ━━━━━━━━━━━━━━━━━━━[37m━ 0s 68ms/step - loss: 0.1075 Epoch 9: val_loss improved from 0.13257 to 0.13057, saving model to model_checkpoint.weights.h5 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 100s 85ms/step - loss: 0.1075 - val_loss: 0.1306 Epoch 10/10 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 0s 66ms/step - loss: 0.1065 Epoch 10: val_loss improved from 0.13057 to 0.12671, saving model to model_checkpoint.weights.h5 1172/1172 ━━━━━━━━━━━━━━━━━━━━ 98s 84ms/step - loss: 0.1065 - val_loss: 0.1267 </code></pre></div> </div> <p>We can visualize the loss with the function below. After one point, the loss stops decreasing.</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">visualize_loss</span><span class="p">(</span><span class="n">history</span><span class="p">,</span> <span class="n">title</span><span class="p">):</span> <span class="n">loss</span> <span class="o">=</span> <span class="n">history</span><span class="o">.</span><span class="n">history</span><span class="p">[</span><span class="s2">"loss"</span><span class="p">]</span> <span class="n">val_loss</span> <span class="o">=</span> <span class="n">history</span><span class="o">.</span><span class="n">history</span><span class="p">[</span><span class="s2">"val_loss"</span><span class="p">]</span> <span class="n">epochs</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">loss</span><span class="p">))</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">epochs</span><span class="p">,</span> <span class="n">loss</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Training loss"</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">epochs</span><span class="p">,</span> <span class="n">val_loss</span><span class="p">,</span> <span class="s2">"r"</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Validation loss"</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="n">title</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s2">"Epochs"</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s2">"Loss"</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span> <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> <span class="n">visualize_loss</span><span class="p">(</span><span class="n">history</span><span class="p">,</span> <span class="s2">"Training and Validation Loss"</span><span class="p">)</span> </code></pre></div> <p><img alt="png" src="/img/examples/timeseries/timeseries_weather_forecasting/timeseries_weather_forecasting_22_0.png" /></p> <hr /> <h2 id="prediction">Prediction</h2> <p>The trained model above is now able to make predictions for 5 sets of values from validation set.</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">show_plot</span><span class="p">(</span><span class="n">plot_data</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="n">title</span><span class="p">):</span> <span class="n">labels</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"History"</span><span class="p">,</span> <span class="s2">"True Future"</span><span class="p">,</span> <span class="s2">"Model Prediction"</span><span class="p">]</span> <span class="n">marker</span> <span class="o">=</span> <span class="p">[</span><span class="s2">".-"</span><span class="p">,</span> <span class="s2">"rx"</span><span class="p">,</span> <span class="s2">"go"</span><span class="p">]</span> <span class="n">time_steps</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">plot_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="mi">0</span><span class="p">))</span> <span class="k">if</span> <span class="n">delta</span><span class="p">:</span> <span class="n">future</span> <span class="o">=</span> <span class="n">delta</span> <span class="k">else</span><span class="p">:</span> <span class="n">future</span> <span class="o">=</span> <span class="mi">0</span> <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="n">title</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">plot_data</span><span class="p">):</span> <span class="k">if</span> <span class="n">i</span><span class="p">:</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">future</span><span class="p">,</span> <span class="n">plot_data</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">marker</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">markersize</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">labels</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="k">else</span><span class="p">:</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time_steps</span><span class="p">,</span> <span class="n">plot_data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span> <span class="n">marker</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="n">labels</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span> <span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">([</span><span class="n">time_steps</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="p">(</span><span class="n">future</span> <span class="o">+</span> <span class="mi">5</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span><span class="p">])</span> <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s2">"Time-Step"</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> <span class="k">return</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">dataset_val</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span> <span class="n">show_plot</span><span class="p">(</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">][:,</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">numpy</span><span class="p">(),</span> <span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">numpy</span><span class="p">(),</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">]],</span> <span class="mi">12</span><span class="p">,</span> <span class="s2">"Single Step Prediction"</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step </code></pre></div> </div> <p><img alt="png" src="/img/examples/timeseries/timeseries_weather_forecasting/timeseries_weather_forecasting_24_1.png" /></p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step </code></pre></div> </div> <p><img alt="png" src="/img/examples/timeseries/timeseries_weather_forecasting/timeseries_weather_forecasting_24_3.png" /></p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step </code></pre></div> </div> <p><img alt="png" src="/img/examples/timeseries/timeseries_weather_forecasting/timeseries_weather_forecasting_24_5.png" /></p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step </code></pre></div> </div> <p><img alt="png" src="/img/examples/timeseries/timeseries_weather_forecasting/timeseries_weather_forecasting_24_7.png" /></p> <div class="k-default-codeblock"> <div class="codehilite"><pre><span></span><code> 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step </code></pre></div> </div> <p><img alt="png" src="/img/examples/timeseries/timeseries_weather_forecasting/timeseries_weather_forecasting_24_9.png" /></p> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#timeseries-forecasting-for-weather-prediction'>Timeseries forecasting for weather prediction</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#setup'>Setup</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#climate-data-timeseries'>Climate Data Time-Series</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#raw-data-visualization'>Raw Data Visualization</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#data-preprocessing'>Data Preprocessing</a> </div> <div class='k-outline-depth-1'> <a href='#training-dataset'>Training dataset</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#validation-dataset'>Validation dataset</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#training'>Training</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#prediction'>Prediction</a> </div> </div> </div> </div> </div> </body> <footer style="float: left; width: 100%; padding: 1em; border-top: solid 1px #bbb;"> <a href="https://policies.google.com/terms">Terms</a> | <a href="https://policies.google.com/privacy">Privacy</a> </footer> </html>