Evaluate data frame analytics API

edit

Evaluates the machine learning algorithm that ran on a data frame. The API accepts an EvaluateDataFrameRequest object and returns an EvaluateDataFrameResponse.

Evaluate data frame analytics request

edit
EvaluateDataFrameRequest request = new EvaluateDataFrameRequest( 
    indexName, 
    new QueryConfig(QueryBuilders.termQuery("dataset", "blue")),  
    new BinarySoftClassification( 
        "label", 
        "p", 
        // Evaluation metrics 
        PrecisionMetric.at(0.4, 0.5, 0.6), 
        RecallMetric.at(0.5, 0.7), 
        ConfusionMatrixMetric.at(0.5), 
        AucRocMetric.withCurve())); 

Constructing a new evaluation request

Reference to an existing index

The query with which to select data from indices

Kind of evaluation to perform

Name of the field in the index. Its value denotes the actual (i.e. ground truth) label for an example. Must be either true or false

Name of the field in the index. Its value denotes the probability (as per some ML algorithm) of the example being classified as positive

The remaining parameters are the metrics to be calculated based on the two fields described above.

Precision calculated at thresholds: 0.4, 0.5 and 0.6

Recall calculated at thresholds: 0.5 and 0.7

Confusion matrix calculated at threshold 0.5

AuC ROC calculated and the curve points returned

Synchronous execution

edit

When executing a EvaluateDataFrameRequest in the following manner, the client waits for the EvaluateDataFrameResponse to be returned before continuing with code execution:

EvaluateDataFrameResponse response = client.machineLearning().evaluateDataFrame(request, RequestOptions.DEFAULT);

Synchronous calls may throw an IOException in case of either failing to parse the REST response in the high-level REST client, the request times out or similar cases where there is no response coming back from the server.

In cases where the server returns a 4xx or 5xx error code, the high-level client tries to parse the response body error details instead and then throws a generic ElasticsearchException and adds the original ResponseException as a suppressed exception to it.

Asynchronous execution

edit

Executing a EvaluateDataFrameRequest can also be done in an asynchronous fashion so that the client can return directly. Users need to specify how the response or potential failures will be handled by passing the request and a listener to the asynchronous evaluate-data-frame method:

client.machineLearning().evaluateDataFrameAsync(request, RequestOptions.DEFAULT, listener); 

The EvaluateDataFrameRequest to execute and the ActionListener to use when the execution completes

The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed. Failure scenarios and expected exceptions are the same as in the synchronous execution case.

A typical listener for evaluate-data-frame looks like:

ActionListener<EvaluateDataFrameResponse> listener = new ActionListener<EvaluateDataFrameResponse>() {
    @Override
    public void onResponse(EvaluateDataFrameResponse response) {
        
    }

    @Override
    public void onFailure(Exception e) {
        
    }
};

Called when the execution is successfully completed.

Called when the whole EvaluateDataFrameRequest fails.

Response

edit

The returned EvaluateDataFrameResponse contains the requested evaluation metrics.

List<EvaluationMetric.Result> metrics = response.getMetrics(); 

PrecisionMetric.Result precisionResult = response.getMetricByName(PrecisionMetric.NAME); 
double precision = precisionResult.getScoreByThreshold("0.4"); 

ConfusionMatrixMetric.Result confusionMatrixResult = response.getMetricByName(ConfusionMatrixMetric.NAME); 
ConfusionMatrix confusionMatrix = confusionMatrixResult.getScoreByThreshold("0.5"); 

Fetching all the calculated metrics results

Fetching precision metric by name

Fetching precision at a given (0.4) threshold

Fetching confusion matrix metric by name

Fetching confusion matrix at a given (0.5) threshold