Skip to content

Commit

Permalink
[ML] Make Ensemble feature names optional (#51996)
Browse files Browse the repository at this point in the history
The featureNames field is requisite in individual models but is not required by the Ensemble.
  • Loading branch information
davidkyle committed Feb 7, 2020
1 parent 3ccea31 commit d43bcbf
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public String getName() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
if (featureNames != null) {
if (featureNames != null && featureNames.isEmpty() == false) {
builder.field(FEATURE_NAMES.getPreferredName(), featureNames);
}
if (models != null) {
Expand Down Expand Up @@ -157,7 +157,7 @@ public static Builder builder() {
}

public static class Builder {
private List<String> featureNames;
private List<String> featureNames = Collections.emptyList();
private List<TrainedModel> trainedModels;
private OutputAggregator outputAggregator;
private TargetType targetType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static Ensemble createRandom(TargetType targetType) {
.toArray() :
null;

return new Ensemble(featureNames,
return new Ensemble(randomBoolean() ? featureNames : Collections.emptyList(),
models,
outputAggregator,
targetType,
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/ml/df-analytics/apis/put-inference.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ See <<ml-put-inference-preprocessor-example>> for more details.
The definition for a binary decision tree.

`tree`.`feature_names`:::
(Required, string)
(Required, string)
Features expected by the tree, in their expected order.

`tree`.`tree_structure`:::
Expand Down Expand Up @@ -221,7 +221,7 @@ children).
The definition for an ensemble model.

`ensemble`.`feature_names`:::
(Required, string)
(Optional, string)
Features expected by the ensemble, in their expected order.

`ensemble`.`trained_models`:::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ public String getName() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(FEATURE_NAMES.getPreferredName(), featureNames);
if (featureNames.isEmpty() == false) {
builder.field(FEATURE_NAMES.getPreferredName(), featureNames);
}
NamedXContentObjectHelper.writeNamedObjects(builder, params, true, TRAINED_MODELS.getPreferredName(), models);
NamedXContentObjectHelper.writeNamedObjects(builder,
params,
Expand Down Expand Up @@ -327,8 +329,9 @@ public static class Builder {
private double[] classificationWeights;
private boolean modelsAreOrdered;

private Builder (boolean modelsAreOrdered) {
private Builder(boolean modelsAreOrdered) {
this.modelsAreOrdered = modelsAreOrdered;
this.featureNames = Collections.emptyList();
}

private static Builder builderForParser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static Ensemble createRandom() {
.toArray() :
null;

return new Ensemble(featureNames,
return new Ensemble(randomBoolean() ? featureNames : Collections.emptyList(),
models,
outputAggregator,
targetType,
Expand Down

0 comments on commit d43bcbf

Please sign in to comment.