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

    https://github.com/apache/spark/pull/11419#discussion_r57982986
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/clustering/GaussianMixture.scala ---
    @@ -0,0 +1,291 @@
    +/*
    + * 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.ml.clustering
    +
    +import org.apache.hadoop.fs.Path
    +
    +import org.apache.spark.SparkException
    +import org.apache.spark.annotation.{Experimental, Since}
    +import org.apache.spark.ml.{Estimator, Model}
    +import org.apache.spark.ml.param.{IntParam, ParamMap, Params}
    +import org.apache.spark.ml.param.shared._
    +import org.apache.spark.ml.util._
    +import org.apache.spark.mllib.clustering.{GaussianMixture => MLlibGM, 
GaussianMixtureModel => MLlibGMModel}
    +import org.apache.spark.mllib.linalg._
    +import org.apache.spark.mllib.stat.distribution.MultivariateGaussian
    +import org.apache.spark.sql.{DataFrame, Row}
    +import org.apache.spark.sql.functions.{col, udf}
    +import org.apache.spark.sql.types.{IntegerType, StructType}
    +
    +
    +/**
    + * Common params for GaussianMixture and GaussianMixtureModel
    + */
    +private[clustering] trait GaussianMixtureParams extends Params with 
HasMaxIter with HasFeaturesCol
    +  with HasSeed with HasPredictionCol with HasProbabilityCol with HasTol {
    +
    +  /**
    +   * Set the number of clusters to create (k). Must be > 1. Default: 2.
    +   * @group param
    +   */
    +  @Since("2.0.0")
    +  final val k = new IntParam(this, "k", "number of clusters to create", 
(x: Int) => x > 1)
    +
    +  /** @group getParam */
    +  @Since("2.0.0")
    +  def getK: Int = $(k)
    +
    +  /**
    +   * Validates and transforms the input schema.
    +   * @param schema input schema
    +   * @return output schema
    +   */
    +  protected def validateAndTransformSchema(schema: StructType): StructType 
= {
    +    validateParams()
    +    SchemaUtils.checkColumnType(schema, $(featuresCol), new VectorUDT)
    +    SchemaUtils.appendColumn(schema, $(predictionCol), IntegerType)
    +    SchemaUtils.appendColumn(schema, $(probabilityCol), new VectorUDT)
    +  }
    +}
    +
    +/**
    + * :: Experimental ::
    + * Model fitted by GaussianMixture.
    + * @param parentModel a model trained by 
spark.mllib.clustering.GaussianMixture.
    + */
    +@Since("2.0.0")
    +@Experimental
    +class GaussianMixtureModel private[ml] (
    +    @Since("2.0.0") override val uid: String,
    +    private val parentModel: MLlibGMModel)
    +  extends Model[GaussianMixtureModel] with GaussianMixtureParams with 
MLWritable {
    +
    +  @Since("2.0.0")
    +  override def copy(extra: ParamMap): GaussianMixtureModel = {
    +    val copied = new GaussianMixtureModel(uid, parentModel)
    +    copyValues(copied, extra)
    +  }
    +
    +  @Since("2.0.0")
    +  override def transform(dataset: DataFrame): DataFrame = {
    +    val predUDF = udf((vector: Vector) => predict(vector))
    +    val probUDF = udf((vector: Vector) => predictProbability(vector))
    +    dataset.withColumn($(predictionCol), predUDF(col($(featuresCol))))
    +      .withColumn($(probabilityCol), probUDF(col($(featuresCol))))
    +  }
    +
    +  @Since("2.0.0")
    +  override def transformSchema(schema: StructType): StructType = {
    +    validateAndTransformSchema(schema)
    +  }
    +
    +  private[clustering] def predict(features: Vector): Int = 
parentModel.predict(features)
    +
    +  private[clustering] def predictProbability(features: Vector): Vector = {
    +    Vectors.dense(parentModel.predictSoft(features))
    +  }
    +
    +  @Since("2.0.0")
    +  def weights: Array[Double] = parentModel.weights
    +
    +  @Since("2.0.0")
    +  def gaussians: Array[MultivariateGaussian] = parentModel.gaussians
    +
    +  @Since("2.0.0")
    +  override def write: MLWriter = new 
GaussianMixtureModel.GaussianMixtureModelWriter(this)
    +
    +  private var trainingSummary: Option[GaussianMixtureSummary] = None
    +
    +  private[clustering] def setSummary(summary: GaussianMixtureSummary): 
this.type = {
    +    this.trainingSummary = Some(summary)
    +    this
    +  }
    +
    +  /**
    +   * Gets summary of model on training set. An exception is
    +   * thrown if `trainingSummary == None`.
    +   */
    +  @Since("2.0.0")
    +  def summary: GaussianMixtureSummary = trainingSummary match {
    --- End diff --
    
    Could you also add a hasSummary method?
    ```
    def hasSummary: Boolean = trainingSummary.nonEmpty
    ```


---
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