Skip to content

Commit 0c04ba7

Browse files
committed
Add num_top_classes parameter to HLRC
Document this parameter in HLRC docs
1 parent 2c5e1cc commit 0c04ba7

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Classification.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public static Builder builder(String dependentVariable) {
4848
static final ParseField FEATURE_BAG_FRACTION = new ParseField("feature_bag_fraction");
4949
static final ParseField PREDICTION_FIELD_NAME = new ParseField("prediction_field_name");
5050
static final ParseField TRAINING_PERCENT = new ParseField("training_percent");
51+
static final ParseField NUM_TOP_CLASSES = new ParseField("num_top_classes");
5152

5253
private static final ConstructingObjectParser<Classification, Void> PARSER =
5354
new ConstructingObjectParser<>(
@@ -61,7 +62,8 @@ public static Builder builder(String dependentVariable) {
6162
(Integer) a[4],
6263
(Double) a[5],
6364
(String) a[6],
64-
(Double) a[7]));
65+
(Double) a[7],
66+
(Integer) a[8]));
6567

6668
static {
6769
PARSER.declareString(ConstructingObjectParser.constructorArg(), DEPENDENT_VARIABLE);
@@ -72,6 +74,7 @@ public static Builder builder(String dependentVariable) {
7274
PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), FEATURE_BAG_FRACTION);
7375
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PREDICTION_FIELD_NAME);
7476
PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), TRAINING_PERCENT);
77+
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), NUM_TOP_CLASSES);
7578
}
7679

7780
private final String dependentVariable;
@@ -82,10 +85,11 @@ public static Builder builder(String dependentVariable) {
8285
private final Double featureBagFraction;
8386
private final String predictionFieldName;
8487
private final Double trainingPercent;
88+
private final Integer numTopClasses;
8589

8690
private Classification(String dependentVariable, @Nullable Double lambda, @Nullable Double gamma, @Nullable Double eta,
8791
@Nullable Integer maximumNumberTrees, @Nullable Double featureBagFraction, @Nullable String predictionFieldName,
88-
@Nullable Double trainingPercent) {
92+
@Nullable Double trainingPercent, @Nullable Integer numTopClasses) {
8993
this.dependentVariable = Objects.requireNonNull(dependentVariable);
9094
this.lambda = lambda;
9195
this.gamma = gamma;
@@ -94,6 +98,7 @@ private Classification(String dependentVariable, @Nullable Double lambda, @Nulla
9498
this.featureBagFraction = featureBagFraction;
9599
this.predictionFieldName = predictionFieldName;
96100
this.trainingPercent = trainingPercent;
101+
this.numTopClasses = numTopClasses;
97102
}
98103

99104
@Override
@@ -133,6 +138,10 @@ public Double getTrainingPercent() {
133138
return trainingPercent;
134139
}
135140

141+
public Integer getNumTopClasses() {
142+
return numTopClasses;
143+
}
144+
136145
@Override
137146
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
138147
builder.startObject();
@@ -158,14 +167,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
158167
if (trainingPercent != null) {
159168
builder.field(TRAINING_PERCENT.getPreferredName(), trainingPercent);
160169
}
170+
if (numTopClasses != null) {
171+
builder.field(NUM_TOP_CLASSES.getPreferredName(), numTopClasses);
172+
}
161173
builder.endObject();
162174
return builder;
163175
}
164176

165177
@Override
166178
public int hashCode() {
167179
return Objects.hash(dependentVariable, lambda, gamma, eta, maximumNumberTrees, featureBagFraction, predictionFieldName,
168-
trainingPercent);
180+
trainingPercent, numTopClasses);
169181
}
170182

171183
@Override
@@ -180,7 +192,8 @@ public boolean equals(Object o) {
180192
&& Objects.equals(maximumNumberTrees, that.maximumNumberTrees)
181193
&& Objects.equals(featureBagFraction, that.featureBagFraction)
182194
&& Objects.equals(predictionFieldName, that.predictionFieldName)
183-
&& Objects.equals(trainingPercent, that.trainingPercent);
195+
&& Objects.equals(trainingPercent, that.trainingPercent)
196+
&& Objects.equals(numTopClasses, that.numTopClasses);
184197
}
185198

