A Gaussian Mixture Model

A Gaussian Mixture Model represents a composite distribution whereby points are drawn from one of k Gaussian sub-distributions, each with its own probability. The spark.ml implementation uses the expectation-maximization algorithm to induce the maximum-likelihood model given a set of samples.

GaussianMixture is implemented as an Estimator and generates a GaussianMixtureModel as the base model.

A Gaussian Mixture Model represents a composite distribution whereby points are drawn from one of k Gaussian sub-distributions, each with its own probability. The spark.ml implementation uses the expectation-maximization algorithm to induce the maximum-likelihood model given a set of samples.

import org.apache.spark.ml.clustering.GaussianMixture // Loads data val dataset = spark.read.format("libsvm").load("/opt/spark/data/mllib/sample_kmeans_data.txt") // Trains Gaussian Mixture Model val gmm = new GaussianMixture() .setK(2) val model = gmm.fit(dataset) // output parameters of mixture model model for (i <- 0 until model.getK) { println("weight=%fnmu=%snsigma=n%sn" format (model.weights(i), model.gaussians(i).mean, model.gaussians(i).cov)) }

import org.apache.spark.ml.clustering.GaussianMixture
// Loads data
val dataset = spark.read.format("libsvm").load("file:///opt/spark/data/mllib/sample_kmeans_data.txt")
// Trains Gaussian Mixture Model
val gmm = new GaussianMixture()
.setK(2)
val model = gmm.fit(dataset)
// output parameters of mixture model model
for (i <- 0 until model.getK) {
println("weight=%fnmu=%snsigma=n%sn" format
(model.weights(i), model.gaussians(i).mean, model.gaussians(i).cov))
}

/*
Output:
weight=0.500000nmu=[0.10000000000001552,0.10000000000001552,0.10000000000001552]nsigma=n0.006666666666806454  0.006666666666806454  0.006666666666806454  
0.006666666666806454  0.006666666666806454  0.006666666666806454  
0.006666666666806454  0.006666666666806454  0.006666666666806454  n
weight=0.500000nmu=[9.099999999999984,9.099999999999984,9.099999999999984]nsigma=n0.006666666666812185  0.006666666666812185  0.006666666666812185  
0.006666666666812185  0.006666666666812185  0.006666666666812185  
0.006666666666812185  0.006666666666812185  0.006666666666812185  n

*/

Last updated