Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions mllib/src/main/scala/org/apache/spark/ml/tree/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,10 @@ private[tree] class LearningNode(
new InternalNode(stats.impurityCalculator.predict, stats.impurity, stats.gain,
leftChild.get.toNode, rightChild.get.toNode, split.get, stats.impurityCalculator)
} else {
if (stats.valid) {
new LeafNode(stats.impurityCalculator.predict, stats.impurity,
stats.impurityCalculator)
} else {
// Here we want to keep same behavior with the old mllib.DecisionTreeModel
new LeafNode(stats.impurityCalculator.predict, -1.0, stats.impurityCalculator)
}

assert(stats != null, "Unknown error during Decision Tree learning. Could not convert " +
"LearningNode to Node")
new LeafNode(stats.impurityCalculator.predict, stats.impurity,
stats.impurityCalculator)
}
}

Expand Down Expand Up @@ -334,7 +330,7 @@ private[tree] object LearningNode {
id: Int,
isLeaf: Boolean,
stats: ImpurityStats): LearningNode = {
new LearningNode(id, None, None, None, false, stats)
new LearningNode(id, None, None, None, isLeaf, stats)
}

/** Create an empty node with the given node index. Values must be set later on. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.tree.impl

import org.apache.spark.ml.tree.Split

/**
* Helpers for updating DTStatsAggregators during collection of sufficient stats for tree training.
*/
private[impl] object AggUpdateUtils {

/**
* Updates the parent node stats of the passed-in impurity aggregator with the labels
* corresponding to the feature values at indices [from, to).
* @param indices Array of row indices for feature values; indices(i) = row index of the ith
* feature value
*/
private[impl] def updateParentImpurity(
statsAggregator: DTStatsAggregator,
indices: Array[Int],
from: Int,
to: Int,
instanceWeights: Array[Double],
labels: Array[Double]): Unit = {
from.until(to).foreach { idx =>
val rowIndex = indices(idx)
val label = labels(rowIndex)
statsAggregator.updateParent(label, instanceWeights(rowIndex))
}
}

/**
* Update aggregator for an (unordered feature, label) pair
* @param featureSplits Array of splits for the current feature
*/
private[impl] def updateUnorderedFeature(
agg: DTStatsAggregator,
featureValue: Int,
label: Double,
featureIndex: Int,
featureIndexIdx: Int,
featureSplits: Array[Split],
instanceWeight: Double): Unit = {
val leftNodeFeatureOffset = agg.getFeatureOffset(featureIndexIdx)
// Each unordered split has a corresponding bin for impurity stats of data points that fall
// onto the left side of the split. For each unordered split, update left-side bin if applicable
// for the current data point.
val numSplits = agg.metadata.numSplits(featureIndex)
var splitIndex = 0
while (splitIndex < numSplits) {
if (featureSplits(splitIndex).shouldGoLeft(featureValue, featureSplits)) {
agg.featureUpdate(leftNodeFeatureOffset, splitIndex, label, instanceWeight)
}
splitIndex += 1
}
}

/** Update aggregator for an (ordered feature, label) pair */
private[impl] def updateOrderedFeature(
agg: DTStatsAggregator,
featureValue: Int,
label: Double,
featureIndexIdx: Int,
instanceWeight: Double): Unit = {
// The bin index of an ordered feature is just the feature value itself
val binIndex = featureValue
agg.update(featureIndexIdx, binIndex, label, instanceWeight)
}

}
135 changes: 135 additions & 0 deletions mllib/src/main/scala/org/apache/spark/ml/tree/impl/ImpurityUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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.tree.impl

import org.apache.spark.mllib.tree.impurity._
import org.apache.spark.mllib.tree.model.ImpurityStats

