<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pyspark.mllib.fpm — PySpark master documentation</title>
<link rel="stylesheet" href="../../../_static/nature.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../',
VERSION: 'master',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</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>
<link rel="top" title="PySpark master documentation" href="../../../index.html" />
<link rel="up" title="Module code" href="../../index.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="../../../index.html">PySpark master documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyspark.mllib.fpm</h1><div class="highlight"><pre>
<span class="c">#</span>
<span class="c"># Licensed to the Apache Software Foundation (ASF) under one or more</span>
<span class="c"># contributor license agreements. See the NOTICE file distributed with</span>
<span class="c"># this work for additional information regarding copyright ownership.</span>
<span class="c"># The ASF licenses this file to You under the Apache License, Version 2.0</span>
<span class="c"># (the "License"); you may not use this file except in compliance with</span>
<span class="c"># the License. You may obtain a copy of the License at</span>
<span class="c">#</span>
<span class="c"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c">#</span>
<span class="c"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c"># distributed under the License is distributed on an "AS IS" BASIS,</span>
<span class="c"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c"># See the License for the specific language governing permissions and</span>
<span class="c"># limitations under the License.</span>
<span class="c">#</span>
<span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">array</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
<span class="kn">from</span> <span class="nn">pyspark</span> <span class="kn">import</span> <span class="n">SparkContext</span>
<span class="kn">from</span> <span class="nn">pyspark.rdd</span> <span class="kn">import</span> <span class="n">ignore_unicode_prefix</span>
<span class="kn">from</span> <span class="nn">pyspark.mllib.common</span> <span class="kn">import</span> <span class="n">JavaModelWrapper</span><span class="p">,</span> <span class="n">callMLlibFunc</span><span class="p">,</span> <span class="n">inherit_doc</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">'FPGrowth'</span><span class="p">,</span> <span class="s">'FPGrowthModel'</span><span class="p">]</span>
<span class="nd">@inherit_doc</span>
<span class="nd">@ignore_unicode_prefix</span>
<div class="viewcode-block" id="FPGrowthModel"><a class="viewcode-back" href="../../../pyspark.mllib.html#pyspark.mllib.fpm.FPGrowthModel">[docs]</a><span class="k">class</span> <span class="nc">FPGrowthModel</span><span class="p">(</span><span class="n">JavaModelWrapper</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> .. note:: Experimental</span>
<span class="sd"> A FP-Growth model for mining frequent itemsets</span>
<span class="sd"> using the Parallel FP-Growth algorithm.</span>
<span class="sd"> >>> data = [["a", "b", "c"], ["a", "b", "d", "e"], ["a", "c", "e"], ["a", "c", "f"]]</span>
<span class="sd"> >>> rdd = sc.parallelize(data, 2)</span>
<span class="sd"> >>> model = FPGrowth.train(rdd, 0.6, 2)</span>
<span class="sd"> >>> sorted(model.freqItemsets().collect())</span>
<span class="sd"> [FreqItemset(items=[u'a'], freq=4), FreqItemset(items=[u'c'], freq=3), ...</span>
<span class="sd"> """</span>
<div class="viewcode-block" id="FPGrowthModel.freqItemsets"><a class="viewcode-back" href="../../../pyspark.mllib.html#pyspark.mllib.fpm.FPGrowthModel.freqItemsets">[docs]</a> <span class="k">def</span> <span class="nf">freqItemsets</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Returns the frequent itemsets of this model.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s">"getFreqItemsets"</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">FPGrowth</span><span class="o">.</span><span class="n">FreqItemset</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="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">])))</span>
</div></div>
<div class="viewcode-block" id="FPGrowth"><a class="viewcode-back" href="../../../pyspark.mllib.html#pyspark.mllib.fpm.FPGrowth">[docs]</a><span class="k">class</span> <span class="nc">FPGrowth</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> .. note:: Experimental</span>
<span class="sd"> A Parallel FP-growth algorithm to mine frequent itemsets.</span>
<span class="sd"> """</span>
<span class="nd">@classmethod</span>
<div class="viewcode-block" id="FPGrowth.train"><a class="viewcode-back" href="../../../pyspark.mllib.html#pyspark.mllib.fpm.FPGrowth.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">minSupport</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">numPartitions</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Computes an FP-Growth model that contains frequent itemsets.</span>
<span class="sd"> :param data: The input data set, each element contains a</span>
<span class="sd"> transaction.</span>
<span class="sd"> :param minSupport: The minimal support level (default: `0.3`).</span>
<span class="sd"> :param numPartitions: The number of partitions used by</span>
<span class="sd"> parallel FP-growth (default: same as input data).</span>
<span class="sd"> """</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">callMLlibFunc</span><span class="p">(</span><span class="s">"trainFPGrowthModel"</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="n">minSupport</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">numPartitions</span><span class="p">))</span>
<span class="k">return</span> <span class="n">FPGrowthModel</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="FPGrowth.FreqItemset"><a class="viewcode-back" href="../../../pyspark.mllib.html#pyspark.mllib.fpm.FPGrowth.FreqItemset">[docs]</a> <span class="k">class</span> <span class="nc">FreqItemset</span><span class="p">(</span><span class="n">namedtuple</span><span class="p">(</span><span class="s">"FreqItemset"</span><span class="p">,</span> <span class="p">[</span><span class="s">"items"</span><span class="p">,</span> <span class="s">"freq"</span><span class="p">])):</span>
<span class="sd">"""</span>
<span class="sd"> Represents an (items, freq) tuple.</span>
<span class="sd"> """</span>
</div></div>
<span class="k">def</span> <span class="nf">_test</span><span class="p">():</span>
<span class="kn">import</span> <span class="nn">doctest</span>
<span class="kn">import</span> <span class="nn">pyspark.mllib.fpm</span>
<span class="n">globs</span> <span class="o">=</span> <span class="n">pyspark</span><span class="o">.</span><span class="n">mllib</span><span class="o">.</span><span class="n">fpm</span><span class="o">.</span><span class="n">__dict__</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="n">globs</span><span class="p">[</span><span class="s">'sc'</span><span class="p">]</span> <span class="o">=</span> <span class="n">SparkContext</span><span class="p">(</span><span class="s">'local[4]'</span><span class="p">,</span> <span class="s">'PythonTest'</span><span class="p">)</span>
<span class="p">(</span><span class="n">failure_count</span><span class="p">,</span> <span class="n">test_count</span><span class="p">)</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">(</span><span class="n">globs</span><span class="o">=</span><span class="n">globs</span><span class="p">,</span> <span class="n">optionflags</span><span class="o">=</span><span class="n">doctest</span><span class="o">.</span><span class="n">ELLIPSIS</span><span class="p">)</span>
<span class="n">globs</span><span class="p">[</span><span class="s">'sc'</span><span class="p">]</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
<span class="k">if</span> <span class="n">failure_count</span><span class="p">:</span>
<span class="nb">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>
<span class="n">_test</span><span class="p">()</span>
</pre></div>
</div>
</div>
</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/spark-logo-hd.png" alt="Logo"/>
</a></p>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<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>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#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="nav-item nav-item-0"><a href="../../../index.html">PySpark master 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 .
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
</div>
</body>
</html>