Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/3022#discussion_r22092923
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixtureModel.scala
 ---
    @@ -0,0 +1,94 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.mllib.clustering
    +
    +import breeze.linalg.{DenseVector => BreezeVector}
    +
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.mllib.linalg.Matrix
    +import org.apache.spark.mllib.linalg.Vector
    +import org.apache.spark.mllib.stat.impl.MultivariateGaussian
    +
    +/**
    + * Multivariate Gaussian Mixture Model (GMM) consisting of k Gaussians, 
where points 
    + * are drawn from each Gaussian i=1..k with probability w(i); mu(i) and 
sigma(i) are 
    + * the respective mean and covariance for each Gaussian distribution 
i=1..k. 
    + * 
    + * @param weight Weights for each Gaussian distribution in the mixture, 
where mu(i) is
    + *               the weight for Gaussian i, and weight.sum == 1
    + * @param mu Means for each Gaussian in the mixture, where mu(i) is the 
mean for Gaussian i
    + * @param sigma Covariance maxtrix for each Gaussian in the mixture, where 
sigma(i) is the
    + *              covariance matrix for Gaussian i
    + */
    +class GaussianMixtureModel(
    +  val weight: Array[Double], 
    +  val mu: Array[Vector], 
    +  val sigma: Array[Matrix]) extends Serializable {
    +  
    +  /** Number of gaussians in mixture */
    +  def k: Int = weight.length
    +
    +  /** Maps given points to their cluster indices. */
    +  def predict(points: RDD[Vector]): (RDD[Array[Double]],RDD[Int]) = {
    +    val responsibilityMatrix = predictMembership(points,mu,sigma,weight,k)
    +    val clusterLabels = responsibilityMatrix.map(r => r.indexOf(r.max))
    +    (responsibilityMatrix, clusterLabels)
    +  }
    +  
    +  /**
    +   * Given the input vectors, return the membership value of each vector
    +   * to all mixture components. 
    +   */
    +  def predictMembership(
    +      points: RDD[Vector], 
    +      mu: Array[Vector], 
    +      sigma: Array[Matrix],
    +      weight: Array[Double], k: Int): RDD[Array[Double]] = {
    +    val sc = points.sparkContext
    +    val dists = sc.broadcast{
    +      (0 until k).map{ i => 
    +        new MultivariateGaussian(mu(i).toBreeze.toDenseVector, 
sigma(i).toBreeze.toDenseMatrix)
    +      }.toArray
    +    }
    +    val weights = sc.broadcast(weight)
    +    points.map{ x => 
    +      computeSoftAssignments(x.toBreeze.toDenseVector, dists.value, 
weights.value, k)
    +    }
    +  }
    +  
    +  // We use "eps" as the minimum likelihood density for any given point
    +  // in every cluster; this prevents any divide by zero conditions for
    +  // outlier points.
    +  private val eps = math.pow(2.0, -52)
    --- End diff --
    
    EPS is defined in `MLUtils.EPSILON`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to