Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
cd53eae
skeletal framework
manishamde Nov 28, 2013
92cedce
basic building blocks for intermediate RDD calculation. untested.
manishamde Dec 2, 2013
8bca1e2
additional code for creating intermediate RDD
manishamde Dec 9, 2013
0012a77
basic stump working
manishamde Dec 10, 2013
03f534c
some more tests
manishamde Dec 10, 2013
dad0afc
decison stump functionality working
manishamde Dec 15, 2013
4798aae
added gain stats class
manishamde Dec 15, 2013
80e8c66
working version of multi-level split calculation
manishamde Dec 16, 2013
b0eb866
added logic to handle leaf nodes
manishamde Dec 16, 2013
98ec8d5
tree building and prediction logic
manishamde Dec 22, 2013
02c595c
added command line parsing
manishamde Dec 22, 2013
733d6dd
fixed tests
manishamde Dec 22, 2013
154aa77
enums for configurations
manishamde Dec 23, 2013
b0e3e76
adding enum for feature type
manishamde Jan 12, 2014
c8f6d60
adding enum for feature type
manishamde Jan 12, 2014
e23c2e5
added regression support
manishamde Jan 19, 2014
53108ed
fixing index for highest bin
manishamde Jan 20, 2014
6df35b9
regression predict logic
manishamde Jan 21, 2014
dbb7ac1
categorical feature support
manishamde Jan 23, 2014
d504eb1
more tests for categorical features
manishamde Jan 23, 2014
6b7de78
minor refactoring and tests
manishamde Jan 26, 2014
b09dc98
minor refactoring
manishamde Jan 26, 2014
c0e522b
updated predict and split threshold logic
manishamde Jan 27, 2014
f067d68
minor cleanup
manishamde Jan 27, 2014
5841c28
unit tests for categorical features
manishamde Jan 27, 2014
0dd7659
basic doc
manishamde Jan 27, 2014
dd0c0d7
minor: some docs
manishamde Jan 27, 2014
9372779
code style: max line lenght <= 100
manishamde Feb 17, 2014
84f85d6
code documentation
manishamde Feb 28, 2014
d3023b3
adding more docs for nested methods
manishamde Mar 6, 2014
63e786b
added multiple train methods for java compatability
manishamde Mar 6, 2014
cd2c2b4
fixing code style based on feedback
manishamde Mar 7, 2014
eb8fcbe
minor code style updates
manishamde Mar 7, 2014
794ff4d
minor improvements to docs and style
manishamde Mar 10, 2014
d1ef4f6
more documentation
manishamde Mar 10, 2014
ad1fc21
incorporated mengxr's code style suggestions
manishamde Mar 11, 2014
62c2562
fixing comment indentation
manishamde Mar 11, 2014
6068356
ensuring num bins is always greater than max number of categories
manishamde Mar 12, 2014
2116360
removing dummy bin calculation for categorical variables
manishamde Mar 12, 2014
632818f
removing threshold for classification predict method
manishamde Mar 13, 2014
ff363a7
binary search for bins and while loop for categorical feature bins
manishamde Mar 17, 2014
4576b64
documentation and for to while loop conversion
manishamde Mar 23, 2014
24500c5
minor style updates
mengxr Mar 23, 2014
c487e6a
Merge pull request #1 from mengxr/dtree
manishamde Mar 23, 2014
f963ef5
making methods private
manishamde Mar 23, 2014
201702f
making some more methods private
manishamde Mar 23, 2014
62dc723
updating javadoc and converting helper methods to package private to …
manishamde Mar 24, 2014
e1dd86f
implementing code style suggestions
manishamde Mar 25, 2014
f536ae9
another pass on code style
mengxr Mar 31, 2014
7d54b4f
Merge pull request #4 from mengxr/dtree
manishamde Mar 31, 2014
1e8c704
remove numBins field in the Strategy class
manishamde Apr 1, 2014
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
Prev Previous commit
Next Next commit
working version of multi-level split calculation
Signed-off-by: Manish Amde <[email protected]>
  • Loading branch information
