@@ -208,27 +208,30 @@ class GaussianMixtureModelEM private (
208
208
cov
209
209
}
210
210
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 ]] = {
217
217
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
+ }
221
223
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
+ }
224
227
}
228
+
225
229
/**
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
232
235
val pSum = p.sum
233
236
for (i<- 0 until k){
234
237
p(i) /= pSum
0 commit comments