186199
@Override
@@ -197,6 +210,7 @@ public static class Builder {
197210
private Double featureBagFraction;
198211
private String predictionFieldName;
199212
private Double trainingPercent;
213+
private Integer numTopClasses;
200214

201215
private Builder(String dependentVariable) {
202216
this.dependentVariable = Objects.requireNonNull(dependentVariable);
@@ -237,9 +251,14 @@ public Builder setTrainingPercent(Double trainingPercent) {
237251
return this;
238252
}
239253

254+
public Builder setNumTopClasses(Integer numTopClasses) {
255+
this.numTopClasses = numTopClasses;
256+
return this;
257+
}
258+
240259
public Classification build() {
241260
return new Classification(dependentVariable, lambda, gamma, eta, maximumNumberTrees, featureBagFraction, predictionFieldName,
242-
trainingPercent);
261+
trainingPercent, numTopClasses);
243262
}
244263
}
245264
}

client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,8 +1266,7 @@ public void testPutDataFrameAnalyticsConfig_GivenRegression() throws Exception {
12661266
.setDest(DataFrameAnalyticsDest.builder()
12671267
.setIndex("put-test-dest-index")
12681268
.build())
1269-
.setAnalysis(org.elasticsearch.client.ml.dataframe.Regression
1270-
.builder("my_dependent_variable")
1269+
.setAnalysis(org.elasticsearch.client.ml.dataframe.Regression.builder("my_dependent_variable")
12711270
.setTrainingPercent(80.0)
12721271
.build())
12731272
.setDescription("this is a regression")
@@ -1301,9 +1300,9 @@ public void testPutDataFrameAnalyticsConfig_GivenClassification() throws Excepti
13011300
.setDest(DataFrameAnalyticsDest.builder()
13021301
.setIndex("put-test-dest-index")
13031302
.build())
1304-
.setAnalysis(org.elasticsearch.client.ml.dataframe.Classification
1305-
.builder("my_dependent_variable")
1303+
.setAnalysis(org.elasticsearch.client.ml.dataframe.Classification.builder("my_dependent_variable")
13061304
.setTrainingPercent(80.0)
1305+
.setNumTopClasses(1)
13071306
.build())
13081307
.setDescription("this is a classification")
13091308
.build();

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,6 +2951,7 @@ public void testPutDataFrameAnalytics() throws Exception {
29512951
.setFeatureBagFraction(0.4) // <6>
29522952
.setPredictionFieldName("my_prediction_field_name") // <7>
29532953
.setTrainingPercent(50.0) // <8>
2954+
.setNumTopClasses(1) // <9>
29542955
.build();
29552956
// end::put-data-frame-analytics-classification
29562957

client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/ClassificationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static Classification randomClassification() {
3434
.setFeatureBagFraction(randomBoolean() ? null : randomDoubleBetween(0.0, 1.0, false))
3535
.setPredictionFieldName(randomBoolean() ? null : randomAlphaOfLength(10))
3636
.setTrainingPercent(randomBoolean() ? null : randomDoubleBetween(1.0, 100.0, true))
37+
.setNumTopClasses(randomBoolean() ? null : randomIntBetween(0, 10))
3738
.build();
3839
}
3940

docs/java-rest/high-level/ml/put-data-frame-analytics.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ include-tagged::{doc-tests-file}[{api}-classification]
118118
<6> The fraction of features which will be used when selecting a random bag for each candidate split. A double in (0, 1].
119119
<7> The name of the prediction field in the results object.
120120
<8> The percentage of training-eligible rows to be used in training. Defaults to 100%.
121+
<9> The number of top classes to be reported in the results. Defaults to 2.
121122

122123
===== Regression
123124

0 commit comments

Comments
 (0)