Github user yanboliang commented on a diff in the pull request:
https://github.com/apache/spark/pull/15746#discussion_r86568876
--- Diff:
mllib/src/main/scala/org/apache/spark/ml/r/GBTClassificationWrapper.scala ---
@@ -0,0 +1,144 @@
+/*
+ * 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.r
+
+import org.apache.hadoop.fs.Path
+import org.json4s._
+import org.json4s.JsonDSL._
+import org.json4s.jackson.JsonMethods._
+
+import org.apache.spark.ml.{Pipeline, PipelineModel}
+import org.apache.spark.ml.attribute.AttributeGroup
+import org.apache.spark.ml.classification.{GBTClassificationModel,
GBTClassifier}
+import org.apache.spark.ml.feature.RFormula
+import org.apache.spark.ml.linalg.Vector
+import org.apache.spark.ml.util._
+import org.apache.spark.sql.{DataFrame, Dataset}
+
+private[r] class GBTClassifierWrapper private (
+ val pipeline: PipelineModel,
+ val formula: String,
+ val features: Array[String]) extends MLWritable {
+
+ private val DTModel: GBTClassificationModel =
+ pipeline.stages(1).asInstanceOf[GBTClassificationModel]
+
+ lazy val numFeatures: Int = DTModel.numFeatures
+ lazy val featureImportances: Vector = DTModel.featureImportances
+ lazy val numTrees: Int = DTModel.getNumTrees
+ lazy val treeWeights: Array[Double] = DTModel.treeWeights
+
+ def summary: String = DTModel.toDebugString
+
+ def transform(dataset: Dataset[_]): DataFrame = {
+ pipeline.transform(dataset).drop(DTModel.getFeaturesCol)
+ }
+
+ override def write: MLWriter = new
+ GBTClassifierWrapper.GBTClassifierWrapperWriter(this)
+}
+
+private[r] object GBTClassifierWrapper extends
MLReadable[GBTClassifierWrapper] {
+ def fit( // scalastyle:ignore
+ data: DataFrame,
+ formula: String,
+ maxDepth: Int,
+ maxBins: Int,
+ maxIter: Int,
+ stepSize: Double,
+ minInstancesPerNode: Int,
+ minInfoGain: Double,
+ checkpointInterval: Int,
+ lossType: String,
+ seed: String,
+ subsamplingRate: Double,
+ maxMemoryInMB: Int,
+ cacheNodeIds: Boolean): GBTClassifierWrapper = {
+
+ val rFormula = new RFormula()
+ .setFormula(formula)
+ RWrapperUtils.checkDataColumns(rFormula, data)
+ val rFormulaModel = rFormula.fit(data)
+
+ // get feature names from output schema
+ val schema = rFormulaModel.transform(data).schema
+ val featureAttrs =
AttributeGroup.fromStructField(schema(rFormulaModel.getFeaturesCol))
+ .attributes.get
+ val features = featureAttrs.map(_.name.get)
+
+ // assemble and fit the pipeline
+ val rfc = new GBTClassifier()
+ .setMaxDepth(maxDepth)
+ .setMaxBins(maxBins)
+ .setMaxIter(maxIter)
+ .setStepSize(stepSize)
+ .setMinInstancesPerNode(minInstancesPerNode)
+ .setMinInfoGain(minInfoGain)
+ .setCheckpointInterval(checkpointInterval)
+ .setLossType(lossType)
+ .setSubsamplingRate(subsamplingRate)
+ .setMaxMemoryInMB(maxMemoryInMB)
+ .setCacheNodeIds(cacheNodeIds)
+ .setFeaturesCol(rFormula.getFeaturesCol)
+ if (seed != null && seed.length > 0) rfc.setSeed(seed.toLong)
+
+ val pipeline = new Pipeline()
+ .setStages(Array(rFormulaModel, rfc))
--- End diff --
I think ```spark.gbt``` also need support to make binary classification
based on dataset of string label such as ```Yes``` and ```No```. This
implementation will output double value when make prediction which may confuse
users, and we should convert the double value back to the original string
label. You can refer ```NaiveBayesWrapper``` to construct the pipeline. BTW,
add R test for dataset of string label.
---
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]