Skip to content

Commit 97044cf

Browse files
committed
Fixed style issues
1 parent dc9c742 commit 97044cf

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

examples/src/main/scala/org/apache/spark/examples/mllib/DenseGmmEM.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ object DenseGmmEM {
3535
val ctx = new SparkContext(conf)
3636

3737
val data = ctx.textFile(inputFile).map{ line =>
38-
Vectors.dense(line.trim.split(' ').map(_.toDouble))
39-
}.cache()
38+
Vectors.dense(line.trim.split(' ').map(_.toDouble))
39+
}.cache
4040

4141
val clusters = new GaussianMixtureModelEM()
42-
.setK(k)
43-
.setConvergenceTol(convergenceTol)
44-
.run(data)
42+
.setK(k)
43+
.setConvergenceTol(convergenceTol)
44+
.run(data)
4545

4646
for (i <- 0 until clusters.k) {
4747
println("weight=%f mu=%s sigma=\n%s\n" format
48-
(clusters.weight(i), clusters.mu(i), clusters.sigma(i)))
48+
(clusters.weight(i), clusters.mu(i), clusters.sigma(i)))
4949
}
5050
}
5151
}

mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixtureModelEM.scala

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ class GaussianMixtureModelEM private (
111111
// we start with uniform weights, a random mean from the data, and
112112
// diagonal covariance matrices using component variances
113113
// derived from the samples
114-
var gaussians = (0 until k).map{ i => (1.0 / k,
115-
vectorMean(samples.slice(i * nSamples, (i + 1) * nSamples)),
116-
initCovariance(samples.slice(i * nSamples, (i + 1) * nSamples)))
117-
}.toArray
114+
var gaussians = (0 until k).map{ i =>
115+
(1.0 / k,
116+
vectorMean(samples.slice(i * nSamples, (i + 1) * nSamples)),
117+
initCovariance(samples.slice(i * nSamples, (i + 1) * nSamples)))
118+
}.toArray
118119

119120
val accW = new Array[Accumulator[Double]](k)
120121
val accMu = new Array[Accumulator[DenseDoubleVector]](k)
@@ -129,25 +130,23 @@ class GaussianMixtureModelEM private (
129130
for (i <- 0 until k) {
130131
accW(i) = ctx.accumulator(0.0)
131132
accMu(i) = ctx.accumulator(
132-
BreezeVector.zeros[Double](d))(DenseDoubleVectorAccumulatorParam)
133+
BreezeVector.zeros[Double](d))(DenseDoubleVectorAccumulatorParam)
133134
accSigma(i) = ctx.accumulator(
134-
BreezeMatrix.zeros[Double](d,d))(DenseDoubleMatrixAccumulatorParam)
135+
BreezeMatrix.zeros[Double](d,d))(DenseDoubleMatrixAccumulatorParam)
135136
}
136137

137138
val logLikelihood = ctx.accumulator(0.0)
138139

139140
// broadcast the current weights and distributions to all nodes
140-
val dists = ctx.broadcast((0 until k).map{ i =>
141-
new MultivariateGaussian(gaussians(i)._2, gaussians(i)._3)
142-
}.toArray)
141+
val dists = ctx.broadcast{
142+
(0 until k).map(i => new MultivariateGaussian(gaussians(i)._2, gaussians(i)._3)).toArray
143+
}
143144
val weights = ctx.broadcast((0 until k).map(i => gaussians(i)._1).toArray)
144145

145146
// calculate partial assignments for each sample in the data
146147
// (often referred to as the "E" step in literature)
147-
breezeData.foreach(x => {
148-
val p = (0 until k).map{ i =>
149-
eps + weights.value(i) * dists.value(i).pdf(x)
150-
}.toArray
148+
breezeData.foreach{ x =>
149+
val p = (0 until k).map(i => eps + weights.value(i) * dists.value(i).pdf(x)).toArray
151150

152151
val pSum = p.sum
153152

@@ -161,7 +160,7 @@ class GaussianMixtureModelEM private (
161160
accMu(i) += x * p(i)
162161
accSigma(i) += xxt * p(i)
163162
}
164-
})
163+
}
165164

166165
// Collect the computed sums
167166
val W = (0 until k).map(i => accW(i).value).toArray
@@ -170,15 +169,14 @@ class GaussianMixtureModelEM private (
170169

171170
// Create new distributions based on the partial assignments
172171
// (often referred to as the "M" step in literature)
173-
gaussians = (0 until k).map{ i => {
174-
val weight = W(i) / W.sum
175-
val mu = MU(i) / W(i)
176-
val sigma = SIGMA(i) / W(i) - mu * new Transpose(mu)
177-
(weight, mu, sigma)
178-
}
179-
}.toArray
172+
gaussians = (0 until k).map{ i =>
173+
val weight = W(i) / W.sum
174+
val mu = MU(i) / W(i)
175+
val sigma = SIGMA(i) / W(i) - mu * new Transpose(mu)
176+
(weight, mu, sigma)
177+
}.toArray
180178

181-
llhp = llh; // current becomes previous
179+
llhp = llh // current becomes previous
182180
llh = logLikelihood.value // this is the freshly computed log-likelihood
183181
iter += 1
184182
} while(iter < maxIterations && Math.abs(llh-llhp) > convergenceTol)

mllib/src/test/scala/org/apache/spark/mllib/clustering/GMMExpectationMaximizationSuite.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import org.apache.spark.mllib.util.TestingUtils._
2626
class GMMExpectationMaximizationSuite extends FunSuite with MLlibTestSparkContext {
2727
test("single cluster") {
2828
val data = sc.parallelize(Array(
29-
Vectors.dense(6.0, 9.0),
30-
Vectors.dense(5.0, 10.0),
31-
Vectors.dense(4.0, 11.0)
32-
))
29+
Vectors.dense(6.0, 9.0),
30+
Vectors.dense(5.0, 10.0),
31+
Vectors.dense(4.0, 11.0)
32+
))
3333

3434
// expectations
3535
val Ew = 1.0

0 commit comments

Comments
 (0)