180 lines
20 KiB
HTML
180 lines
20 KiB
HTML
|
|
|
|
<!doctype html>
|
|
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>pyFTS.models.ensemble.multiseasonal — pyFTS 1.7 documentation</title>
|
|
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
|
|
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
|
|
|
|
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
|
|
<script src="../../../../_static/jquery.js"></script>
|
|
<script src="../../../../_static/underscore.js"></script>
|
|
<script src="../../../../_static/doctools.js"></script>
|
|
<script src="../../../../_static/bizstyle.js"></script>
|
|
<link rel="index" title="Index" href="../../../../genindex.html" />
|
|
<link rel="search" title="Search" href="../../../../search.html" />
|
|
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
|
|
<!--[if lt IE 9]>
|
|
<script src="_static/css3-mediaqueries.js"></script>
|
|
<![endif]-->
|
|
</head><body>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../../../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../../../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ensemble.multiseasonal</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<h1>Source code for pyFTS.models.ensemble.multiseasonal</h1><div class="highlight"><pre>
|
|
<span></span><span class="sd">"""</span>
|
|
<span class="sd">Silva, P. C. L et al. Probabilistic Forecasting with Seasonal Ensemble Fuzzy Time-Series</span>
|
|
<span class="sd">XIII Brazilian Congress on Computational Intelligence, 2017. Rio de Janeiro, Brazil.</span>
|
|
<span class="sd">"""</span>
|
|
|
|
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Util</span> <span class="k">as</span> <span class="n">cUtil</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.models.ensemble</span> <span class="k">import</span> <span class="n">ensemble</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.models.seasonal</span> <span class="k">import</span> <span class="n">cmsfts</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.probabilistic</span> <span class="k">import</span> <span class="n">ProbabilityDistribution</span>
|
|
<span class="kn">from</span> <span class="nn">copy</span> <span class="k">import</span> <span class="n">deepcopy</span>
|
|
<span class="kn">from</span> <span class="nn">joblib</span> <span class="k">import</span> <span class="n">Parallel</span><span class="p">,</span> <span class="n">delayed</span>
|
|
<span class="kn">import</span> <span class="nn">multiprocessing</span>
|
|
|
|
|
|
<div class="viewcode-block" id="train_individual_model"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.train_individual_model">[docs]</a><span class="k">def</span> <span class="nf">train_individual_model</span><span class="p">(</span><span class="n">partitioner</span><span class="p">,</span> <span class="n">train_data</span><span class="p">,</span> <span class="n">indexer</span><span class="p">):</span>
|
|
<span class="n">pttr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="vm">__module__</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="n">diff</span> <span class="o">=</span> <span class="s2">"_diff"</span> <span class="k">if</span> <span class="n">partitioner</span><span class="o">.</span><span class="n">transformation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">""</span>
|
|
<span class="n">_key</span> <span class="o">=</span> <span class="s2">"msfts_"</span> <span class="o">+</span> <span class="n">pttr</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">partitions</span><span class="p">)</span> <span class="o">+</span> <span class="n">diff</span> <span class="o">+</span> <span class="s2">"_"</span> <span class="o">+</span> <span class="n">indexer</span><span class="o">.</span><span class="n">name</span>
|
|
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">_key</span><span class="p">)</span>
|
|
|
|
<span class="n">model</span> <span class="o">=</span> <span class="n">cmsfts</span><span class="o">.</span><span class="n">ContextualMultiSeasonalFTS</span><span class="p">(</span><span class="n">_key</span><span class="p">,</span> <span class="n">indexer</span><span class="o">=</span><span class="n">indexer</span><span class="p">)</span>
|
|
<span class="n">model</span><span class="o">.</span><span class="n">append_transformation</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">transformation</span><span class="p">)</span>
|
|
<span class="n">model</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">train_data</span><span class="p">,</span> <span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
|
|
|
|
<span class="n">cUtil</span><span class="o">.</span><span class="n">persist_obj</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="s2">"models/"</span><span class="o">+</span><span class="n">_key</span><span class="o">+</span><span class="s2">".pkl"</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">model</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="SeasonalEnsembleFTS"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.SeasonalEnsembleFTS">[docs]</a><span class="k">class</span> <span class="nc">SeasonalEnsembleFTS</span><span class="p">(</span><span class="n">ensemble</span><span class="o">.</span><span class="n">EnsembleFTS</span><span class="p">):</span>
|
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="nb">super</span><span class="p">(</span><span class="n">SeasonalEnsembleFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"Seasonal Ensemble FTS"</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">indexers</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">partitioners</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="o">=</span> <span class="kc">True</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">has_seasonality</span> <span class="o">=</span> <span class="kc">True</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
|
|
|
|
<div class="viewcode-block" id="SeasonalEnsembleFTS.update_uod"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.SeasonalEnsembleFTS.update_uod">[docs]</a> <span class="k">def</span> <span class="nf">update_uod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span></div>
|
|
|
|
<div class="viewcode-block" id="SeasonalEnsembleFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.SeasonalEnsembleFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
|
|
|
|
<span class="n">num_cores</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">cpu_count</span><span class="p">()</span>
|
|
|
|
<span class="n">pool</span> <span class="o">=</span> <span class="p">{}</span>
|
|
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
|
<span class="k">for</span> <span class="n">ix</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexers</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">pt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioners</span><span class="p">:</span>
|
|
<span class="n">pool</span><span class="p">[</span><span class="n">count</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'ix'</span><span class="p">:</span> <span class="n">ix</span><span class="p">,</span> <span class="s1">'pt'</span><span class="p">:</span> <span class="n">pt</span><span class="p">}</span>
|
|
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
|
|
|
<span class="n">results</span> <span class="o">=</span> <span class="n">Parallel</span><span class="p">(</span><span class="n">n_jobs</span><span class="o">=</span><span class="n">num_cores</span><span class="p">)(</span>
|
|
<span class="n">delayed</span><span class="p">(</span><span class="n">train_individual_model</span><span class="p">)(</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">m</span><span class="p">][</span><span class="s1">'pt'</span><span class="p">]),</span> <span class="n">data</span><span class="p">,</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">m</span><span class="p">][</span><span class="s1">'ix'</span><span class="p">]))</span>
|
|
<span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">pool</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
|
|
|
|
<span class="k">for</span> <span class="n">tmp</span> <span class="ow">in</span> <span class="n">results</span><span class="p">:</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">append_model</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
|
|
|
|
<span class="n">cUtil</span><span class="o">.</span><span class="n">persist_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s2">"models/"</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="o">+</span><span class="s2">".pkl"</span><span class="p">)</span></div>
|
|
|
|
<div class="viewcode-block" id="SeasonalEnsembleFTS.forecast_distribution"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.SeasonalEnsembleFTS.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">smooth</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"smooth"</span><span class="p">,</span> <span class="s2">"KDE"</span><span class="p">)</span>
|
|
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"alpha"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
|
|
|
|
<span class="n">uod</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_UoD</span><span class="p">()</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">index</span><span class="p">:</span>
|
|
|
|
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
|
|
|
|
<span class="k">if</span> <span class="n">alpha</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="n">tmp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_distribution_interquantile</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span> <span class="n">alpha</span><span class="p">)</span>
|
|
|
|
<span class="n">name</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_index</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="n">k</span><span class="p">]))</span>
|
|
|
|
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="n">smooth</span><span class="p">,</span> <span class="n">uod</span><span class="o">=</span><span class="n">uod</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">tmp</span><span class="p">,</span>
|
|
<span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">ret</span></div></div>
|
|
</pre></div>
|
|
|
|
<div class="clearer"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|
<div class="sphinxsidebarwrapper">
|
|
<div id="searchbox" style="display: none" role="search">
|
|
<h3 id="searchlabel">Quick search</h3>
|
|
<div class="searchformwrapper">
|
|
<form class="search" action="../../../../search.html" method="get">
|
|
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
|
|
<input type="submit" value="Go" />
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<script>$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../../../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../../../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ensemble.multiseasonal</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer" role="contentinfo">
|
|
© Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
|
|
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
|
|
</div>
|
|
</body>
|
|
</html> |