<!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.ml.wrapper — 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.ml.wrapper</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">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">ABCMeta</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.sql</span> <span class="kn">import</span> <span class="n">DataFrame</span>
<span class="kn">from</span> <span class="nn">pyspark.ml.param</span> <span class="kn">import</span> <span class="n">Params</span>
<span class="kn">from</span> <span class="nn">pyspark.ml.pipeline</span> <span class="kn">import</span> <span class="n">Estimator</span><span class="p">,</span> <span class="n">Transformer</span><span class="p">,</span> <span class="n">Model</span>
<span class="kn">from</span> <span class="nn">pyspark.mllib.common</span> <span class="kn">import</span> <span class="n">inherit_doc</span><span class="p">,</span> <span class="n">_java2py</span><span class="p">,</span> <span class="n">_py2java</span>
<span class="k">def</span> <span class="nf">_jvm</span><span class="p">():</span>
<span class="sd">"""</span>
<span class="sd"> Returns the JVM view associated with SparkContext. Must be called</span>
<span class="sd"> after SparkContext is initialized.</span>
<span class="sd"> """</span>
<span class="n">jvm</span> <span class="o">=</span> <span class="n">SparkContext</span><span class="o">.</span><span class="n">_jvm</span>
<span class="k">if</span> <span class="n">jvm</span><span class="p">:</span>
<span class="k">return</span> <span class="n">jvm</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s">"Cannot load _jvm from SparkContext. Is SparkContext initialized?"</span><span class="p">)</span>
<span class="nd">@inherit_doc</span>
<span class="k">class</span> <span class="nc">JavaWrapper</span><span class="p">(</span><span class="n">Params</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Utility class to help create wrapper classes from Java/Scala</span>
<span class="sd"> implementations of pipeline components.</span>
<span class="sd"> """</span>
<span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">ABCMeta</span>
<span class="c">#: The wrapped Java companion object. Subclasses should initialize</span>
<span class="c">#: it properly. The param values in the Java object should be</span>
<span class="c">#: synced with the Python wrapper in fit/transform/evaluate/copy.</span>
<span class="n">_java_obj</span> <span class="o">=</span> <span class="bp">None</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">_new_java_obj</span><span class="p">(</span><span class="n">java_class</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Construct a new Java object.</span>
<span class="sd"> """</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SparkContext</span><span class="o">.</span><span class="n">_active_spark_context</span>
<span class="n">java_obj</span> <span class="o">=</span> <span class="n">_jvm</span><span class="p">()</span>
<span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">java_class</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">"."</span><span class="p">):</span>
<span class="n">java_obj</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">java_obj</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
<span class="n">java_args</span> <span class="o">=</span> <span class="p">[</span><span class="n">_py2java</span><span class="p">(</span><span class="n">sc</span><span class="p">,</span> <span class="n">arg</span><span class="p">)</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">]</span>
<span class="k">return</span> <span class="n">java_obj</span><span class="p">(</span><span class="o">*</span><span class="n">java_args</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_make_java_param_pair</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Makes a Java parm pair.</span>
<span class="sd"> """</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SparkContext</span><span class="o">.</span><span class="n">_active_spark_context</span>
<span class="n">param</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resolveParam</span><span class="p">(</span><span class="n">param</span><span class="p">)</span>
<span class="n">java_param</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="o">.</span><span class="n">getParam</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="n">java_value</span> <span class="o">=</span> <span class="n">_py2java</span><span class="p">(</span><span class="n">sc</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="k">return</span> <span class="n">java_param</span><span class="o">.</span><span class="n">w</span><span class="p">(</span><span class="n">java_value</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_transfer_params_to_java</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Transforms the embedded params to the companion Java object.</span>
<span class="sd"> """</span>
<span class="n">paramMap</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extractParamMap</span><span class="p">()</span>
<span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
<span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">paramMap</span><span class="p">:</span>
<span class="n">pair</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_java_param_pair</span><span class="p">(</span><span class="n">param</span><span class="p">,</span> <span class="n">paramMap</span><span class="p">[</span><span class="n">param</span><span class="p">])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">pair</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_transfer_params_from_java</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Transforms the embedded params from the companion Java object.</span>
<span class="sd"> """</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SparkContext</span><span class="o">.</span><span class="n">_active_spark_context</span>
<span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="o">.</span><span class="n">hasParam</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">name</span><span class="p">):</span>
<span class="n">java_param</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="o">.</span><span class="n">getParam</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">_java2py</span><span class="p">(</span><span class="n">sc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="n">java_param</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_paramMap</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">_empty_java_param_map</span><span class="p">():</span>
<span class="sd">"""</span>
<span class="sd"> Returns an empty Java ParamMap reference.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="n">_jvm</span><span class="p">()</span><span class="o">.</span><span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">spark</span><span class="o">.</span><span class="n">ml</span><span class="o">.</span><span class="n">param</span><span class="o">.</span><span class="n">ParamMap</span><span class="p">()</span>
<span class="nd">@inherit_doc</span>
<span class="k">class</span> <span class="nc">JavaEstimator</span><span class="p">(</span><span class="n">Estimator</span><span class="p">,</span> <span class="n">JavaWrapper</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Base class for :py:class:`Estimator`s that wrap Java/Scala</span>
<span class="sd"> implementations.</span>
<span class="sd"> """</span>
<span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">ABCMeta</span>
<span class="k">def</span> <span class="nf">_create_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">java_model</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Creates a model from the input Java model reference.</span>
<span class="sd"> """</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">_fit_java</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Fits a Java model to the input dataset.</span>
<span class="sd"> :param dataset: input dataset, which is an instance of</span>
<span class="sd"> :py:class:`pyspark.sql.DataFrame`</span>
<span class="sd"> :param params: additional params (overwriting embedded values)</span>
<span class="sd"> :return: fitted Java model</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_transfer_params_to_java</span><span class="p">()</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">dataset</span><span class="o">.</span><span class="n">_jdf</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset</span><span class="p">):</span>
<span class="n">java_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fit_java</span><span class="p">(</span><span class="n">dataset</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_model</span><span class="p">(</span><span class="n">java_model</span><span class="p">)</span>
<span class="nd">@inherit_doc</span>
<span class="k">class</span> <span class="nc">JavaTransformer</span><span class="p">(</span><span class="n">Transformer</span><span class="p">,</span> <span class="n">JavaWrapper</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Base class for :py:class:`Transformer`s that wrap Java/Scala</span>
<span class="sd"> implementations.</span>
<span class="sd"> """</span>
<span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">ABCMeta</span>
<span class="k">def</span> <span class="nf">_transform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataset</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_transfer_params_to_java</span><span class="p">()</span>
<span class="k">return</span> <span class="n">DataFrame</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">dataset</span><span class="o">.</span><span class="n">_jdf</span><span class="p">),</span> <span class="n">dataset</span><span class="o">.</span><span class="n">sql_ctx</span><span class="p">)</span>
<span class="nd">@inherit_doc</span>
<span class="k">class</span> <span class="nc">JavaModel</span><span class="p">(</span><span class="n">Model</span><span class="p">,</span> <span class="n">JavaTransformer</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Base class for :py:class:`Model`s that wrap Java/Scala</span>
<span class="sd"> implementations. Subclasses should inherit this class before</span>
<span class="sd"> param mix-ins, because this sets the UID from the Java model.</span>
<span class="sd"> """</span>
<span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">ABCMeta</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">java_model</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Initialize this instance with a Java model object.</span>
<span class="sd"> Subclasses should call this constructor, initialize params,</span>
<span class="sd"> and then call _transformer_params_from_java.</span>
<span class="sd"> """</span>
<span class="nb">super</span><span class="p">(</span><span class="n">JavaModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span> <span class="o">=</span> <span class="n">java_model</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="n">java_model</span><span class="o">.</span><span class="n">uid</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Creates a copy of this instance with the same uid and some</span>
<span class="sd"> extra params. This implementation first calls Params.copy and</span>
<span class="sd"> then make a copy of the companion Java model with extra params.</span>
<span class="sd"> So both the Python wrapper and the Java model get copied.</span>
<span class="sd"> :param extra: Extra parameters to copy to the new instance</span>
<span class="sd"> :return: Copy of this instance</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">extra</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">extra</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
<span class="n">that</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">JavaModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">extra</span><span class="p">)</span>
<span class="n">that</span><span class="o">.</span><span class="n">_java_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_empty_java_param_map</span><span class="p">())</span>
<span class="n">that</span><span class="o">.</span><span class="n">_transfer_params_to_java</span><span class="p">()</span>
<span class="k">return</span> <span class="n">that</span>
<span class="k">def</span> <span class="nf">_call_java</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">args</span><span class="p">):</span>
<span class="n">m</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_java_obj</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SparkContext</span><span class="o">.</span><span class="n">_active_spark_context</span>
<span class="n">java_args</span> <span class="o">=</span> <span class="p">[</span><span class="n">_py2java</span><span class="p">(</span><span class="n">sc</span><span class="p">,</span> <span class="n">arg</span><span class="p">)</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">]</span>
<span class="k">return</span> <span class="n">_java2py</span><span class="p">(</span><span class="n">sc</span><span class="p">,</span> <span class="n">m</span><span class="p">(</span><span class="o">*</span><span class="n">java_args</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>