# 混合高斯模型(GMM)Spark MLlib调用实例(Scala/Java/Python)

混合高斯模型描述数据点以一定的概率服从k种高斯子分布的一种混合分布。Spark.ml使用EM算法给出一组样本的极大似然模型。

featuresCol:

k:

maxIter:

predictionCol:

probabilityCol:

seed:

tol:

Scala:

import org.apache.spark.ml.clustering.GaussianMixture

// 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=%f\nmu=%s\nsigma=\n%s\n" format
(model.weights(i), model.gaussians(i).mean, model.gaussians(i).cov))
}
Java:

import org.apache.spark.ml.clustering.GaussianMixture;
import org.apache.spark.ml.clustering.GaussianMixtureModel;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;

// Trains a GaussianMixture model
GaussianMixture gmm = new GaussianMixture()
.setK(2);
GaussianMixtureModel model = gmm.fit(dataset);

// Output the parameters of the mixture model
for (int i = 0; i < model.getK(); i++) {
System.out.printf("weight=%f\nmu=%s\nsigma=\n%s\n",
model.weights()[i], model.gaussians()[i].mean(), model.gaussians()[i].cov());
}
Python：

from pyspark.ml.clustering import GaussianMixture

model.gaussiansDF.show()