-
Notifications
You must be signed in to change notification settings - Fork 368
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
Add new xgboost parameter to represent missing features in feature vector #252
Closed
akshaykumar90
wants to merge
4
commits into
o19s:master
from
akshaykumar90:akshay_dectree_ranker_with_missing
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bbf4bf4
Add new xgboost parameter to represent missing features in feature ve…
e401247
Make NaiveAdditiveDecisionTree aware of missing value sentinel
ffd3c21
Make checkstyleTest pass
80abe56
Allow users to specify any floating point value as missing value
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 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 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 |
---|---|---|
|
@@ -57,18 +57,34 @@ public NaiveAdditiveDecisionTree parse(FeatureSet set, String model) { | |
float[] weights = new float[trees.length]; | ||
// Tree weights are already encoded in outputs | ||
Arrays.fill(weights, 1F); | ||
return new NaiveAdditiveDecisionTree(trees, weights, set.size(), modelDefinition.normalizer); | ||
return new NaiveAdditiveDecisionTree(trees, weights, set.size(), | ||
modelDefinition.normalizer, | ||
modelDefinition.missingValue); | ||
} | ||
|
||
private static class XGBoostDefinition { | ||
private static final ObjectParser<XGBoostDefinition, FeatureSet> PARSER; | ||
static { | ||
PARSER = new ObjectParser<>("xgboost_definition", XGBoostDefinition::new); | ||
PARSER.declareString(XGBoostDefinition::setNormalizer, new ParseField("objective")); | ||
|
||
// Parameters to represent missing features in feature vector | ||
// | ||
// There are convenience options like `use_float_max_for_missing` | ||
// which can be used instead of specifying the actual floating | ||
// point value. | ||
// | ||
// Only one of the following parameter should be specified | ||
// otherwise the behavior is undefined (depends on the order of | ||
// names in JSON). | ||
PARSER.declareFloat(XGBoostDefinition::setMissingValue, new ParseField("missing_value")); | ||
PARSER.declareBoolean(XGBoostDefinition::setFloatMaxForMissing, new ParseField("use_float_max_for_missing")); | ||
|
||
PARSER.declareObjectArray(XGBoostDefinition::setSplitParserStates, SplitParserState::parse, new ParseField("splits")); | ||
} | ||
|
||
private Normalizer normalizer; | ||
private Float missingValue; | ||
private List<SplitParserState> splitParserStates; | ||
|
||
public static XGBoostDefinition parse(XContentParser parser, FeatureSet set) throws IOException { | ||
|
@@ -132,6 +148,14 @@ void setNormalizer(String objectiveName) { | |
} | ||
} | ||
|
||
void setFloatMaxForMissing(boolean useFloatMaxForMissing) { | ||
this.missingValue = Float.MAX_VALUE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useFloatMaxForMissing is now ignored |
||
} | ||
|
||
void setMissingValue(float missingValue) { | ||
this.missingValue = missingValue; | ||
} | ||
|
||
void setSplitParserStates(List<SplitParserState> splitParserStates) { | ||
this.splitParserStates = splitParserStates; | ||
} | ||
|
@@ -140,7 +164,7 @@ Node[] getTrees(FeatureSet set) { | |
Node[] trees = new Node[splitParserStates.size()]; | ||
ListIterator<SplitParserState> it = splitParserStates.listIterator(); | ||
while(it.hasNext()) { | ||
trees[it.nextIndex()] = it.next().toNode(set); | ||
trees[it.nextIndex()] = it.next().toNode(set, this); | ||
} | ||
return trees; | ||
} | ||
|
@@ -169,7 +193,6 @@ private static class SplitParserState { | |
private Float threshold; | ||
private Integer rightNodeId; | ||
private Integer leftNodeId; | ||
// Ignored | ||
private Integer missingNodeId; | ||
private Float leaf; | ||
private List<SplitParserState> children; | ||
|
@@ -246,10 +269,16 @@ boolean isSplit() { | |
} | ||
|
||
|
||
Node toNode(FeatureSet set) { | ||
Node toNode(FeatureSet set, XGBoostDefinition xgb) { | ||
if (isSplit()) { | ||
return new NaiveAdditiveDecisionTree.Split(children.get(0).toNode(set), children.get(1).toNode(set), | ||
set.featureOrdinal(split), threshold); | ||
Node left = children.get(0).toNode(set, xgb); | ||
Node right = children.get(1).toNode(set, xgb); | ||
Node onMissing = this.missingNodeId.equals(this.rightNodeId) ? right : left; | ||
if (xgb.missingValue != null) { | ||
return new NaiveAdditiveDecisionTree.Split(left, right, onMissing, set.featureOrdinal(split), threshold); | ||
} else { | ||
return new NaiveAdditiveDecisionTree.Split(left, right, null, set.featureOrdinal(split), threshold); | ||
} | ||
} else { | ||
return new NaiveAdditiveDecisionTree.Leaf(leaf); | ||
} | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of adding a new param it might be more coherent to be able to parse a float and string
this way one could pass:
"missing_value": "max"
or
"missing_value": 0.0
everything else would throw an exception.
See https://github.com/elastic/elasticsearch/blob/237650e9c054149fd08213b38a81a3666c1868e5/server/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java#L66 to declare a field that accepts any kind of value
and https://github.com/elastic/elasticsearch/blob/f92ebb2ff909d0083ae988e04ecd398d979e9210/server/src/main/java/org/elasticsearch/common/unit/Fuzziness.java#L160 for how to parse the value