Skip to content

Commit d695034

Browse files
committed
Fixed style issues
1 parent c3b8ce0 commit d695034

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ object DenseGmmEM {
4747
println("weight=%f mu=%s sigma=\n%s\n" format
4848
(clusters.weight(i), clusters.mu(i), clusters.sigma(i)))
4949
}
50-
val (responsibility_matrix, cluster_labels) = clusters.predict(data)
51-
for(x <- cluster_labels.collect()){
52-
print(" " + x)
50+
51+
val (responsibilityMatrix, clusterLabels) = clusters.predict(data)
52+
for (x <- clusterLabels.collect) {
53+
print(" " + x)
5354
}
5455
}
5556
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class GaussianMixtureModel(
4242

4343
/** Maps given points to their cluster indices. */
4444
def predict(points: RDD[Vector]): (RDD[Array[Double]],RDD[Int]) = {
45-
val responsibility_matrix = new GaussianMixtureModelEM()
46-
.predictClusters(points,mu,sigma,weight,k)
47-
val cluster_labels = responsibility_matrix.map(r => r.indexOf(r.max))
48-
(responsibility_matrix,cluster_labels)
49-
}
45+
val responsibilityMatrix = new GaussianMixtureModelEM()
46+
.predictClusters(points,mu,sigma,weight,k)
47+
val clusterLabels = responsibilityMatrix.map(r => r.indexOf(r.max))
48+
(responsibilityMatrix, clusterLabels)
49+
}
5050
}

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,27 +208,30 @@ class GaussianMixtureModelEM private (
208208
cov
209209
}
210210

211-
/**
212-
Given the input vectors, return the membership value of each vector
213-
to all mixture components.
214-
*/
215-
def predictClusters(points:RDD[Vector],mu:Array[Vector],sigma:Array[Matrix],
216-
weight:Array[Double],k:Int):RDD[Array[Double]]= {
211+
/**
212+
* Given the input vectors, return the membership value of each vector
213+
* to all mixture components.
214+
*/
215+
def predictClusters(points:RDD[Vector], mu:Array[Vector], sigma:Array[Matrix],
216+
weight:Array[Double],k:Int): RDD[Array[Double]] = {
217217
val ctx = points.sparkContext
218-
val dists = ctx.broadcast((0 until k).map(i =>
219-
new MultivariateGaussian(mu(i).toBreeze.toDenseVector,sigma(i).toBreeze.toDenseMatrix))
220-
.toArray)
218+
val dists = ctx.broadcast{
219+
(0 until k).map{ i =>
220+
new MultivariateGaussian(mu(i).toBreeze.toDenseVector, sigma(i).toBreeze.toDenseMatrix)
221+
}.toArray
222+
}
221223
val weights = ctx.broadcast((0 until k).map(i => weight(i)).toArray)
222-
points.map(x=>compute_log_likelihood(x.toBreeze.toDenseVector,dists.value,weights.value,k))
223-
224+
points.map{ x =>
225+
computeSoftAssignments(x.toBreeze.toDenseVector, dists.value, weights.value, k)
226+
}
224227
}
228+
225229
/**
226-
* Compute the log density of each vector
227-
*/
228-
def compute_log_likelihood(pt:DenseDoubleVector,dists:Array[MultivariateGaussian],
229-
weights:Array[Double],k:Int):Array[Double]={
230-
val p = (0 until k).map(i =>
231-
eps + weights(i) * dists(i).pdf(pt)).toArray
230+
* Compute the partial assignments for each vector
231+
*/
232+
def computeSoftAssignments(pt: DenseDoubleVector, dists: Array[MultivariateGaussian],
233+
weights: Array[Double], k: Int): Array[Double] = {
234+
val p = (0 until k).map(i => eps + weights(i) * dists(i).pdf(pt)).toArray
232235
val pSum = p.sum
233236
for(i<- 0 until k){
234237
p(i) /= pSum

0 commit comments

Comments
 (0)