Github user freeman-lab commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5267#discussion_r32357267
  
    --- Diff: python/pyspark/mllib/clustering.py ---
    @@ -192,6 +196,107 @@ def train(cls, rdd, k, convergenceTol=1e-3, 
maxIterations=100, seed=None):
             return GaussianMixtureModel(weight, mvg_obj)
     
     
    +@inherit_doc
    +class HierarchicalClusteringModel(JavaModelWrapper, JavaSaveable, 
JavaLoader):
    +
    +    """A clustering model derived from the hierarchical clustering method.
    +
    +    >>> data = array([0.0,0.0, 1.0,1.0, 9.0,8.0, 8.0,9.0]).reshape(4, 2)
    +    >>> rdd = sc.parallelize(data)
    +    >>> model = HierarchicalClustering.train(rdd, 2)
    +    >>> len(model.clusterCenters)
    +    2
    +    >>> model.predict(array([0.0, 0.0])) == model.predict(array([1.0, 
1.0]))
    +    True
    +    >>> model.predict(array([8.0, 9.0])) == model.predict(array([9.0, 
8.0]))
    +    True
    +    >>> abs(model.WSSSE(rdd) - 2.82842712) < 10e-8
    +    True
    +    >>> len(model.toLinkageMatrix())
    +    1
    +    >>> len(model.toAdjacencyList())
    +    2
    +
    +    >>> sparse_data = [
    +    ...     SparseVector(3, {1: 1.0}),
    +    ...     SparseVector(3, {1: 1.1}),
    +    ...     SparseVector(3, {2: 1.0}),
    +    ...     SparseVector(3, {2: 1.1})
    +    ... ]
    +    >>> sparse_rdd = sc.parallelize(sparse_data)
    +    >>> model = HierarchicalClustering.train(sparse_rdd, 2)
    +    >>> model.predict(array([0., 1., 0.])) == model.predict(array([0, 1.1, 
0.]))
    +    True
    +    >>> model.predict(array([0., 0., 1.])) == model.predict(array([0, 0, 
1.1]))
    +    True
    +    >>> model.predict(sparse_data[0]) == model.predict(sparse_data[1])
    +    True
    +    >>> model.predict(sparse_data[2]) == model.predict(sparse_data[3])
    +    True
    +    >>> len(model.clusterCenters)
    +    2
    +    >>> abs(model.WSSSE(sparse_rdd) - 0.2) < 10e-2
    +    True
    +    >>> len(model.toLinkageMatrix())
    +    1
    +    >>> len(model.toAdjacencyList())
    +    2
    +
    +    >>> import os, tempfile
    +    >>> path = os.path.join(tempfile.gettempdir(), str(id(model)))
    +    >>> model.save(sc, path)
    +    >>> sameModel = HierarchicalClusteringModel.load(sc, path)
    +    >>> sameModel.predict(sparse_data[0]) == model.predict(sparse_data[0])
    +    True
    +    >>> try:
    +    ...     os.removedirs(path)
    +    ... except OSError:
    +    ...     pass
    +    """
    +
    +    def predict(self, x):
    +        """Find the cluster to which x belongs in this model."""
    +        if isinstance(x, RDD):
    +            return self.call("predict", x.map(_convert_to_vector))
    +        else:
    +            return self.call("predict", _convert_to_vector(x))
    +
    +    def toAdjacencyList(self):
    +        """Convert a cluster dendrogram to a adjacency list with distances 
as their weights."""
    +        return self.call("toJavaAdjacencyList")
    +
    +    def toLinkageMatrix(self):
    +        return self.call("toJavaLinkageMatrix")
    +
    +    @property
    +    def clusterCenters(self):
    +        """Get the cluster centers, represented as a list of NumPy 
arrays."""
    +        centers = _java2py(self._sc, self.call("getCenters"))
    +        return [c.toArray() for c in centers]
    +
    +    def WSSSE(self, rdd):
    +        """Get Within Set Sum of Squared Error (WSSSE)."""
    +        return self.call("WSSSE", rdd.map(_convert_to_vector))
    +
    +    def save(self, sc, path):
    +        return self.call("save", sc, path)
    +
    +    @classmethod
    +    def load(cls, sc, path):
    +        java_model = sc._jvm.org.apache.spark.mllib.clustering \
    +            .HierarchicalClusteringModel.load(sc._jsc.sc(), path)
    +        return HierarchicalClusteringModel(java_model)
    +
    +
    +class HierarchicalClustering(object):
    +
    +    @classmethod
    +    def train(cls, rdd, k, maxIterations=100, maxRetries=10, seed=None):
    +        model = callMLlibFunc("trainHierarchicalClusteringModel", 
rdd.map(_convert_to_vector),
    --- End diff --
    
    Add a brief docstring, e.g. `Train a hierarchical clustering model.` Should 
really describe all arguments in the docstring, but I noticed that so far we 
don't do that for the other PySpark mllib algorithms.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to