-
Notifications
You must be signed in to change notification settings - Fork 2k
[WIP] GH-6769: multinomial dt yuliia #16310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
syzonyuliia
wants to merge
32
commits into
master
Choose a base branch
from
GH-6769_multinomial_DT_yuliia
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+463
−194
Draft
Changes from 13 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
46bf5cf
GH-6769: correct and prepare for multinomial
syzonyuliia-h2o 44ae3c2
GH-6769: adapt binning for the multinomial classification
syzonyuliia-h2o 476f0d0
GH-6769: adapt splitting for the multinomial classification
syzonyuliia-h2o 9680a8d
GH-6769: enable DT creation for the multinomial classification
syzonyuliia-h2o ad3035a
GH-6769: detect and fix bugs and improvements
syzonyuliia-h2o 631866a
GH-6769: fix tests
syzonyuliia-h2o b85c558
GH-6769: correct and prepare for multinomial
syzonyuliia-h2o c6ab1e7
GH-6769: adapt binning for the multinomial classification
syzonyuliia-h2o c689119
GH-6769: adapt splitting for the multinomial classification
syzonyuliia-h2o 9c0f9b2
GH-6769: enable DT creation for the multinomial classification
syzonyuliia-h2o c954c1b
GH-6769: detect and fix bugs and improvements
syzonyuliia-h2o 840b8f6
GH-6769: fix tests
syzonyuliia-h2o 8e087c8
Merge remote-tracking branch 'origin/GH-6769_multinomial_DT_yuliia' i…
syzonyuliia-h2o def4e00
GH-6769: clean code
syzonyuliia-h2o 24abdbe
GH-6769: enable multiclass probabilities in prediction
syzonyuliia-h2o 9b1b472
GH-6769: refactor for multiclass entropy
syzonyuliia-h2o 3cfcfa7
GH-6769: fix multiclass specifics
syzonyuliia-h2o 5d38297
GH-6769: clean comments
syzonyuliia-h2o d7bb06a
GH-6769: remove restriction on binary response
syzonyuliia-h2o 0ef110b
GH-6769: add distribution parameter
syzonyuliia-h2o 2bcf611
GH-6769: fix categorical splitting bug
syzonyuliia-h2o 455234e
GH-6769: update binomial test
syzonyuliia-h2o 067369c
GH-6769: adapt tests for multinomial features
syzonyuliia-h2o 789717d
GH-6769: add multinomial java tests
syzonyuliia-h2o 19d8cda
GH-6769: add multinomial python test
syzonyuliia-h2o 1a4db7c
GH-6769: add generated changes
syzonyuliia-h2o 2875434
Fix python tests
valenad1 8e041f1
GH-6769: add R test multinomial
syzonyuliia-h2o 1cb6282
GH-6769: add asserts to python test
syzonyuliia-h2o 6663953
fix R test
valenad1 aa7eb38
add assert to python
valenad1 b7d2aae
add assert to R
valenad1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,6 @@ | |
| import hex.tree.dt.binning.Histogram; | ||
| import hex.tree.dt.mrtasks.GetClassCountsMRTask; | ||
| import hex.tree.dt.mrtasks.ScoreDTTask; | ||
| import org.apache.commons.math3.util.Precision; | ||
| import org.apache.log4j.Logger; | ||
| import water.DKV; | ||
| import water.exceptions.H2OModelBuilderIllegalArgumentException; | ||
|
|
@@ -19,7 +18,7 @@ | |
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static hex.tree.dt.binning.SplitStatistics.entropyBinarySplit; | ||
| import static hex.tree.dt.binning.SplitStatistics.entropyBinarySplitMultinomial; | ||
|
|
||
| /** | ||
| * Decision Tree | ||
|
|
@@ -108,8 +107,8 @@ private AbstractSplittingRule findBestSplit(Histogram histogram) { | |
|
|
||
| private AbstractSplittingRule findBestSplitForFeature(Histogram histogram, int featureIndex) { | ||
| return (_train.vec(featureIndex).isNumeric() | ||
| ? histogram.calculateSplitStatisticsForNumericFeature(featureIndex) | ||
| : histogram.calculateSplitStatisticsForCategoricalFeature(featureIndex)) | ||
| ? histogram.calculateSplitStatisticsForNumericFeature(featureIndex, _nclass) | ||
| : histogram.calculateSplitStatisticsForCategoricalFeature(featureIndex, _nclass)) | ||
| .stream() | ||
| // todo - consider setting min count of samples in bin instead of filtering splits | ||
| .filter(binStatistics -> ((binStatistics._leftCount >= _min_rows) | ||
|
|
@@ -128,6 +127,7 @@ private AbstractSplittingRule findBestSplitForFeature(Histogram histogram, int f | |
|
|
||
|
|
||
| private static double calculateCriterionOfSplit(SplitStatistics binStatistics) { | ||
| // if(binStatistics.() == 2) // todo - fix bin statistics first, they are binomial-only now | ||
|
||
| return binStatistics.binaryEntropy(); | ||
syzonyuliia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
@@ -139,7 +139,7 @@ private static double calculateCriterionOfSplit(SplitStatistics binStatistics) { | |
| */ | ||
| private int selectDecisionValue(int[] countsByClass) { | ||
| if (_nclass == 1) { | ||
| return countsByClass[0]; | ||
| return 0; | ||
| } | ||
| int currentMaxClass = 0; | ||
| int currentMax = countsByClass[currentMaxClass]; | ||
|
|
@@ -205,11 +205,7 @@ public void buildNextNode(Queue<DataFeaturesLimits> limitsQueue, int nodeIndex) | |
| // compute node depth | ||
syzonyuliia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| int nodeDepth = (int) Math.floor(MathUtils.log2(nodeIndex + 1)); | ||
| // stop building from this node, the node will be a leaf | ||
| if ((nodeDepth >= _parms._max_depth) | ||
| || (countsByClass[0] <= _min_rows) | ||
| || (countsByClass[1] <= _min_rows) | ||
| // || zeroRatio > 0.999 || zeroRatio < 0.001 | ||
| ) { | ||
| if ((nodeDepth >= _parms._max_depth) || Arrays.stream(countsByClass).anyMatch(c -> c <= _min_rows)) { | ||
syzonyuliia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // add imaginary left and right children to imitate valid tree structure | ||
| // left child | ||
| limitsQueue.add(null); | ||
|
|
@@ -219,10 +215,10 @@ public void buildNextNode(Queue<DataFeaturesLimits> limitsQueue, int nodeIndex) | |
| return; | ||
| } | ||
|
|
||
| Histogram histogram = new Histogram(_train, actualLimits, BinningStrategy.EQUAL_WIDTH/*, minNumSamplesInBin - todo consider*/); | ||
| Histogram histogram = new Histogram(_train, actualLimits, BinningStrategy.EQUAL_WIDTH, _nclass/*, minNumSamplesInBin - todo consider*/); | ||
|
|
||
| AbstractSplittingRule bestSplittingRule = findBestSplit(histogram); | ||
| double criterionForTheParentNode = entropyBinarySplit(1.0 * countsByClass[0] / (countsByClass[0] + countsByClass[1])); | ||
| double criterionForTheParentNode = entropyBinarySplitMultinomial(countsByClass, Arrays.stream(countsByClass).sum()); | ||
| // if no split could be found, make a list from current node | ||
| // if the information gain is low, make a leaf from current node | ||
| if (bestSplittingRule == null | ||
syzonyuliia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
@@ -365,7 +361,7 @@ public BuilderVisibility builderVisibility() { | |
| public ModelCategory[] can_build() { | ||
| return new ModelCategory[]{ | ||
| ModelCategory.Binomial, | ||
| // ModelCategory.Multinomial, | ||
| ModelCategory.Multinomial, | ||
| // ModelCategory.Ordinal, | ||
| // ModelCategory.Regression | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 12 additions & 6 deletions
18
h2o-algos/src/main/java/hex/tree/dt/binning/CategoricalBin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,39 @@ | ||
| package hex.tree.dt.binning; | ||
|
|
||
| import org.apache.commons.lang.ArrayUtils; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * For categorical features values are already binned to categories - each bin corresponds to one value (category) | ||
| */ | ||
| public class CategoricalBin extends AbstractBin { | ||
| public int _category; | ||
|
|
||
| public CategoricalBin(int category, int count, int count0) { | ||
| public CategoricalBin(int category, int[] classesDistribution, int count) { | ||
| _category = category; | ||
| _classesDistribution = classesDistribution; | ||
| _count = count; | ||
| _count0 = count0; | ||
| } | ||
|
|
||
| public CategoricalBin(int category) { | ||
| public CategoricalBin(int category, int nclass) { | ||
| _category = category; | ||
| _classesDistribution = new int[nclass]; | ||
| _count = 0; | ||
| _count0 = 0; | ||
| } | ||
|
|
||
| public int getCategory() { | ||
| return _category; | ||
| } | ||
|
|
||
| public CategoricalBin clone() { | ||
| return new CategoricalBin(_category, _count, _count0); | ||
| return new CategoricalBin(_category, _classesDistribution, _count); | ||
| } | ||
|
|
||
| public double[] toDoubles() { | ||
| return new double[]{_category, _count, _count0}; | ||
| // category|count|class0|class1|... | ||
| return ArrayUtils.addAll(new double[]{_category, _count}, | ||
| Arrays.stream(_classesDistribution).asDoubleStream().toArray()); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.