Github user mengxr commented on a diff in the pull request: https://github.com/apache/spark/pull/79#discussion_r10360401 --- Diff: mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala --- @@ -0,0 +1,915 @@ +/* + * 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.mllib.tree + +import org.apache.spark.SparkContext._ +import org.apache.spark.rdd.RDD +import org.apache.spark.mllib.tree.model._ +import org.apache.spark.{SparkContext, Logging} +import org.apache.spark.mllib.regression.LabeledPoint +import org.apache.spark.mllib.tree.model.Split +import scala.util.control.Breaks._ +import org.apache.spark.mllib.tree.configuration.Strategy +import org.apache.spark.mllib.tree.configuration.QuantileStrategy._ +import org.apache.spark.mllib.tree.configuration.FeatureType._ +import org.apache.spark.mllib.tree.configuration.Algo._ +import org.apache.spark.mllib.tree.impurity.Impurity + +/** +A class that implements a decision tree algorithm for classification and regression. +It supports both continuous and categorical features. + +@param strategy The configuration parameters for the tree algorithm which specify the type of +algorithm (classification, +regression, etc.), feature type (continuous, categorical), depth of the tree, +quantile calculation strategy, etc. + */ +class DecisionTree private (val strategy : Strategy) extends Serializable with Logging { + + /** + Method to train a decision tree model over an RDD + + @param input RDD of [[org.apache.spark.mllib.regression.LabeledPoint]] used as training data + for DecisionTree + @return a DecisionTreeModel that can be used for prediction + */ + def train(input : RDD[LabeledPoint]) : DecisionTreeModel = { + + //Cache input RDD for speedup during multiple passes + input.cache() + + val (splits, bins) = DecisionTree.findSplitsBins(input, strategy) + logDebug("numSplits = " + bins(0).length) + strategy.numBins = bins(0).length + + val maxDepth = strategy.maxDepth + + val maxNumNodes = scala.math.pow(2,maxDepth).toInt - 1 + val filters = new Array[List[Filter]](maxNumNodes) + filters(0) = List() + val parentImpurities = new Array[Double](maxNumNodes) + //Dummy value for top node (updated during first split calculation) + //parentImpurities(0) = Double.MinValue + val nodes = new Array[Node](maxNumNodes) + + logDebug("algo = " + strategy.algo) + + breakable { + for (level <- 0 until maxDepth){ + + logDebug("#####################################") + logDebug("level = " + level) + logDebug("#####################################") + + //Find best split for all nodes at a level + val splitsStatsForLevel = DecisionTree.findBestSplits(input, parentImpurities, strategy, + level, filters,splits,bins) + + for ((nodeSplitStats, index) <- splitsStatsForLevel.view.zipWithIndex){ + + extractNodeInfo(nodeSplitStats, level, index, nodes) + extractInfoForLowerLevels(level, index, maxDepth, nodeSplitStats, parentImpurities, + filters) + logDebug("final best split = " + nodeSplitStats._1) + + } + require(scala.math.pow(2,level)==splitsStatsForLevel.length) + + val allLeaf = splitsStatsForLevel.forall(_._2.gain <= 0 ) + logDebug("all leaf = " + allLeaf) + if (allLeaf) break + + } + } + + val topNode = nodes(0) + topNode.build(nodes) + + val decisionTreeModel = { + return new DecisionTreeModel(topNode, strategy.algo) + } + + return decisionTreeModel + } + --- End diff -- Remove extra blank line.
--- 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. ---