<!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.tuning — 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.tuning</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">itertools</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</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="p">,</span> <span class="n">Param</span>
<span class="kn">from</span> <span class="nn">pyspark.ml</span> <span class="kn">import</span> <span class="n">Estimator</span><span class="p">,</span> <span class="n">Model</span>
<span class="kn">from</span> <span class="nn">pyspark.ml.util</span> <span class="kn">import</span> <span class="n">keyword_only</span>
<span class="kn">from</span> <span class="nn">pyspark.sql.functions</span> <span class="kn">import</span> <span class="n">rand</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">'ParamGridBuilder'</span><span class="p">,</span> <span class="s">'CrossValidator'</span><span class="p">,</span> <span class="s">'CrossValidatorModel'</span><span class="p">]</span>
<div class="viewcode-block" id="ParamGridBuilder"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.ParamGridBuilder">[docs]</a><span class="k">class</span> <span class="nc">ParamGridBuilder</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">r"""</span>
<span class="sd"> Builder for a param grid used in grid search-based model selection.</span>
<span class="sd"> >>> from pyspark.ml.classification import LogisticRegression</span>
<span class="sd"> >>> lr = LogisticRegression()</span>
<span class="sd"> >>> output = ParamGridBuilder() \</span>
<span class="sd"> ... .baseOn({lr.labelCol: 'l'}) \</span>
<span class="sd"> ... .baseOn([lr.predictionCol, 'p']) \</span>
<span class="sd"> ... .addGrid(lr.regParam, [1.0, 2.0]) \</span>
<span class="sd"> ... .addGrid(lr.maxIter, [1, 5]) \</span>
<span class="sd"> ... .build()</span>
<span class="sd"> >>> expected = [</span>
<span class="sd"> ... {lr.regParam: 1.0, lr.maxIter: 1, lr.labelCol: 'l', lr.predictionCol: 'p'},</span>
<span class="sd"> ... {lr.regParam: 2.0, lr.maxIter: 1, lr.labelCol: 'l', lr.predictionCol: 'p'},</span>
<span class="sd"> ... {lr.regParam: 1.0, lr.maxIter: 5, lr.labelCol: 'l', lr.predictionCol: 'p'},</span>
<span class="sd"> ... {lr.regParam: 2.0, lr.maxIter: 5, lr.labelCol: 'l', lr.predictionCol: 'p'}]</span>
<span class="sd"> >>> len(output) == len(expected)</span>
<span class="sd"> True</span>
<span class="sd"> >>> all([m in expected for m in output])</span>
<span class="sd"> True</span>
<span class="sd"> """</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="bp">self</span><span class="o">.</span><span class="n">_param_grid</span> <span class="o">=</span> <span class="p">{}</span>
<div class="viewcode-block" id="ParamGridBuilder.addGrid"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.ParamGridBuilder.addGrid">[docs]</a> <span class="k">def</span> <span class="nf">addGrid</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">values</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Sets the given parameters in this grid to fixed values.</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_param_grid</span><span class="p">[</span><span class="n">param</span><span class="p">]</span> <span class="o">=</span> <span class="n">values</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="ParamGridBuilder.baseOn"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.ParamGridBuilder.baseOn">[docs]</a> <span class="k">def</span> <span class="nf">baseOn</span><span class="p">(</span><span class="bp">self</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"> Sets the given parameters in this grid to fixed values.</span>
<span class="sd"> Accepts either a parameter dictionary or a list of (parameter, value) pairs.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">dict</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">baseOn</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">for</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="ow">in</span> <span class="n">args</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">addGrid</span><span class="p">(</span><span class="n">param</span><span class="p">,</span> <span class="p">[</span><span class="n">value</span><span class="p">])</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="ParamGridBuilder.build"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.ParamGridBuilder.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="sd">"""</span>
<span class="sd"> Builds and returns all combinations of parameters specified</span>
<span class="sd"> by the param grid.</span>
<span class="sd"> """</span>
<span class="n">keys</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_param_grid</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="n">grid_values</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_param_grid</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
<span class="k">return</span> <span class="p">[</span><span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="n">prod</span><span class="p">))</span> <span class="k">for</span> <span class="n">prod</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">grid_values</span><span class="p">)]</span>
</div></div>
<div class="viewcode-block" id="CrossValidator"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator">[docs]</a><span class="k">class</span> <span class="nc">CrossValidator</span><span class="p">(</span><span class="n">Estimator</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> K-fold cross validation.</span>
<span class="sd"> >>> from pyspark.ml.classification import LogisticRegression</span>
<span class="sd"> >>> from pyspark.ml.evaluation import BinaryClassificationEvaluator</span>
<span class="sd"> >>> from pyspark.mllib.linalg import Vectors</span>
<span class="sd"> >>> dataset = sqlContext.createDataFrame(</span>
<span class="sd"> ... [(Vectors.dense([0.0]), 0.0),</span>
<span class="sd"> ... (Vectors.dense([0.4]), 1.0),</span>
<span class="sd"> ... (Vectors.dense([0.5]), 0.0),</span>
<span class="sd"> ... (Vectors.dense([0.6]), 1.0),</span>
<span class="sd"> ... (Vectors.dense([1.0]), 1.0)] * 10,</span>
<span class="sd"> ... ["features", "label"])</span>
<span class="sd"> >>> lr = LogisticRegression()</span>
<span class="sd"> >>> grid = ParamGridBuilder().addGrid(lr.maxIter, [0, 1]).build()</span>
<span class="sd"> >>> evaluator = BinaryClassificationEvaluator()</span>
<span class="sd"> >>> cv = CrossValidator(estimator=lr, estimatorParamMaps=grid, evaluator=evaluator)</span>
<span class="sd"> >>> cvModel = cv.fit(dataset)</span>
<span class="sd"> >>> evaluator.evaluate(cvModel.transform(dataset))</span>
<span class="sd"> 0.8333...</span>
<span class="sd"> """</span>
<span class="c"># a placeholder to make it appear in the generated doc</span>
<span class="n">estimator</span> <span class="o">=</span> <span class="n">Param</span><span class="p">(</span><span class="n">Params</span><span class="o">.</span><span class="n">_dummy</span><span class="p">(),</span> <span class="s">"estimator"</span><span class="p">,</span> <span class="s">"estimator to be cross-validated"</span><span class="p">)</span>
<span class="c"># a placeholder to make it appear in the generated doc</span>
<span class="n">estimatorParamMaps</span> <span class="o">=</span> <span class="n">Param</span><span class="p">(</span><span class="n">Params</span><span class="o">.</span><span class="n">_dummy</span><span class="p">(),</span> <span class="s">"estimatorParamMaps"</span><span class="p">,</span> <span class="s">"estimator param maps"</span><span class="p">)</span>
<span class="c"># a placeholder to make it appear in the generated doc</span>
<span class="n">evaluator</span> <span class="o">=</span> <span class="n">Param</span><span class="p">(</span>
<span class="n">Params</span><span class="o">.</span><span class="n">_dummy</span><span class="p">(),</span> <span class="s">"evaluator"</span><span class="p">,</span>
<span class="s">"evaluator used to select hyper-parameters that maximize the cross-validated metric"</span><span class="p">)</span>
<span class="c"># a placeholder to make it appear in the generated doc</span>
<span class="n">numFolds</span> <span class="o">=</span> <span class="n">Param</span><span class="p">(</span><span class="n">Params</span><span class="o">.</span><span class="n">_dummy</span><span class="p">(),</span> <span class="s">"numFolds"</span><span class="p">,</span> <span class="s">"number of folds for cross validation"</span><span class="p">)</span>
<span class="nd">@keyword_only</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">estimator</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">estimatorParamMaps</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">evaluator</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">numFolds</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> __init__(self, estimator=None, estimatorParamMaps=None, evaluator=None, numFolds=3)</span>
<span class="sd"> """</span>
<span class="nb">super</span><span class="p">(</span><span class="n">CrossValidator</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="c">#: param for estimator to be cross-validated</span>
<span class="bp">self</span><span class="o">.</span><span class="n">estimator</span> <span class="o">=</span> <span class="n">Param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">"estimator"</span><span class="p">,</span> <span class="s">"estimator to be cross-validated"</span><span class="p">)</span>
<span class="c">#: param for estimator param maps</span>
<span class="bp">self</span><span class="o">.</span><span class="n">estimatorParamMaps</span> <span class="o">=</span> <span class="n">Param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">"estimatorParamMaps"</span><span class="p">,</span> <span class="s">"estimator param maps"</span><span class="p">)</span>
<span class="c">#: param for the evaluator used to select hyper-parameters that</span>
<span class="c">#: maximize the cross-validated metric</span>
<span class="bp">self</span><span class="o">.</span><span class="n">evaluator</span> <span class="o">=</span> <span class="n">Param</span><span class="p">(</span>
<span class="bp">self</span><span class="p">,</span> <span class="s">"evaluator"</span><span class="p">,</span>
<span class="s">"evaluator used to select hyper-parameters that maximize the cross-validated metric"</span><span class="p">)</span>
<span class="c">#: param for number of folds for cross validation</span>
<span class="bp">self</span><span class="o">.</span><span class="n">numFolds</span> <span class="o">=</span> <span class="n">Param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">"numFolds"</span><span class="p">,</span> <span class="s">"number of folds for cross validation"</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_setDefault</span><span class="p">(</span><span class="n">numFolds</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">kwargs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__init__</span><span class="o">.</span><span class="n">_input_kwargs</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_set</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="nd">@keyword_only</span>
<div class="viewcode-block" id="CrossValidator.setParams"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.setParams">[docs]</a> <span class="k">def</span> <span class="nf">setParams</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">estimator</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">estimatorParamMaps</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">evaluator</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">numFolds</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> setParams(self, estimator=None, estimatorParamMaps=None, evaluator=None, numFolds=3):</span>
<span class="sd"> Sets params for cross validator.</span>
<span class="sd"> """</span>
<span class="n">kwargs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">setParams</span><span class="o">.</span><span class="n">_input_kwargs</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_set</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="CrossValidator.setEstimator"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.setEstimator">[docs]</a> <span class="k">def</span> <span class="nf">setEstimator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Sets the value of :py:attr:`estimator`.</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_paramMap</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="CrossValidator.getEstimator"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.getEstimator">[docs]</a> <span class="k">def</span> <span class="nf">getEstimator</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Gets the value of estimator or its default value.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="CrossValidator.setEstimatorParamMaps"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.setEstimatorParamMaps">[docs]</a> <span class="k">def</span> <span class="nf">setEstimatorParamMaps</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Sets the value of :py:attr:`estimatorParamMaps`.</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_paramMap</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">estimatorParamMaps</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="CrossValidator.getEstimatorParamMaps"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.getEstimatorParamMaps">[docs]</a> <span class="k">def</span> <span class="nf">getEstimatorParamMaps</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Gets the value of estimatorParamMaps or its default value.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimatorParamMaps</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="CrossValidator.setEvaluator"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.setEvaluator">[docs]</a> <span class="k">def</span> <span class="nf">setEvaluator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Sets the value of :py:attr:`evaluator`.</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_paramMap</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">evaluator</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="CrossValidator.getEvaluator"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.getEvaluator">[docs]</a> <span class="k">def</span> <span class="nf">getEvaluator</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Gets the value of evaluator or its default value.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">evaluator</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="CrossValidator.setNumFolds"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.setNumFolds">[docs]</a> <span class="k">def</span> <span class="nf">setNumFolds</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Sets the value of :py:attr:`numFolds`.</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_paramMap</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">numFolds</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="CrossValidator.getNumFolds"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.getNumFolds">[docs]</a> <span class="k">def</span> <span class="nf">getNumFolds</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Gets the value of numFolds or its default value.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">numFolds</span><span class="p">)</span>
</div>
<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">est</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">)</span>
<span class="n">epm</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimatorParamMaps</span><span class="p">)</span>
<span class="n">numModels</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">epm</span><span class="p">)</span>
<span class="n">eva</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">evaluator</span><span class="p">)</span>
<span class="n">nFolds</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getOrDefault</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">numFolds</span><span class="p">)</span>
<span class="n">h</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="n">nFolds</span>
<span class="n">randCol</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uid</span> <span class="o">+</span> <span class="s">"_rand"</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">dataset</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s">"*"</span><span class="p">,</span> <span class="n">rand</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="n">randCol</span><span class="p">))</span>
<span class="n">metrics</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">numModels</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="n">nFolds</span><span class="p">):</span>
<span class="n">validateLB</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">h</span>
<span class="n">validateUB</span> <span class="o">=</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">h</span>
<span class="n">condition</span> <span class="o">=</span> <span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="n">randCol</span><span class="p">]</span> <span class="o">>=</span> <span class="n">validateLB</span><span class="p">)</span> <span class="o">&</span> <span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="n">randCol</span><span class="p">]</span> <span class="o"><</span> <span class="n">validateUB</span><span class="p">)</span>
<span class="n">validation</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">condition</span><span class="p">)</span>
<span class="n">train</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">~</span><span class="n">condition</span><span class="p">)</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">numModels</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">est</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">train</span><span class="p">,</span> <span class="n">epm</span><span class="p">[</span><span class="n">j</span><span class="p">])</span>
<span class="c"># TODO: duplicate evaluator to take extra params from input</span>
<span class="n">metric</span> <span class="o">=</span> <span class="n">eva</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">validation</span><span class="p">,</span> <span class="n">epm</span><span class="p">[</span><span class="n">j</span><span class="p">]))</span>
<span class="n">metrics</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+=</span> <span class="n">metric</span>
<span class="k">if</span> <span class="n">eva</span><span class="o">.</span><span class="n">isLargerBetter</span><span class="p">():</span>
<span class="n">bestIndex</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">metrics</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">bestIndex</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">metrics</span><span class="p">)</span>
<span class="n">bestModel</span> <span class="o">=</span> <span class="n">est</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">epm</span><span class="p">[</span><span class="n">bestIndex</span><span class="p">])</span>
<span class="k">return</span> <span class="n">CrossValidatorModel</span><span class="p">(</span><span class="n">bestModel</span><span class="p">)</span>
<div class="viewcode-block" id="CrossValidator.copy"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidator.copy">[docs]</a> <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="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">newCV</span> <span class="o">=</span> <span class="n">Params</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">extra</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isSet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">):</span>
<span class="n">newCV</span><span class="o">.</span><span class="n">setEstimator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">getEstimator</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="c"># estimatorParamMaps remain the same</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isSet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">evaluator</span><span class="p">):</span>
<span class="n">newCV</span><span class="o">.</span><span class="n">setEvaluator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">getEvaluator</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="k">return</span> <span class="n">newCV</span>
</div></div>
<div class="viewcode-block" id="CrossValidatorModel"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidatorModel">[docs]</a><span class="k">class</span> <span class="nc">CrossValidatorModel</span><span class="p">(</span><span class="n">Model</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Model from k-fold cross validation.</span>
<span class="sd"> """</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">bestModel</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">CrossValidatorModel</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="c">#: best model from cross validation</span>
<span class="bp">self</span><span class="o">.</span><span class="n">bestModel</span> <span class="o">=</span> <span class="n">bestModel</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">bestModel</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">dataset</span><span class="p">)</span>
<div class="viewcode-block" id="CrossValidatorModel.copy"><a class="viewcode-back" href="../../../pyspark.ml.html#pyspark.ml.tuning.CrossValidatorModel.copy">[docs]</a> <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 a randomly generated uid</span>
<span class="sd"> and some extra params. This copies the underlying bestModel,</span>
<span class="sd"> creates a deep copy of the embedded paramMap, and</span>
<span class="sd"> copies the embedded and extra parameters over.</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="k">return</span> <span class="n">CrossValidatorModel</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bestModel</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">extra</span><span class="p">))</span>
</div></div>
<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="kn">import</span> <span class="nn">doctest</span>
<span class="kn">from</span> <span class="nn">pyspark.context</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">SQLContext</span>
<span class="n">globs</span> <span class="o">=</span> <span class="nb">globals</span><span class="p">()</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="c"># The small batch size here ensures that we see multiple batches,</span>
<span class="c"># even in these small test examples:</span>
<span class="n">sc</span> <span class="o">=</span> <span class="n">SparkContext</span><span class="p">(</span><span class="s">"local[2]"</span><span class="p">,</span> <span class="s">"ml.tuning tests"</span><span class="p">)</span>
<span class="n">sqlContext</span> <span class="o">=</span> <span class="n">SQLContext</span><span class="p">(</span><span class="n">sc</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">sc</span>
<span class="n">globs</span><span class="p">[</span><span class="s">'sqlContext'</span><span class="p">]</span> <span class="o">=</span> <span class="n">sqlContext</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">sc</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>
</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>