197 lines
23 KiB
HTML
197 lines
23 KiB
HTML
|
||
|
||
<!doctype html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
<head>
|
||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
<title>pyFTS.partitioners.Entropy — pyFTS 1.2.3 documentation</title>
|
||
<link rel="stylesheet" href="../../../_static/bizstyle.css" type="text/css" />
|
||
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
|
||
<script type="text/javascript" src="../../../_static/documentation_options.js"></script>
|
||
<script type="text/javascript" src="../../../_static/jquery.js"></script>
|
||
<script type="text/javascript" src="../../../_static/underscore.js"></script>
|
||
<script type="text/javascript" src="../../../_static/doctools.js"></script>
|
||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
||
<script type="text/javascript" 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 type="text/javascript" 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.2.3 documentation</a> »</li>
|
||
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> »</li>
|
||
</ul>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<p class="logo"><a href="../../../index.html">
|
||
<img class="logo" src="../../../_static/logo_heading2.png" alt="Logo"/>
|
||
</a></p>
|
||
<div id="searchbox" style="display: none" role="search">
|
||
<h3>Quick search</h3>
|
||
<div class="searchformwrapper">
|
||
<form class="search" action="../../../search.html" method="get">
|
||
<input type="text" name="q" />
|
||
<input type="submit" value="Go" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
</div>
|
||
<script type="text/javascript">$('#searchbox').show(0);</script>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<h1>Source code for pyFTS.partitioners.Entropy</h1><div class="highlight"><pre>
|
||
<span></span><span class="sd">"""</span>
|
||
<span class="sd">C. H. Cheng, R. J. Chang, and C. A. Yeh, “Entropy-based and trapezoidal fuzzification-based fuzzy time series approach for forecasting IT project cost,”</span>
|
||
<span class="sd">Technol. Forecast. Social Change, vol. 73, no. 5, pp. 524–542, Jun. 2006.</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">import</span> <span class="nn">math</span>
|
||
<span class="kn">import</span> <span class="nn">random</span> <span class="k">as</span> <span class="nn">rnd</span>
|
||
<span class="kn">import</span> <span class="nn">functools</span><span class="o">,</span> <span class="nn">operator</span>
|
||
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">Membership</span>
|
||
<span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">partitioner</span>
|
||
|
||
|
||
<div class="viewcode-block" id="splitBelow"><a class="viewcode-back" href="../../../pyFTS.partitioners.html#pyFTS.partitioners.Entropy.splitBelow">[docs]</a><span class="k">def</span> <span class="nf">splitBelow</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">threshold</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span> <span class="k">if</span> <span class="n">k</span> <span class="o"><=</span> <span class="n">threshold</span><span class="p">]</span></div>
|
||
|
||
|
||
<div class="viewcode-block" id="splitAbove"><a class="viewcode-back" href="../../../pyFTS.partitioners.html#pyFTS.partitioners.Entropy.splitAbove">[docs]</a><span class="k">def</span> <span class="nf">splitAbove</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">threshold</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span> <span class="k">if</span> <span class="n">k</span> <span class="o">></span> <span class="n">threshold</span><span class="p">]</span></div>
|
||
|
||
|
||
<div class="viewcode-block" id="PMF"><a class="viewcode-back" href="../../../pyFTS.partitioners.html#pyFTS.partitioners.Entropy.PMF">[docs]</a><span class="k">def</span> <span class="nf">PMF</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">threshold</span><span class="p">):</span>
|
||
<span class="n">a</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="mf">1.0</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">splitBelow</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">threshold</span><span class="p">)])</span>
|
||
<span class="n">b</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="mf">1.0</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">splitAbove</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">threshold</span><span class="p">)])</span>
|
||
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
|
||
<span class="k">return</span> <span class="p">[</span><span class="n">a</span> <span class="o">/</span> <span class="n">l</span><span class="p">,</span> <span class="n">b</span> <span class="o">/</span> <span class="n">l</span><span class="p">]</span></div>
|
||
|
||
|
||
<div class="viewcode-block" id="entropy"><a class="viewcode-back" href="../../../pyFTS.partitioners.html#pyFTS.partitioners.Entropy.entropy">[docs]</a><span class="k">def</span> <span class="nf">entropy</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">threshold</span><span class="p">):</span>
|
||
<span class="n">pmf</span> <span class="o">=</span> <span class="n">PMF</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">threshold</span><span class="p">)</span>
|
||
<span class="k">if</span> <span class="n">pmf</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">pmf</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="mi">1</span>
|
||
<span class="k">else</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">([</span><span class="n">pmf</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">pmf</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">pmf</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">pmf</span><span class="p">[</span><span class="mi">1</span><span class="p">])])</span></div>
|
||
|
||
|
||
<div class="viewcode-block" id="informationGain"><a class="viewcode-back" href="../../../pyFTS.partitioners.html#pyFTS.partitioners.Entropy.informationGain">[docs]</a><span class="k">def</span> <span class="nf">informationGain</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">thres1</span><span class="p">,</span> <span class="n">thres2</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">entropy</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">thres1</span><span class="p">)</span> <span class="o">-</span> <span class="n">entropy</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">thres2</span><span class="p">)</span></div>
|
||
|
||
|
||
<div class="viewcode-block" id="bestSplit"><a class="viewcode-back" href="../../../pyFTS.partitioners.html#pyFTS.partitioners.Entropy.bestSplit">[docs]</a><span class="k">def</span> <span class="nf">bestSplit</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">npart</span><span class="p">):</span>
|
||
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o"><</span> <span class="mi">2</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="kc">None</span>
|
||
<span class="n">count</span> <span class="o">=</span> <span class="mi">1</span>
|
||
<span class="n">ndata</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
|
||
<span class="n">ndata</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
|
||
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
|
||
<span class="n">threshold</span> <span class="o">=</span> <span class="mi">0</span>
|
||
<span class="k">try</span><span class="p">:</span>
|
||
<span class="k">while</span> <span class="n">count</span> <span class="o"><</span> <span class="n">l</span> <span class="ow">and</span> <span class="n">informationGain</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">ndata</span><span class="p">[</span><span class="n">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="n">ndata</span><span class="p">[</span><span class="n">count</span><span class="p">])</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">:</span>
|
||
<span class="n">threshold</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">count</span><span class="p">]</span>
|
||
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||
<span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">threshold</span><span class="p">)</span>
|
||
<span class="nb">print</span> <span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
|
||
<span class="nb">print</span> <span class="p">(</span><span class="n">count</span><span class="p">)</span>
|
||
|
||
<span class="n">rem</span> <span class="o">=</span> <span class="n">npart</span> <span class="o">%</span> <span class="mi">2</span>
|
||
|
||
<span class="k">if</span> <span class="p">(</span><span class="n">npart</span> <span class="o">-</span> <span class="n">rem</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
|
||
<span class="n">p1</span> <span class="o">=</span> <span class="n">splitBelow</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">threshold</span><span class="p">)</span>
|
||
<span class="n">p2</span> <span class="o">=</span> <span class="n">splitAbove</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">threshold</span><span class="p">)</span>
|
||
|
||
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">p1</span><span class="p">)</span> <span class="o">></span> <span class="nb">len</span><span class="p">(</span><span class="n">p2</span><span class="p">):</span>
|
||
<span class="n">np1</span> <span class="o">=</span> <span class="p">(</span><span class="n">npart</span> <span class="o">-</span> <span class="n">rem</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="n">rem</span>
|
||
<span class="n">np2</span> <span class="o">=</span> <span class="p">(</span><span class="n">npart</span> <span class="o">-</span> <span class="n">rem</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span>
|
||
<span class="k">else</span><span class="p">:</span>
|
||
<span class="n">np1</span> <span class="o">=</span> <span class="p">(</span><span class="n">npart</span> <span class="o">-</span> <span class="n">rem</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>
|
||
<span class="n">np2</span> <span class="o">=</span> <span class="p">(</span><span class="n">npart</span> <span class="o">-</span> <span class="n">rem</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">+</span> <span class="n">rem</span>
|
||
|
||
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="n">threshold</span><span class="p">]</span>
|
||
|
||
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">bestSplit</span><span class="p">(</span><span class="n">p1</span><span class="p">,</span> <span class="n">np1</span> <span class="p">):</span> <span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
|
||
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">bestSplit</span><span class="p">(</span><span class="n">p2</span><span class="p">,</span> <span class="n">np2</span> <span class="p">):</span> <span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
|
||
|
||
<span class="k">return</span> <span class="n">tmp</span>
|
||
|
||
<span class="k">else</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="p">[</span><span class="n">threshold</span><span class="p">]</span></div>
|
||
|
||
|
||
<div class="viewcode-block" id="EntropyPartitioner"><a class="viewcode-back" href="../../../pyFTS.partitioners.html#pyFTS.partitioners.Entropy.EntropyPartitioner">[docs]</a><span class="k">class</span> <span class="nc">EntropyPartitioner</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">Partitioner</span><span class="p">):</span>
|
||
<span class="sd">"""Huarng Entropy Partitioner"""</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="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
||
<span class="nb">super</span><span class="p">(</span><span class="n">EntropyPartitioner</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">"Entropy"</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
||
|
||
<div class="viewcode-block" id="EntropyPartitioner.build"><a class="viewcode-back" href="../../../pyFTS.partitioners.html#pyFTS.partitioners.Entropy.EntropyPartitioner.build">[docs]</a> <span class="k">def</span> <span class="nf">build</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="n">sets</span> <span class="o">=</span> <span class="p">{}</span>
|
||
|
||
<span class="n">partitions</span> <span class="o">=</span> <span class="n">bestSplit</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">partitions</span><span class="p">)</span>
|
||
<span class="n">partitions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">min</span><span class="p">)</span>
|
||
<span class="n">partitions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max</span><span class="p">)</span>
|
||
<span class="n">partitions</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">partitions</span><span class="p">))</span>
|
||
<span class="n">partitions</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
|
||
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">partitions</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
|
||
<span class="n">_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_name</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
|
||
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">membership_function</span> <span class="o">==</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">:</span>
|
||
<span class="n">sets</span><span class="p">[</span><span class="n">_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">FuzzySet</span><span class="o">.</span><span class="n">FuzzySet</span><span class="p">(</span><span class="n">_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">,</span>
|
||
<span class="p">[</span><span class="n">partitions</span><span class="p">[</span><span class="n">c</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="n">partitions</span><span class="p">[</span><span class="n">c</span><span class="p">],</span> <span class="n">partitions</span><span class="p">[</span><span class="n">c</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]],</span><span class="n">partitions</span><span class="p">[</span><span class="n">c</span><span class="p">])</span>
|
||
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">membership_function</span> <span class="o">==</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trapmf</span><span class="p">:</span>
|
||
<span class="n">b1</span> <span class="o">=</span> <span class="p">(</span><span class="n">partitions</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">-</span> <span class="n">partitions</span><span class="p">[</span><span class="n">c</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span><span class="o">/</span><span class="mi">2</span>
|
||
<span class="n">b2</span> <span class="o">=</span> <span class="p">(</span><span class="n">partitions</span><span class="p">[</span><span class="n">c</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">partitions</span><span class="p">[</span><span class="n">c</span><span class="p">])</span> <span class="o">/</span> <span class="mi">2</span>
|
||
<span class="n">sets</span><span class="p">[</span><span class="n">_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">FuzzySet</span><span class="o">.</span><span class="n">FuzzySet</span><span class="p">(</span><span class="n">_name</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trapmf</span><span class="p">,</span>
|
||
<span class="p">[</span><span class="n">partitions</span><span class="p">[</span><span class="n">c</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="n">partitions</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">-</span> <span class="n">b1</span><span class="p">,</span>
|
||
<span class="n">partitions</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+</span> <span class="n">b2</span><span class="p">,</span> <span class="n">partitions</span><span class="p">[</span><span class="n">c</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]],</span>
|
||
<span class="n">partitions</span><span class="p">[</span><span class="n">c</span><span class="p">])</span>
|
||
|
||
<span class="k">return</span> <span class="n">sets</span></div></div>
|
||
</pre></div>
|
||
|
||
</div>
|
||
</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.2.3 documentation</a> »</li>
|
||
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> »</li>
|
||
</ul>
|
||
</div>
|
||
<div class="footer" role="contentinfo">
|
||
© Copyright 2018, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
|
||
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.2.
|
||
</div>
|
||
</body>
|
||
</html> |