Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/10150#discussion_r48097828
--- Diff: python/pyspark/mllib/clustering.py ---
@@ -38,13 +38,158 @@
from pyspark.mllib.util import Saveable, Loader, inherit_doc, JavaLoader,
JavaSaveable
from pyspark.streaming import DStream
-__all__ = ['KMeansModel', 'KMeans', 'GaussianMixtureModel',
'GaussianMixture',
- 'PowerIterationClusteringModel', 'PowerIterationClustering',
- 'StreamingKMeans', 'StreamingKMeansModel',
+__all__ = ['BisectingKMeansModel', 'BisectingKMeans', 'KMeansModel',
'KMeans',
+ 'GaussianMixtureModel', 'GaussianMixture',
'PowerIterationClusteringModel',
+ 'PowerIterationClustering', 'StreamingKMeans',
'StreamingKMeansModel',
'LDA', 'LDAModel']
@inherit_doc
+class BisectingKMeansModel(JavaModelWrapper):
+ """
+ .. note:: Experimental
+
+ A clustering model derived from the bisecting k-means method.
+
+ >>> data = array([0.0,0.0, 1.0,1.0, 9.0,8.0, 8.0,9.0]).reshape(4, 2)
+ >>> bskm = BisectingKMeans()
+ >>> model = bskm.run(sc.parallelize(data))
+ >>> model.predict(array([0.0, 0.0])) == model.predict(array([0.0,
0.0]))
+ True
+ >>> model.k
+ 4
+ >>> model.computeCost(array([0.0, 0.0]))
+ 0.0
+ >>> model.k == len(model.clusterCenters)
+ True
+ >>> model = bskm.setK(2).run(sc.parallelize(data))
+ >>> model.predict(array([0.0, 0.0])) == model.predict(array([1.0,
1.0]))
+ True
+ >>> model.k
+ 2
+ """
+
+ @property
+ def clusterCenters(self):
+ """Get the cluster centers, represented as a list of NumPy
arrays."""
+ return [c.toArray() for c in self.call("clusterCenters")]
+
+ @property
+ def k(self):
+ """Get the number of clusters"""
+ return self.call("k")
+
+ def predict(self, x):
+ """
+ Find the cluster to which x belongs in this model.
+
+ :param x: Either the point to determine the cluster for or an RDD
of points to determine
+ the clusters for.
+ """
+ if isinstance(x, RDD):
+ return x.map(self.predict(x))
+
+ x = _convert_to_vector(x)
+ return self.call("predict", x)
+
+ def computeCost(self, point):
+ """
+ Return the Bisecting K-means cost (sum of squared distances of
points to
+ their nearest center) for this model on the given data.
+
+ :param point: the point to compute the cost to
+ """
+ return self.call("computeCost", _convert_to_vector(point))
+
+
+class BisectingKMeans:
+ """
+ A bisecting k-means algorithm based on the paper "A comparison of
document clustering
+ techniques" by Steinbach, Karypis, and Kumar, with modification to fit
Spark.
+ The algorithm starts from a single cluster that contains all points.
+ Iteratively it finds divisible clusters on the bottom level and
bisects each of them using
+ k-means, until there are `k` leaf clusters in total or no leaf
clusters are divisible.
+ The bisecting steps of clusters on the same level are grouped together
to increase parallelism.
+ If bisecting all divisible clusters on the bottom level would result
more than `k` leaf
+ clusters, larger clusters get higher priority.
+
+ Based on
[[http://glaros.dtc.umn.edu/gkhome/fetch/papers/docclusterKDDTMW00.pdf
+ Steinbach, Karypis, and Kumar, A comparison of document clustering
techniques,
+ KDD Workshop on Text Mining, 2000.]]
+ """
+ def __init__(self):
+ self.k = 4
+ self.maxIterations = 20
+ self.minDivisibleClusterSize = 1.0
+ self.seed = -1888008604 # classOf[BisectingKMeans].getName.##
+
+ def setK(self, k):
+ """
+ Set the number of leaf clusters.
+
+ :param k: the desired number of leaf clusters (default: 4). The
actual number could be
+ smaller if there are no divisible leaf clusters.
+ """
+ self.k = k
+ return self
+
+ def getK(self):
+ """Return the desired number of leaf clusters."""
+ return self.k
+
+ def setMaxIterations(self, maxIterations):
+ """
+ Set the maximum number of iterations.
+
+ :param maxIterations: the max number of k-means iterations to
split clusters (default: 20)
+ """
+ self.maxIterations = maxIterations
+ return self
+
+ def getMaxIterations(self):
+ """Return the maximum number of iterations."""
+ return self.maxIterations
+
+ def setMinDivisibleClusterSize(self, minDivisibleClusterSize):
+ """
+ Set the minimum divisible cluster size.
+
+ :param minDivisibleClusterSize: the minimum number of points (if
>= 1.0) or the minimum
+ proportion of points (if < 1.0) of a divisible cluster (default: 1)
+ """
+ self.minDivisibleClusterSize = minDivisibleClusterSize
+ return self
+
+ def getMinDivisibleClusterSize(self):
+ """Return the min divisible cluster size."""
+ return minDivisibleClusterSize
+
+ def setSeed(self, seed):
+ """
+ Set the seed.
+
+ :param seed: a random seed (default: -1888008604)
+ """
+ self.seed = seed
+ return self
+
+ def getSeed(self):
+ """Return the random seed used."""
+ return self.seed
+
+ def run(self, rdd):
--- End diff --
Sounds simpler, I'll switch it over.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]