/** Helper methods for impurity-related calculations during node split decisions. */
private[impl] object ImpurityUtils {

/**
* Get impurity calculator containing statistics for all labels for rows corresponding to
* feature values in [from, to).
* @param indices indices(i) = row index corresponding to ith feature value
*/
private[impl] def getParentImpurityCalculator(
metadata: DecisionTreeMetadata,
indices: Array[Int],
from: Int,
to: Int,
instanceWeights: Array[Double],
labels: Array[Double]): ImpurityCalculator = {
// Compute sufficient stats (e.g. label counts) for all data at the current node,
// store result in currNodeStatsAgg.parentStats so that we can share it across
// all features for the current node
val currNodeStatsAgg = new DTStatsAggregator(metadata, featureSubset = None)
AggUpdateUtils.updateParentImpurity(currNodeStatsAgg, indices, from, to,
instanceWeights, labels)
currNodeStatsAgg.getParentImpurityCalculator()
}

/**
* Calculate the impurity statistics for a given (feature, split) based upon left/right
* aggregates.
*
* @param parentImpurityCalculator An ImpurityCalculator containing the impurity stats
* of the node currently being split.
* @param leftImpurityCalculator left node aggregates for this (feature, split)
* @param rightImpurityCalculator right node aggregate for this (feature, split)
* @param metadata learning and dataset metadata for DecisionTree
* @return Impurity statistics for this (feature, split)
*/
private[impl] def calculateImpurityStats(
parentImpurityCalculator: ImpurityCalculator,
leftImpurityCalculator: ImpurityCalculator,
rightImpurityCalculator: ImpurityCalculator,
metadata: DecisionTreeMetadata): ImpurityStats = {

val impurity: Double = parentImpurityCalculator.calculate()

val leftCount = leftImpurityCalculator.count
val rightCount = rightImpurityCalculator.count

val totalCount = leftCount + rightCount

// If left child or right child doesn't satisfy minimum instances per node,
// then this split is invalid, return invalid information gain stats.
if ((leftCount < metadata.minInstancesPerNode) ||
(rightCount < metadata.minInstancesPerNode)) {
return ImpurityStats.getInvalidImpurityStats(parentImpurityCalculator)
}

val leftImpurity = leftImpurityCalculator.calculate() // Note: This equals 0 if count = 0
val rightImpurity = rightImpurityCalculator.calculate()

val leftWeight = leftCount / totalCount.toDouble
val rightWeight = rightCount / totalCount.toDouble

val gain = impurity - leftWeight * leftImpurity - rightWeight * rightImpurity
// If information gain doesn't satisfy minimum information gain,
// then this split is invalid, return invalid information gain stats.
if (gain < metadata.minInfoGain) {
return ImpurityStats.getInvalidImpurityStats(parentImpurityCalculator)
}

// If information gain is non-positive but doesn't violate the minimum info gain constraint,
// return a stats object with correct values but valid = false to indicate that we should not
// split.
if (gain <= 0) {
return new ImpurityStats(gain, impurity, parentImpurityCalculator, leftImpurityCalculator,
rightImpurityCalculator, valid = false)
}


new ImpurityStats(gain, impurity, parentImpurityCalculator,
leftImpurityCalculator, rightImpurityCalculator)
}

/**
* Given an impurity aggregator containing label statistics for a given (node, feature, bin),
* returns the corresponding "centroid", used to order bins while computing best splits.
*
* @param metadata learning and dataset metadata for DecisionTree
*/
private[impl] def getCentroid(
metadata: DecisionTreeMetadata,
binStats: ImpurityCalculator): Double = {

if (binStats.count != 0) {
if (metadata.isMulticlass) {
// multiclass classification
// For categorical features in multiclass classification,
// the bins are ordered by the impurity of their corresponding labels.
binStats.calculate()
} else if (metadata.isClassification) {
// binary classification
// For categorical features in binary classification,
// the bins are ordered by the count of class 1.
binStats.stats(1)
} else {
// regression
// For categorical features in regression and binary classification,
// the bins are ordered by the prediction.
binStats.predict
}
} else {
Double.MaxValue
}
}
}
Loading