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

    https://github.com/apache/spark/pull/5530#discussion_r28476293
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/classification/DecisionTreeClassifier.scala
 ---
    @@ -0,0 +1,155 @@
    +/*
    + * 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.annotation.AlphaComponent
    +import org.apache.spark.ml.impl.estimator.{Predictor, PredictionModel}
    +import org.apache.spark.ml.impl.tree._
    +import org.apache.spark.ml.param.{Params, ParamMap}
    +import org.apache.spark.ml.util.MetadataUtils
    +import org.apache.spark.mllib.linalg.Vector
    +import org.apache.spark.mllib.regression.LabeledPoint
    +import org.apache.spark.mllib.tree.{DecisionTree => OldDecisionTree}
    +import org.apache.spark.mllib.tree.configuration.{Algo => OldAlgo, 
Strategy => OldStrategy}
    +import org.apache.spark.mllib.tree.model.{DecisionTreeModel => 
OldDecisionTreeModel}
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.sql.DataFrame
    +
    +
    +/**
    + * :: AlphaComponent ::
    + *
    + * [[http://en.wikipedia.org/wiki/Decision_tree_learning Decision tree]] 
learning algorithm
    + * for classification.
    + * It supports both binary and multiclass labels, as well as both 
continuous and categorical
    + * features.
    + */
    +@AlphaComponent
    +class DecisionTreeClassifier
    +  extends Predictor[Vector, DecisionTreeClassifier, 
DecisionTreeClassificationModel]
    +  with DecisionTreeParams[DecisionTreeClassifier]
    +  with TreeClassifierParams[DecisionTreeClassifier] {
    +
    +  // Override parameter setters from parent trait for Java API 
compatibility.
    +
    +  override def setMaxDepth(maxDepth: Int): DecisionTreeClassifier = 
super.setMaxDepth(maxDepth)
    +
    +  override def setMaxBins(maxBins: Int): DecisionTreeClassifier = 
super.setMaxBins(maxBins)
    +
    +  override def setMinInstancesPerNode(minInstancesPerNode: Int): 
DecisionTreeClassifier =
    +    super.setMinInstancesPerNode(minInstancesPerNode)
    +
    +  override def setMinInfoGain(minInfoGain: Double): DecisionTreeClassifier 
=
    +    super.setMinInfoGain(minInfoGain)
    +
    +  override def setMaxMemoryInMB(maxMemoryInMB: Int): 
DecisionTreeClassifier =
    +    super.setMaxMemoryInMB(maxMemoryInMB)
    +
    +  override def setCacheNodeIds(cacheNodeIds: Boolean): 
DecisionTreeClassifier =
    +    super.setCacheNodeIds(cacheNodeIds)
    +
    +  override def setCheckpointInterval(checkpointInterval: Int): 
DecisionTreeClassifier =
    +    super.setCheckpointInterval(checkpointInterval)
    +
    +  override def setImpurity(impurity: String): DecisionTreeClassifier = 
super.setImpurity(impurity)
    +
    +  override protected def train(
    +      dataset: DataFrame,
    +      paramMap: ParamMap): DecisionTreeClassificationModel = {
    +    val categoricalFeatures: Map[Int, Int] =
    +      
MetadataUtils.getCategoricalFeatures(dataset.schema(paramMap(featuresCol)))
    +    val numClasses: Int = 
MetadataUtils.getNumClasses(dataset.schema(paramMap(labelCol))) match {
    +      case Some(n: Int) => n
    +      case None => throw new 
IllegalArgumentException("DecisionTreeClassifier was given input" +
    +        s" with invalid label column, without the number of classes 
specified.")
    +        // TODO: Automatically index labels.
    +    }
    +    val oldDataset: RDD[LabeledPoint] = extractLabeledPoints(dataset, 
paramMap)
    +    val strategy = getOldStrategy(categoricalFeatures, numClasses)
    +    val oldModel = OldDecisionTree.train(oldDataset, strategy)
    +    DecisionTreeClassificationModel.fromOld(oldModel, this, paramMap)
    +  }
    +
    +  /** Create a Strategy instance to use with the old API. */
    +  override private[ml] def getOldStrategy(
    +      categoricalFeatures: Map[Int, Int],
    +      numClasses: Int): OldStrategy = {
    +    val strategy = super.getOldStrategy(categoricalFeatures, numClasses)
    +    strategy.algo = OldAlgo.Classification
    +    strategy.setImpurity(getOldImpurity)
    +    strategy
    +  }
    +}
    +
    +object DecisionTreeClassifier {
    +  /** Accessor for supported impurities */
    +  final val supportedImpurities: Array[String] = 
TreeClassifierParams.supportedImpurities
    +}
    +
    +/**
    + * :: AlphaComponent ::
    + *
    + * [[http://en.wikipedia.org/wiki/Decision_tree_learning Decision tree]] 
model for classification.
    + * It supports both binary and multiclass labels, as well as both 
continuous and categorical
    + * features.
    + */
    +@AlphaComponent
    +class DecisionTreeClassificationModel private[ml] (
    +    override val parent: DecisionTreeClassifier,
    +    override val fittingParamMap: ParamMap,
    +    override val rootNode: Node)
    +  extends PredictionModel[Vector, DecisionTreeClassificationModel]
    +  with DecisionTreeModel with Serializable {
    +
    +  require(rootNode != null,
    +    "DecisionTreeClassificationModel given null rootNode, but it requires 
a non-null rootNode.")
    +
    +  override protected def predict(features: Vector): Double = {
    --- End diff --
    
    Let's mark `DecisionTreeClassificationModel` final. Otherwise, if we make 
`predict` protected now, changing it to public will be a break change in the 
future.


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