Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class GaussianMixture @Since("2.0.0") (

val model = copyValues(new GaussianMixtureModel(uid, weights, gaussianDists)).setParent(this)
val summary = new GaussianMixtureSummary(model.transform(dataset),
$(predictionCol), $(probabilityCol), $(featuresCol), $(k))
$(predictionCol), $(probabilityCol), $(featuresCol), $(k), logLikelihood)
model.setSummary(Some(summary))
instr.logSuccess(model)
model
Expand Down Expand Up @@ -674,6 +674,7 @@ private class ExpectationAggregator(
* in `predictions`.
* @param featuresCol Name for column of features in `predictions`.
* @param k Number of clusters.
* @param logLikelihood Total log-likelihood for this model on the given data.
*/
@Since("2.0.0")
@Experimental
Expand All @@ -682,7 +683,9 @@ class GaussianMixtureSummary private[clustering] (
predictionCol: String,
@Since("2.0.0") val probabilityCol: String,
featuresCol: String,
k: Int) extends ClusteringSummary(predictions, predictionCol, featuresCol, k) {
k: Int,
@Since("2.2.0") val logLikelihood: Double)
extends ClusteringSummary(predictions, predictionCol, featuresCol, k) {

/**
* Probability of each cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.apache.spark.storage.StorageLevel
import org.apache.spark.util.random.BernoulliCellSampler

/**
* Helper methods to load, save and pre-process data used in ML Lib.
* Helper methods to load, save and pre-process data used in MLLib.
*/
@Since("0.8.0")
object MLUtils extends Logging {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ class GaussianMixtureSuite extends SparkFunSuite with MLlibTestSparkContext
[,1] [,2]
[1,] 0.2961543 0.160783
[2,] 0.1607830 1.008878

model$loglik

[1] -46.89499
*/
val weights = Array(0.5333333, 0.4666667)
val means = Array(Vectors.dense(10.363673, 9.897081), Vectors.dense(0.11731091, -0.06192351))
Expand All @@ -219,6 +223,9 @@ class GaussianMixtureSuite extends SparkFunSuite with MLlibTestSparkContext
val expected = new GaussianMixtureModel("dummy", weights, gaussians)
val actual = new GaussianMixture().setK(2).setSeed(seed).fit(rDataset)
modelEquals(expected, actual)

val llk = actual.summary.logLikelihood
assert(llk ~== -46.89499 absTol 1E-5)
}

test("upper triangular matrix unpacking") {
Expand Down
5 changes: 4 additions & 1 deletion project/MimaExcludes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ object MimaExcludes {
ProblemFilters.exclude[ReversedMissingMethodProblem]("org.apache.spark.streaming.scheduler.StreamingListener.onStreamingStarted"),

// [SPARK-19148][SQL] do not expose the external table concept in Catalog
ProblemFilters.exclude[ReversedMissingMethodProblem]("org.apache.spark.sql.catalog.Catalog.createTable")
ProblemFilters.exclude[ReversedMissingMethodProblem]("org.apache.spark.sql.catalog.Catalog.createTable"),

// [SPARK-14272][ML] Add logLikelihood in GaussianMixtureSummary
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.spark.ml.clustering.GaussianMixtureSummary.this")
)

// Exclude rules for 2.1.x
Expand Down
10 changes: 10 additions & 0 deletions python/pyspark/ml/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ class GaussianMixture(JavaEstimator, HasFeaturesCol, HasPredictionCol, HasMaxIte
3
>>> summary.clusterSizes
[2, 2, 2]
>>> summary.logLikelihood
8.14636...
>>> weights = model.weights
>>> len(weights)
3
Expand Down Expand Up @@ -281,6 +283,14 @@ def probability(self):
"""
return self._call_java("probability")

@property
@since("2.2.0")
def logLikelihood(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this to doc test.

"""
Total log-likelihood for this model on the given data.
"""
return self._call_java("logLikelihood")


class KMeansSummary(ClusteringSummary):
"""
Expand Down