MCPcopy Create free account
hub / github.com/EdwardRaff/JSAT / buildTree

Method buildTree

JSAT/src/jsat/classifiers/trees/ID3.java:77–141  ·  view source on GitHub ↗
( List<DataPointPair<Integer>> dataPoints, Set<Integer> remainingAtribues, final ExecutorService threadPool)

Source from the content-addressed store, hash-verified

75 }
76
77 private ID3Node buildTree( List<DataPointPair<Integer>> dataPoints, Set<Integer> remainingAtribues, final ExecutorService threadPool)
78 {
79 double curEntropy = entropy(dataPoints);
80 double size = dataPoints.size();
81
82 if(remainingAtribues.isEmpty() || curEntropy == 0)
83 {
84 CategoricalResults cr = new CategoricalResults(predicting.getNumOfCategories());
85 for(DataPointPair<Integer> dpp : dataPoints)
86 cr.setProb(dpp.getPair(), cr.getProb(dpp.getPair()) + 1);
87 cr.divideConst(size);
88
89 latch.countDown();
90 return new ID3Node(cr);
91 }
92
93 int bestAttribute = -1;
94 double bestInfoGain = Double.MIN_VALUE;
95 List<List<DataPointPair<Integer>>> bestSplit = null;
96
97 for(int attribute : remainingAtribues)
98 {
99 List<List<DataPointPair<Integer>>> newSplit = new ArrayList<List<DataPointPair<Integer>>>(attributes[attribute].getNumOfCategories());
100 for( int i = 0; i < attributes[attribute].getNumOfCategories(); i++)
101 newSplit.add( new ArrayList<DataPointPair<Integer>>());
102
103 //Putting the datapoints in their respective bins by attribute value
104 for(DataPointPair<Integer> dpp : dataPoints)
105 newSplit.get(dpp.getDataPoint().getCategoricalValue(attribute)).add(dpp);
106
107 double splitEntrop = 0;
108 for(int i = 0; i < newSplit.size(); i++)
109 splitEntrop += entropy(newSplit.get(i))*newSplit.get(i).size()/size;
110
111 double infoGain = curEntropy - splitEntrop;
112 if(infoGain > bestInfoGain)
113 {
114 bestAttribute = attribute;
115 bestInfoGain = infoGain;
116 bestSplit = newSplit;
117 }
118
119 }
120
121 final ID3Node node = new ID3Node(attributes[bestAttribute].getNumOfCategories(), bestAttribute);
122 final Set<Integer> newRemaining = new IntSet(remainingAtribues);
123 newRemaining.remove(bestAttribute);
124 for(int i = 0; i < bestSplit.size(); i++)
125 {
126 final int ii = i;
127 final List<DataPointPair<Integer>> bestSplitII = bestSplit.get(ii);
128 latch.countUp();
129 threadPool.submit(new Runnable() {
130
131 public void run()
132 {
133 node.setNode(ii, buildTree(bestSplitII, newRemaining, threadPool));
134 }

Callers 2

trainCMethod · 0.95
runMethod · 0.95

Calls 15

entropyMethod · 0.95
setProbMethod · 0.95
getProbMethod · 0.95
divideConstMethod · 0.95
removeMethod · 0.95
getNumOfCategoriesMethod · 0.80
countDownMethod · 0.80
getCategoricalValueMethod · 0.80
countUpMethod · 0.80
submitMethod · 0.80
sizeMethod · 0.65
getPairMethod · 0.45

Tested by

no test coverage detected