Skip to content

Commit

Permalink
Fixed style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tgaloppo committed Dec 16, 2014
1 parent c3b8ce0 commit d695034
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ object DenseGmmEM {
println("weight=%f mu=%s sigma=\n%s\n" format
(clusters.weight(i), clusters.mu(i), clusters.sigma(i)))
}
val (responsibility_matrix, cluster_labels) = clusters.predict(data)
for(x <- cluster_labels.collect()){
print(" " + x)

val (responsibilityMatrix, clusterLabels) = clusters.predict(data)
for (x <- clusterLabels.collect) {
print(" " + x)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class GaussianMixtureModel(

/** Maps given points to their cluster indices. */
def predict(points: RDD[Vector]): (RDD[Array[Double]],RDD[Int]) = {
val responsibility_matrix = new GaussianMixtureModelEM()
.predictClusters(points,mu,sigma,weight,k)
val cluster_labels = responsibility_matrix.map(r => r.indexOf(r.max))
(responsibility_matrix,cluster_labels)
}
val responsibilityMatrix = new GaussianMixtureModelEM()
.predictClusters(points,mu,sigma,weight,k)
val clusterLabels = responsibilityMatrix.map(r => r.indexOf(r.max))
(responsibilityMatrix, clusterLabels)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,27 +208,30 @@ class GaussianMixtureModelEM private (
cov
}

/**
Given the input vectors, return the membership value of each vector
to all mixture components.
*/
def predictClusters(points:RDD[Vector],mu:Array[Vector],sigma:Array[Matrix],
weight:Array[Double],k:Int):RDD[Array[Double]]= {
/**
* Given the input vectors, return the membership value of each vector
* to all mixture components.
*/
def predictClusters(points:RDD[Vector], mu:Array[Vector], sigma:Array[Matrix],
weight:Array[Double],k:Int): RDD[Array[Double]] = {
val ctx = points.sparkContext
val dists = ctx.broadcast((0 until k).map(i =>
new MultivariateGaussian(mu(i).toBreeze.toDenseVector,sigma(i).toBreeze.toDenseMatrix))
.toArray)
val dists = ctx.broadcast{
(0 until k).map{ i =>
new MultivariateGaussian(mu(i).toBreeze.toDenseVector, sigma(i).toBreeze.toDenseMatrix)
}.toArray
}
val weights = ctx.broadcast((0 until k).map(i => weight(i)).toArray)
points.map(x=>compute_log_likelihood(x.toBreeze.toDenseVector,dists.value,weights.value,k))

points.map{ x =>
computeSoftAssignments(x.toBreeze.toDenseVector, dists.value, weights.value, k)
}
}

/**
* Compute the log density of each vector
*/
def compute_log_likelihood(pt:DenseDoubleVector,dists:Array[MultivariateGaussian],
weights:Array[Double],k:Int):Array[Double]={
val p = (0 until k).map(i =>
eps + weights(i) * dists(i).pdf(pt)).toArray
* Compute the partial assignments for each vector
*/
def computeSoftAssignments(pt: DenseDoubleVector, dists: Array[MultivariateGaussian],
weights: Array[Double], k: Int): Array[Double] = {
val p = (0 until k).map(i => eps + weights(i) * dists(i).pdf(pt)).toArray
val pSum = p.sum
for(i<- 0 until k){
p(i) /= pSum
Expand Down

0 comments on commit d695034

Please sign in to comment.