Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/7166#discussion_r34081654
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixture.scala ---
@@ -171,14 +185,30 @@ class GaussianMixture private (
// Create new distributions based on the partial assignments
// (often referred to as the "M" step in literature)
val sumWeights = sums.weights.sum
- var i = 0
- while (i < k) {
- val mu = sums.means(i) / sums.weights(i)
- BLAS.syr(-sums.weights(i), Vectors.fromBreeze(mu),
- Matrices.fromBreeze(sums.sigmas(i)).asInstanceOf[DenseMatrix])
- weights(i) = sums.weights(i) / sumWeights
- gaussians(i) = new MultivariateGaussian(mu, sums.sigmas(i) /
sums.weights(i))
- i = i + 1
+
+ if (distributeGaussians) {
+ val (ws, gs) = sc.parallelize(0 until k).map { i =>
+ val mu = sums.means(i) / sums.weights(i)
+ BLAS.syr(-sums.weights(i), Vectors.fromBreeze(mu),
+ Matrices.fromBreeze(sums.sigmas(i)).asInstanceOf[DenseMatrix])
+ val weight = sums.weights(i) / sumWeights
+ val gaussian = new MultivariateGaussian(mu, sums.sigmas(i) /
sums.weights(i))
+ (weight, gaussian)
+ }.collect.unzip
+ (0 until k).foreach { i =>
+ weights(i) = ws(i)
+ gaussians(i) = gs(i)
+ }
+ } else {
+ var i = 0
+ while (i < k) {
+ val mu = sums.means(i) / sums.weights(i)
+ BLAS.syr(-sums.weights(i), Vectors.fromBreeze(mu),
+ Matrices.fromBreeze(sums.sigmas(i)).asInstanceOf[DenseMatrix])
+ weights(i) = sums.weights(i) / sumWeights
+ gaussians(i) = new MultivariateGaussian(mu, sums.sigmas(i) /
sums.weights(i))
--- End diff --
This is essentially the same code as the distributed branch. Should we
define a method?
---
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]