Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix test
  • Loading branch information
zhengruifeng committed Jan 16, 2017
commit d5bb62b3019f9b61ba715158b73f4eb0a19a2f5f
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,14 @@ class GaussianMixtureModel private[ml] (
@Since("2.2.0")
def computeLogLikelihood(dataset: Dataset[_]): Double = {
SchemaUtils.checkColumnType(dataset.schema, $(featuresCol), new VectorUDT)

val spark = dataset.sparkSession
import spark.implicits._

val bcWeightAndDists =
spark.sparkContext.broadcast(weights.zip(gaussians))

val bcWeightAndDists = spark.sparkContext.broadcast(weights.zip(gaussians))
dataset.select(col($(featuresCol))).map {
case Row(point: Vector) =>
case Row(feature: Vector) =>
val likelihood = bcWeightAndDists.value.map {
case (weight, dist) =>
EPSILON + weight * dist.pdf(point)
case (weight, dist) => EPSILON + weight * dist.pdf(feature)
}.sum
math.log(likelihood)
}.reduce(_ + _)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,6 @@ class GaussianMixtureSuite extends SparkFunSuite with MLlibTestSparkContext

model.setSummary(None)
assert(!model.hasSummary)

// Check validity of LogLikelihood
transformed.select(probabilityColName).rdd.map(_.getAs[Vector](0))
val llk = transformed.select(probabilityColName).rdd.map(_.getAs[Vector](0))
.map { probs =>
val likelihood = probs.toArray.zip(model.weights).map {
case (a, b) => a * b }.sum
math.log(likelihood)
}.sum()
assert(model.computeLogLikelihood(dataset) ~== llk relTol 1E-8)
}

test("check LogLikelihood") {
Expand Down