-
Notifications
You must be signed in to change notification settings - Fork 25.2k
[ML] Adds support for regression.mean_squared_error to eval API #44140
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
Merged
benwtrent
merged 4 commits into
elastic:master
from
benwtrent:feature/ml-add-regression-mse-evaluation
Jul 11, 2019
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
130 changes: 130 additions & 0 deletions
130
...in/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredError.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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.ml.dataframe.evaluation.regression; | ||
|
||
import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.ParsingException; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
|
||
/** | ||
* Calculates the mean squared error between two known numerical fields. | ||
* | ||
* equation: mse = 1/n * Σ(y - y´)^2 | ||
*/ | ||
public class MeanSquaredError implements EvaluationMetric { | ||
|
||
public static final String NAME = "mean_squared_error"; | ||
|
||
private static final ObjectParser<MeanSquaredError, Void> PARSER = | ||
new ObjectParser<>("mean_squared_error", true, MeanSquaredError::new); | ||
|
||
public static MeanSquaredError fromXContent(XContentParser parser) { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
public MeanSquaredError() { | ||
|
||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
// create static hash code from name as there are currently no unique fields per class instance | ||
return Objects.hashCode(NAME); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
public static class Result implements EvaluationMetric.Result { | ||
|
||
public static final ParseField ERROR = new ParseField("error"); | ||
private final double error; | ||
|
||
public static Result fromXContent(XContentParser parser) { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
private static final ConstructingObjectParser<Result, Void> PARSER = | ||
new ConstructingObjectParser<>("mean_squared_error_result", true, args -> new Result((double) args[0])); | ||
|
||
static { | ||
PARSER.declareDouble(constructorArg(), ERROR); | ||
} | ||
|
||
public Result(double error) { | ||
this.error = error; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(ERROR.getPreferredName(), error); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public double getError() { | ||
return error; | ||
} | ||
|
||
@Override | ||
public String getMetricName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Result that = (Result) o; | ||
return Objects.equals(that.error, this.error); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(error); | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
...src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/Regression.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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.ml.dataframe.evaluation.regression; | ||
|
||
import org.elasticsearch.client.ml.dataframe.evaluation.Evaluation; | ||
import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Evaluation of regression results. | ||
*/ | ||
public class Regression implements Evaluation { | ||
|
||
public static final String NAME = "regression"; | ||
|
||
private static final ParseField ACTUAL_FIELD = new ParseField("actual_field"); | ||
private static final ParseField PREDICTED_FIELD = new ParseField("predicted_field"); | ||
private static final ParseField METRICS = new ParseField("metrics"); | ||
|
||
@SuppressWarnings("unchecked") | ||
public static final ConstructingObjectParser<Regression, Void> PARSER = new ConstructingObjectParser<>( | ||
NAME, a -> new Regression((String) a[0], (String) a[1], (List<EvaluationMetric>) a[2])); | ||
|
||
static { | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), ACTUAL_FIELD); | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), PREDICTED_FIELD); | ||
PARSER.declareNamedObjects(ConstructingObjectParser.optionalConstructorArg(), | ||
(p, c, n) -> p.namedObject(EvaluationMetric.class, n, c), METRICS); | ||
} | ||
|
||
public static Regression fromXContent(XContentParser parser) { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
/** | ||
* The field containing the actual value | ||
* The value of this field is assumed to be numeric | ||
*/ | ||
private final String actualField; | ||
|
||
/** | ||
* The field containing the predicted value | ||
* The value of this field is assumed to be numeric | ||
*/ | ||
private final String predictedField; | ||
|
||
/** | ||
* The list of metrics to calculate | ||
*/ | ||
private final List<EvaluationMetric> metrics; | ||
|
||
public Regression(String actualField, String predictedField) { | ||
this(actualField, predictedField, Collections.singletonList(new MeanSquaredError())); | ||
przemekwitek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
Regression(String actualField, String predictedField, @Nullable List<EvaluationMetric> metrics) { | ||
this.actualField = actualField; | ||
this.predictedField = predictedField; | ||
this.metrics = metrics; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(ACTUAL_FIELD.getPreferredName(), actualField); | ||
builder.field(PREDICTED_FIELD.getPreferredName(), predictedField); | ||
|
||
builder.startObject(METRICS.getPreferredName()); | ||
for (EvaluationMetric metric : metrics) { | ||
builder.field(metric.getName(), metric); | ||
} | ||
builder.endObject(); | ||
|
||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Regression that = (Regression) o; | ||
return Objects.equals(that.actualField, this.actualField) | ||
&& Objects.equals(that.predictedField, this.predictedField) | ||
&& Objects.equals(that.metrics, this.metrics); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(actualField, predictedField, metrics); | ||
} | ||
} |
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
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.