manishamde committed Feb 28, 2014
commit 80e8c66dd25ad03c706f4993b10ba4caafa54c18
75 changes: 53 additions & 22 deletions mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.apache.spark.mllib.tree.model.Split
import org.apache.spark.mllib.tree.impurity.Gini


class DecisionTree(val strategy : Strategy) {
class DecisionTree(val strategy : Strategy) extends Logging {

def train(input : RDD[LabeledPoint]) : DecisionTreeModel = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no space between "input" and ":"


Expand All @@ -42,20 +42,43 @@ class DecisionTree(val strategy : Strategy) {

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 (calculate from scratch during first split calculation)
parentImpurities(0) = Double.MinValue

for (level <- 0 until maxDepth){

println("#####################################")
println("level = " + level)
println("#####################################")

//Find best split for all nodes at a level
val numNodes= scala.math.pow(2,level).toInt
//TODO: Change the input parent impurities values
val splits_stats_for_level = DecisionTree.findBestSplits(input, Array(2.0), strategy, level, filters,splits,bins)
for (tmp <- splits_stats_for_level){
println("final best split = " + tmp._1)
val splitsStatsForLevel = DecisionTree.findBestSplits(input, parentImpurities, strategy, level, filters,splits,bins)
for ((nodeSplitStats, index) <- splitsStatsForLevel.view.zipWithIndex){
for (i <- 0 to 1){
val nodeIndex = (scala.math.pow(2,level+1)).toInt - 1 + 2*index + i
if(level < maxDepth - 1){
val impurity = if (i == 0) nodeSplitStats._2.leftImpurity else nodeSplitStats._2.rightImpurity
println("nodeIndex = " + nodeIndex + ", impurity = " + impurity)
parentImpurities(nodeIndex) = impurity
println("updating nodeIndex = " + nodeIndex)
filters(nodeIndex) = new Filter(nodeSplitStats._1, if(i == 0) - 1 else 1) :: filters((nodeIndex-1)/2)
for (filter <- filters(nodeIndex)){
println(filter)
}
}
}
println("final best split = " + nodeSplitStats._1)
}
//TODO: update filters and decision tree model
require(scala.math.pow(2,level)==splits_stats_for_level.length)
require(scala.math.pow(2,level)==splitsStatsForLevel.length)


}

//TODO: Extract decision tree model

return new DecisionTreeModel()
}

Expand Down Expand Up @@ -99,7 +122,7 @@ object DecisionTree extends Serializable {
if (level == 0) {
List[Filter]()
} else {
val nodeFilterIndex = scala.math.pow(2, level).toInt + nodeIndex
val nodeFilterIndex = scala.math.pow(2, level).toInt - 1 + nodeIndex
//val parentFilterIndex = nodeFilterIndex / 2
//TODO: Check left or right filter
filters(nodeFilterIndex)
Expand Down Expand Up @@ -155,11 +178,11 @@ object DecisionTree extends Serializable {
// calculating bin index and label per feature per node
val arr = new Array[Double](1+(numFeatures * numNodes))
arr(0) = labeledPoint.label
for (nodeIndex <- 0 until numNodes) {
val parentFilters = findParentFilters(nodeIndex)
for (index <- 0 until numNodes) {
val parentFilters = findParentFilters(index)
//Find out whether the sample qualifies for the particular node
val sampleValid = isSampleValid(parentFilters, labeledPoint)
val shift = 1 + numFeatures * nodeIndex
val shift = 1 + numFeatures * index
if (!sampleValid) {
//Add to invalid bin index -1
for (featureIndex <- 0 until numFeatures) {
Expand Down Expand Up @@ -251,22 +274,26 @@ object DecisionTree extends Serializable {
val right1Count = rightNodeAgg(featureIndex)(2 * index + 1)
val rightCount = right0Count + right1Count

val impurity = if (level > 0) topImpurity else strategy.impurity.calculate(left0Count + right0Count, left1Count + right1Count)

if (leftCount == 0) return new InformationGainStats(0,topImpurity,Double.MinValue,0,topImpurity,rightCount.toLong)
if (rightCount == 0) return new InformationGainStats(0,topImpurity,topImpurity,leftCount.toLong,Double.MinValue,0)

//println("left0count = " + left0Count + ", left1count = " + left1Count + ", leftCount = " + leftCount)
val leftImpurity = strategy.impurity.calculate(left0Count, left1Count)


//println("right0count = " + right0Count + ", right1count = " + right1Count + ", rightCount = " + rightCount)
val rightImpurity = strategy.impurity.calculate(right0Count, right1Count)

val leftWeight = leftCount.toDouble / (leftCount + rightCount)
val rightWeight = rightCount.toDouble / (leftCount + rightCount)

val gain = topImpurity - leftWeight * leftImpurity - rightWeight * rightImpurity
val gain = {
if (level > 0) {
impurity - leftWeight * leftImpurity - rightWeight * rightImpurity
} else {
impurity - leftWeight * leftImpurity - rightWeight * rightImpurity
}
}

new InformationGainStats(gain,topImpurity,leftImpurity,leftCount.toLong,rightImpurity,rightCount.toLong)
new InformationGainStats(gain,impurity,leftImpurity,leftCount.toLong,rightImpurity,rightCount.toLong)

}

Expand Down Expand Up @@ -339,7 +366,7 @@ object DecisionTree extends Serializable {
var bestFeatureIndex = 0
var bestSplitIndex = 0
//Initialization with infeasible values
var bestGainStats = new InformationGainStats(-1.0,-1.0,-1.0,0,-1.0,0)
var bestGainStats = new InformationGainStats(Double.MinValue,-1.0,-1.0,0,-1.0,0)
// var maxGain = Double.MinValue
// var leftSamples = Long.MinValue
// var rightSamples = Long.MinValue
Expand All @@ -351,8 +378,8 @@ object DecisionTree extends Serializable {
bestGainStats = gainStats
bestFeatureIndex = featureIndex
bestSplitIndex = splitIndex
println("bestFeatureIndex = " + bestFeatureIndex + ", bestSplitIndex = " + bestSplitIndex
+ ", gain stats = " + bestGainStats)
//println("bestFeatureIndex = " + bestFeatureIndex + ", bestSplitIndex = " + bestSplitIndex)
//println( "gain stats = " + bestGainStats)
}
}
}
Expand All @@ -365,9 +392,12 @@ object DecisionTree extends Serializable {
//Calculate best splits for all nodes at a given level
val bestSplits = new Array[(Split, InformationGainStats)](numNodes)
for (node <- 0 until numNodes){
val nodeImpurityIndex = scala.math.pow(2, level).toInt - 1 + node
val shift = 2*node*numSplits*numFeatures
val binsForNode = binAggregates.slice(shift,shift+2*numSplits*numFeatures)
val parentNodeImpurity = parentImpurities(node/2)
println("nodeImpurityIndex = " + nodeImpurityIndex)
val parentNodeImpurity = parentImpurities(nodeImpurityIndex)
println("node impurity = " + parentNodeImpurity)
bestSplits(node) = binsToBestSplit(binsForNode, parentNodeImpurity)
}

Expand Down Expand Up @@ -456,8 +486,9 @@ object DecisionTree extends Serializable {

val sc = new SparkContext(args(0), "DecisionTree")
val data = loadLabeledData(sc, args(1))
val maxDepth = args(2).toInt

val strategy = new Strategy(kind = "classification", impurity = Gini, maxDepth = 2, numSplits = 569)
val strategy = new Strategy(kind = "classification", impurity = Gini, maxDepth = maxDepth, numSplits = 569)
val model = new DecisionTree(strategy).train(data)

sc.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ package org.apache.spark.mllib.tree.impurity

object Gini extends Impurity {

def calculate(c0 : Double, c1 : Double): Double = {
val total = c0 + c1
val f0 = c0 / total
val f1 = c1 / total
1 - f0*f0 - f1*f1
}
def calculate(c0 : Double, c1 : Double): Double = {
if (c0 == 0 || c1 == 0) {
0
} else {
val total = c0 + c1
val f0 = c0 / total
val f1 = c1 / total
1 - f0*f0 - f1*f1
}
}

}