Project: Apache Spark (App Definition and Development / Streaming & Messaging)
File: ml-clustering.md

layout: global title: Clustering displayTitle: Clustering license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. This page describes clustering algorithms in MLlib. The also has relevant information about these algorithms. Table of Contents This will become a table of contents (this text will be scraped). {:toc} is one of the most commonly used clustering algorithms that clusters the data points into a predefined number of clusters. The MLlib implementation includes a parallelized variant of the method called . `KMeans` is implemented as an `Estimator` and generates a `KMeansModel` as the base model. <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> </tr> </thead> <tbody> <tr> <td>featuresCol</td> <td>Vector</td> <td>"features"</td> <td>Feature vector</td> </tr> </tbody> </table> <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> </tr> </thead> <tbody> <tr> <td>predictionCol</td> <td>Int</td> <td>"prediction"</td> <td>Predicted cluster center</td> </tr> </tbody> </table> Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/kmeansexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/KMeansExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaKMeansExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/kmeans.R %} </div> </div> `LDA` is implemented as an `Estimator` that supports both `EMLDAOptimizer` and `OnlineLDAOptimizer`, and generates a `LDAModel` as the base model. Expert users may cast a `LDAModel` generated by `EMLDAOptimizer` to a `DistributedLDAModel` if needed. Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/ldaexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/LDAExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaLDAExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example

%} </div> </div> Bisecting k-means is a kind of using a divisive (or "top-down") approach: all observations start in one cluster, and splits are performed recursively as one moves down the hierarchy. Bisecting K-means can often be much faster than regular K-means, but it will generally produce a different clustering. `BisectingKMeans` is implemented as an `Estimator` and generates a `BisectingKMeansModel` as the base model. Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/bisectingkmeansexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/BisectingKMeansExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaBisectingKMeansExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/bisectingKmeans.R %} </div> </div> A represents a composite distribution whereby points are drawn from one of k Gaussian sub-distributions, each with its own probability. The `spark.ml` implementation uses the algorithm to induce the maximum-likelihood model given a set of samples. `GaussianMixture` is implemented as an `Estimator` and generates a `GaussianMixtureModel` as the base model. <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> </tr> </thead> <tbody> <tr> <td>featuresCol</td> <td>Vector</td> <td>"features"</td> <td>Feature vector</td> </tr> </tbody> </table> <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> </tr> </thead> <tbody> <tr> <td>predictionCol</td> <td>Int</td> <td>"prediction"</td> <td>Predicted cluster center</td> </tr> <tr> <td>probabilityCol</td> <td>Vector</td> <td>"probability"</td> <td>Probability of each cluster</td> </tr> </tbody> </table> Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/gaussianmixture_example.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/GaussianMixtureExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaGaussianMixtureExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/gaussianMixture.R %} </div> </div> Power Iteration Clustering (PIC) is a scalable graph clustering algorithm developed by . From the abstract: PIC finds a very low-dimensional embedding of a dataset using truncated power iteration on a normalized pair-wise similarity matrix of the data. `spark.ml`'s PowerIterationClustering implementation takes the following parameters: `k`: the number of clusters to create `initMode`: param for the initialization algorithm `maxIter`: param for maximum number of iterations `srcCol`: param for the name of the input column for source vertex IDs `dstCol`: name of the input column for destination vertex IDs `weightCol`: Param for weight column name Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/poweriterationclusteringexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/PowerIterationClusteringExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaPowerIterationClusteringExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/powerIterationClustering.R %} </div> </div>