|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml.dataframe; |
| 20 | + |
| 21 | +import org.elasticsearch.common.Nullable; |
| 22 | +import org.elasticsearch.common.ParseField; |
| 23 | +import org.elasticsearch.common.Strings; |
| 24 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 26 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +public class Classification implements DataFrameAnalysis { |
| 32 | + |
| 33 | + public static Classification fromXContent(XContentParser parser) { |
| 34 | + return PARSER.apply(parser, null); |
| 35 | + } |
| 36 | + |
| 37 | + public static Builder builder(String dependentVariable) { |
| 38 | + return new Builder(dependentVariable); |
| 39 | + } |
| 40 | + |
| 41 | + public static final ParseField NAME = new ParseField("classification"); |
| 42 | + |
| 43 | + static final ParseField DEPENDENT_VARIABLE = new ParseField("dependent_variable"); |
| 44 | + static final ParseField LAMBDA = new ParseField("lambda"); |
| 45 | + static final ParseField GAMMA = new ParseField("gamma"); |
| 46 | + static final ParseField ETA = new ParseField("eta"); |
| 47 | + static final ParseField MAXIMUM_NUMBER_TREES = new ParseField("maximum_number_trees"); |
| 48 | + static final ParseField FEATURE_BAG_FRACTION = new ParseField("feature_bag_fraction"); |
| 49 | + static final ParseField PREDICTION_FIELD_NAME = new ParseField("prediction_field_name"); |
| 50 | + static final ParseField TRAINING_PERCENT = new ParseField("training_percent"); |
| 51 | + |
| 52 | + private static final ConstructingObjectParser<Classification, Void> PARSER = |
| 53 | + new ConstructingObjectParser<>( |
| 54 | + NAME.getPreferredName(), |
| 55 | + true, |
| 56 | + a -> new Classification( |
| 57 | + (String) a[0], |
| 58 | + (Double) a[1], |
| 59 | + (Double) a[2], |
| 60 | + (Double) a[3], |
| 61 | + (Integer) a[4], |
| 62 | + (Double) a[5], |
| 63 | + (String) a[6], |
| 64 | + (Double) a[7])); |
| 65 | + |
| 66 | + static { |
| 67 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), DEPENDENT_VARIABLE); |
| 68 | + PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), LAMBDA); |
| 69 | + PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), GAMMA); |
| 70 | + PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), ETA); |
| 71 | + PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), MAXIMUM_NUMBER_TREES); |
| 72 | + PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), FEATURE_BAG_FRACTION); |
| 73 | + PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PREDICTION_FIELD_NAME); |
| 74 | + PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), TRAINING_PERCENT); |
| 75 | + } |
| 76 | + |
| 77 | + private final String dependentVariable; |
| 78 | + private final Double lambda; |
| 79 | + private final Double gamma; |
| 80 | + private final Double eta; |
| 81 | + private final Integer maximumNumberTrees; |
| 82 | + private final Double featureBagFraction; |
| 83 | + private final String predictionFieldName; |
| 84 | + private final Double trainingPercent; |
| 85 | + |
| 86 | + private Classification(String dependentVariable, @Nullable Double lambda, @Nullable Double gamma, @Nullable Double eta, |
| 87 | + @Nullable Integer maximumNumberTrees, @Nullable Double featureBagFraction, @Nullable String predictionFieldName, |
| 88 | + @Nullable Double trainingPercent) { |
| 89 | + this.dependentVariable = Objects.requireNonNull(dependentVariable); |
| 90 | + this.lambda = lambda; |
| 91 | + this.gamma = gamma; |
| 92 | + this.eta = eta; |
| 93 | + this.maximumNumberTrees = maximumNumberTrees; |
| 94 | + this.featureBagFraction = featureBagFraction; |
| 95 | + this.predictionFieldName = predictionFieldName; |
| 96 | + this.trainingPercent = trainingPercent; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String getName() { |
| 101 | + return NAME.getPreferredName(); |
| 102 | + } |
| 103 | + |
| 104 | + public String getDependentVariable() { |
| 105 | + return dependentVariable; |
| 106 | + } |
| 107 | + |
| 108 | + public Double getLambda() { |
| 109 | + return lambda; |
| 110 | + } |
| 111 | + |
| 112 | + public Double getGamma() { |
| 113 | + return gamma; |
| 114 | + } |
| 115 | + |
| 116 | + public Double getEta() { |
| 117 | + return eta; |
| 118 | + } |
| 119 | + |
| 120 | + public Integer getMaximumNumberTrees() { |
| 121 | + return maximumNumberTrees; |
| 122 | + } |
| 123 | + |
| 124 | + public Double getFeatureBagFraction() { |
| 125 | + return featureBagFraction; |
| 126 | + } |
| 127 | + |
| 128 | + public String getPredictionFieldName() { |
| 129 | + return predictionFieldName; |
| 130 | + } |
| 131 | + |
| 132 | + public Double getTrainingPercent() { |
| 133 | + return trainingPercent; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 138 | + builder.startObject(); |
| 139 | + builder.field(DEPENDENT_VARIABLE.getPreferredName(), dependentVariable); |
| 140 | + if (lambda != null) { |
| 141 | + builder.field(LAMBDA.getPreferredName(), lambda); |
| 142 | + } |
| 143 | + if (gamma != null) { |
| 144 | + builder.field(GAMMA.getPreferredName(), gamma); |
| 145 | + } |
| 146 | + if (eta != null) { |
| 147 | + builder.field(ETA.getPreferredName(), eta); |
| 148 | + } |
| 149 | + if (maximumNumberTrees != null) { |
| 150 | + builder.field(MAXIMUM_NUMBER_TREES.getPreferredName(), maximumNumberTrees); |
| 151 | + } |
| 152 | + if (featureBagFraction != null) { |
| 153 | + builder.field(FEATURE_BAG_FRACTION.getPreferredName(), featureBagFraction); |
| 154 | + } |
| 155 | + if (predictionFieldName != null) { |
| 156 | + builder.field(PREDICTION_FIELD_NAME.getPreferredName(), predictionFieldName); |
| 157 | + } |
| 158 | + if (trainingPercent != null) { |
| 159 | + builder.field(TRAINING_PERCENT.getPreferredName(), trainingPercent); |
| 160 | + } |
| 161 | + builder.endObject(); |
| 162 | + return builder; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public int hashCode() { |
| 167 | + return Objects.hash(dependentVariable, lambda, gamma, eta, maximumNumberTrees, featureBagFraction, predictionFieldName, |
| 168 | + trainingPercent); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean equals(Object o) { |
| 173 | + if (this == o) return true; |
| 174 | + if (o == null || getClass() != o.getClass()) return false; |
| 175 | + Classification that = (Classification) o; |
| 176 | + return Objects.equals(dependentVariable, that.dependentVariable) |
| 177 | + && Objects.equals(lambda, that.lambda) |
| 178 | + && Objects.equals(gamma, that.gamma) |
| 179 | + && Objects.equals(eta, that.eta) |
| 180 | + && Objects.equals(maximumNumberTrees, that.maximumNumberTrees) |
| 181 | + && Objects.equals(featureBagFraction, that.featureBagFraction) |
| 182 | + && Objects.equals(predictionFieldName, that.predictionFieldName) |
| 183 | + && Objects.equals(trainingPercent, that.trainingPercent); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public String toString() { |
| 188 | + return Strings.toString(this); |
| 189 | + } |
| 190 | + |
| 191 | + public static class Builder { |
| 192 | + private String dependentVariable; |
| 193 | + private Double lambda; |
| 194 | + private Double gamma; |
| 195 | + private Double eta; |
| 196 | + private Integer maximumNumberTrees; |
| 197 | + private Double featureBagFraction; |
| 198 | + private String predictionFieldName; |
| 199 | + private Double trainingPercent; |
| 200 | + |
| 201 | + private Builder(String dependentVariable) { |
| 202 | + this.dependentVariable = Objects.requireNonNull(dependentVariable); |
| 203 | + } |
| 204 | + |
| 205 | + public Builder setLambda(Double lambda) { |
| 206 | + this.lambda = lambda; |
| 207 | + return this; |
| 208 | + } |
| 209 | + |
| 210 | + public Builder setGamma(Double gamma) { |
| 211 | + this.gamma = gamma; |
| 212 | + return this; |
| 213 | + } |
| 214 | + |
| 215 | + public Builder setEta(Double eta) { |
| 216 | + this.eta = eta; |
| 217 | + return this; |
| 218 | + } |
| 219 | + |
| 220 | + public Builder setMaximumNumberTrees(Integer maximumNumberTrees) { |
| 221 | + this.maximumNumberTrees = maximumNumberTrees; |
| 222 | + return this; |
| 223 | + } |
| 224 | + |
| 225 | + public Builder setFeatureBagFraction(Double featureBagFraction) { |
| 226 | + this.featureBagFraction = featureBagFraction; |
| 227 | + return this; |
| 228 | + } |
| 229 | + |
| 230 | + public Builder setPredictionFieldName(String predictionFieldName) { |
| 231 | + this.predictionFieldName = predictionFieldName; |
| 232 | + return this; |
| 233 | + } |
| 234 | + |
| 235 | + public Builder setTrainingPercent(Double trainingPercent) { |
| 236 | + this.trainingPercent = trainingPercent; |
| 237 | + return this; |
| 238 | + } |
| 239 | + |
| 240 | + public Classification build() { |
| 241 | + return new Classification(dependentVariable, lambda, gamma, eta, maximumNumberTrees, featureBagFraction, predictionFieldName, |
| 242 | + trainingPercent); |
| 243 | + } |
| 244 | + } |
| 245 | +} |
0 commit comments