-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-13449] Naive Bayes wrapper in SparkR #11486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fb1bca4
787f25f
b66d3e5
a5ab2e6
9215faf
388e85d
26d38e1
a07beb2
afaba4a
1a685e1
30e9c37
390f8e6
dbaf4e6
9991e79
6c97cef
721a8b7
8e21393
90b6ad9
b4ee1aa
87fa0aa
3d291de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import org.apache.hadoop.fs.Path | |
| import org.apache.spark.SparkException | ||
| import org.apache.spark.annotation.{Experimental, Since} | ||
| import org.apache.spark.ml.PredictorParams | ||
| import org.apache.spark.ml.attribute.AttributeGroup | ||
| import org.apache.spark.ml.param.{DoubleParam, Param, ParamMap, ParamValidators} | ||
| import org.apache.spark.ml.util._ | ||
| import org.apache.spark.mllib.classification.{NaiveBayes => OldNaiveBayes} | ||
|
|
@@ -104,7 +105,12 @@ class NaiveBayes @Since("1.5.0") ( | |
| override protected def train(dataset: DataFrame): NaiveBayesModel = { | ||
| val oldDataset: RDD[LabeledPoint] = extractLabeledPoints(dataset) | ||
| val oldModel = OldNaiveBayes.train(oldDataset, $(smoothing), $(modelType)) | ||
| NaiveBayesModel.fromOld(oldModel, this) | ||
| val nbModel = copyValues(NaiveBayesModel.fromOld(oldModel, this)) | ||
| val attr = AttributeGroup.fromStructField(dataset.schema($(featuresCol))).attributes | ||
| if (attr.isDefined) { | ||
| nbModel.setFeatureNames(attr.get.map(_.name.getOrElse("NA"))) | ||
| } | ||
| nbModel | ||
| } | ||
|
|
||
| @Since("1.5.0") | ||
|
|
@@ -227,6 +233,21 @@ class NaiveBayesModel private[ml] ( | |
|
|
||
| @Since("1.6.0") | ||
| override def write: MLWriter = new NaiveBayesModel.NaiveBayesModelWriter(this) | ||
|
|
||
| private var featureNames: Option[Array[String]] = None | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mengxr I remove the previous |
||
|
|
||
| private[classification] def setFeatureNames(names: Array[String]): this.type = { | ||
| this.featureNames = Some(names) | ||
| this | ||
| } | ||
|
|
||
| private[ml] def getFeatureNames: Array[String] = featureNames match { | ||
| case Some(names) => names | ||
| case None => | ||
| throw new SparkException( | ||
| s"No training result available for the ${this.getClass.getSimpleName}", | ||
| new NullPointerException()) | ||
| } | ||
| } | ||
|
|
||
| @Since("1.6.0") | ||
|
|
@@ -237,7 +258,6 @@ object NaiveBayesModel extends MLReadable[NaiveBayesModel] { | |
| oldModel: OldNaiveBayesModel, | ||
| parent: NaiveBayes): NaiveBayesModel = { | ||
| val uid = if (parent != null) parent.uid else Identifiable.randomUID("nb") | ||
| val labels = Vectors.dense(oldModel.labels) | ||
| val pi = Vectors.dense(oldModel.pi) | ||
| val theta = new DenseMatrix(oldModel.labels.length, oldModel.theta(0).length, | ||
| oldModel.theta.flatten, true) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,6 +257,18 @@ class RFormulaModel private[feature]( | |
| "Label column already exists and is not of type DoubleType.") | ||
| } | ||
|
|
||
| /** | ||
| * Get the original array of labels if exists. | ||
| */ | ||
| private[ml] def getOriginalLabels: Option[Array[String]] = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm rewriting it now. |
||
| // According to the sequences of transformers in RFormula, if the last stage is a | ||
| // StringIndexerModel, then we can extract the original labels from it. | ||
| pipelineModel.stages.last match { | ||
| case m: StringIndexerModel => Some(m.labels) | ||
| case _ => None | ||
| } | ||
| } | ||
|
|
||
| @Since("2.0.0") | ||
| override def write: MLWriter = new RFormulaModel.RFormulaModelWriter(this) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you check manually that the http://ugrad.stat.ubc.ca/R/library/e1071/html/naiveBayes.html
e1071 package naiveBayes still work when SparkR is loaded after e1071?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users need
e1071::naiveBayesto call that. BothnaiveBayesare implemented as S3 generic functions and the Spark one shares the same first argument type (formula) with the one in e1071. So I don't think we can avoid shadowing the method. I tried different loading orders and confirmed that both can be used with namespace prefixes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I have a test to ensure that we can use the prefix to call e1071::naiveBayes().