( List<DataPointPair<Integer>> dataPoints, Set<Integer> remainingAtribues, final ExecutorService threadPool)
| 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 | } |
no test coverage detected