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

    https://github.com/apache/spark/pull/7284#discussion_r34867547
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/classification/NaiveBayes.scala ---
    @@ -0,0 +1,196 @@
    +/*
    + * 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.classification
    +
    +import org.apache.spark.SparkException
    +import org.apache.spark.ml.{PredictorParams, PredictionModel, Predictor}
    +import org.apache.spark.ml.param.{ParamMap, ParamValidators, Param, 
DoubleParam}
    +import org.apache.spark.ml.util.Identifiable
    +import org.apache.spark.mllib.classification.{NaiveBayes => OldNaiveBayes}
    +import org.apache.spark.mllib.classification.{NaiveBayesModel => 
OldNaiveBayesModel}
    +import org.apache.spark.mllib.linalg._
    +import org.apache.spark.mllib.regression.LabeledPoint
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.sql.DataFrame
    +
    +/**
    + * Params for Naive Bayes Classifiers.
    + */
    +private[ml] trait NaiveBayesParams extends PredictorParams {
    +
    +  /**
    +   * The smoothing parameter.
    +   * (default = 1.0).
    +   * @group param
    +   */
    +  final val lambda: DoubleParam = new DoubleParam(this, "lambda", "The 
smoothing parameter.",
    +    ParamValidators.gtEq(0))
    +
    +  /** @group getParam */
    +  final def getLambda: Double = $(lambda)
    +
    +  /**
    +   * The model type which is a string (case-sensitive).
    +   * Supported options: "multinomial" and "bernoulli".
    +   * (default = multinomial)
    +   * @group param
    +   */
    +  final val modelType: Param[String] = new Param[String](this, 
"modelType", "The model type " +
    +    "which is a string (case-sensitive). Supported options: multinomial 
(default) and bernoulli.",
    +    
ParamValidators.inArray[String](OldNaiveBayes.supportedModelTypes.toArray))
    +
    +  /** @group getParam */
    +  final def getModelType: String = $(modelType)
    +}
    +
    +/**
    + * Naive Bayes Classifiers.
    + * It supports both Multinomial NB
    + * 
([[http://nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html]])
    + * which can handle finitely supported discrete data. For example, by 
converting documents into
    + * TF-IDF vectors, it can be used for document classification. By making 
every vector a
    + * binary (0/1) data, it can also be used as Bernoulli NB
    + * 
([[http://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html]]).
    + * The input feature values must be nonnegative.
    + */
    +class NaiveBayes(override val uid: String)
    +  extends Predictor[Vector, NaiveBayes, NaiveBayesModel]
    +  with NaiveBayesParams {
    +
    +  def this() = this(Identifiable.randomUID("nb"))
    +
    +  /**
    +   * Set the smoothing parameter.
    +   * Default is 1.0.
    +   * @group setParam
    +   */
    +  def setLambda(value: Double): this.type = set(lambda, value)
    +  setDefault(lambda -> 1.0)
    +
    +  /**
    +   * Set the model type using a string (case-sensitive).
    +   * Supported options: "multinomial" and "bernoulli".
    +   * Default is "multinomial"
    +   */
    +  def setModelType(value: String): this.type = set(modelType, value)
    +  setDefault(modelType -> OldNaiveBayes.Multinomial)
    +
    +  override protected def train(dataset: DataFrame): NaiveBayesModel = {
    +    val oldDataset: RDD[LabeledPoint] = extractLabeledPoints(dataset)
    +    val instance = oldDataset.map{
    +      case LabeledPoint(label: Double, features: Vector) => label }
    +      .treeAggregate(new MultiClassSummarizer)(
    +        seqOp = (c, v) => (c, v) match {
    +          case (classSummarizer: MultiClassSummarizer, label: Double) => 
classSummarizer.add(label)
    +        },
    +        combOp = (c1, c2) => (c1, c2) match {
    +          case (classSummarizer1: MultiClassSummarizer, classSummarizer2: 
MultiClassSummarizer) =>
    +            classSummarizer1.merge(classSummarizer2)
    +        })
    +    val numInvalid = instance.countInvalid
    +    val numClasses = instance.numClasses
    +    if (numInvalid != 0) {
    +      val msg = s"Classification labels should be in {0 to ${numClasses - 
1} " +
    +        s"Found $numInvalid invalid labels."
    +      logError(msg)
    +      throw new SparkException(msg)
    +    }
    --- End diff --
    
    I like this kind of check, but it should ideally happen within the 
extractLabeledPoints map and avoid an extra RDD action.  Also, it will need to 
check the label column metadata to see if numClasses is already specified.  
Let's remove this verification for now and leave it as a to-do for a follow-up 
PR.  Can you please make a JIRA for it once this PR gets merged?


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to