Search in sources :

Example 16 with MLPredictionOutput

use of org.opensearch.ml.common.parameter.MLPredictionOutput in project ml-commons by opensearch-project.

the class BatchRandomCutForestTest method predict.

@Test
public void predict() {
    Model model = forest.train(trainDataFrame);
    MLPredictionOutput output = (MLPredictionOutput) forest.predict(predictionDataFrame, model);
    verifyPredictionResult(output);
}
Also used : Model(org.opensearch.ml.common.parameter.Model) MLPredictionOutput(org.opensearch.ml.common.parameter.MLPredictionOutput) Test(org.junit.Test)

Example 17 with MLPredictionOutput

use of org.opensearch.ml.common.parameter.MLPredictionOutput in project ml-commons by opensearch-project.

the class BatchRandomCutForestTest method trainAndPredict.

@Test
public void trainAndPredict() {
    MLPredictionOutput output = (MLPredictionOutput) forest.trainAndPredict(trainDataFrame);
    verifyPredictionResult(output);
}
Also used : MLPredictionOutput(org.opensearch.ml.common.parameter.MLPredictionOutput) Test(org.junit.Test)

Example 18 with MLPredictionOutput

use of org.opensearch.ml.common.parameter.MLPredictionOutput in project ml-commons by opensearch-project.

the class LinearRegressionTest method predict.

@Test
public void predict() {
    LinearRegression regression = new LinearRegression(parameters);
    Model model = regression.train(trainDataFrame);
    MLPredictionOutput output = (MLPredictionOutput) regression.predict(predictionDataFrame, model);
    DataFrame predictions = output.getPredictionResult();
    Assert.assertEquals(2, predictions.size());
}
Also used : Model(org.opensearch.ml.common.parameter.Model) MLPredictionOutput(org.opensearch.ml.common.parameter.MLPredictionOutput) LinearRegressionHelper.constructLinearRegressionPredictionDataFrame(org.opensearch.ml.engine.helper.LinearRegressionHelper.constructLinearRegressionPredictionDataFrame) DataFrame(org.opensearch.ml.common.dataframe.DataFrame) LinearRegressionHelper.constructLinearRegressionTrainDataFrame(org.opensearch.ml.engine.helper.LinearRegressionHelper.constructLinearRegressionTrainDataFrame) Test(org.junit.Test)

Example 19 with MLPredictionOutput

use of org.opensearch.ml.common.parameter.MLPredictionOutput in project ml-commons by opensearch-project.

the class IntegTestUtils method predictAndVerifyResult.

// Predict with the model generated, and verify the prediction result.
public static void predictAndVerifyResult(String taskId, MLInputDataset inputDataset) throws IOException {
    MLInput mlInput = MLInput.builder().algorithm(FunctionName.KMEANS).inputDataset(inputDataset).build();
    MLPredictionTaskRequest predictionRequest = new MLPredictionTaskRequest(taskId, mlInput);
    ActionFuture<MLTaskResponse> predictionFuture = client().execute(MLPredictionTaskAction.INSTANCE, predictionRequest);
    MLTaskResponse predictionResponse = predictionFuture.actionGet();
    XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
    builder.startObject();
    MLPredictionOutput mlPredictionOutput = (MLPredictionOutput) predictionResponse.getOutput();
    mlPredictionOutput.getPredictionResult().toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    String jsonStr = Strings.toString(builder);
    String expectedStr1 = "{\"column_metas\":[{\"name\":\"ClusterID\",\"column_type\":\"INTEGER\"}]," + "\"rows\":[{\"values\":[{\"column_type\":\"INTEGER\",\"value\":0}]}]}";
    String expectedStr2 = "{\"column_metas\":[{\"name\":\"ClusterID\",\"column_type\":\"INTEGER\"}]," + "\"rows\":[{\"values\":[{\"column_type\":\"INTEGER\",\"value\":1}]}]}";
    // The prediction result would not be a fixed value.
    assertTrue(expectedStr1.equals(jsonStr) || expectedStr2.equals(jsonStr));
}
Also used : MLTaskResponse(org.opensearch.ml.common.transport.MLTaskResponse) MLInput(org.opensearch.ml.common.parameter.MLInput) MLPredictionOutput(org.opensearch.ml.common.parameter.MLPredictionOutput) MLPredictionTaskRequest(org.opensearch.ml.common.transport.prediction.MLPredictionTaskRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

MLPredictionOutput (org.opensearch.ml.common.parameter.MLPredictionOutput)19 Test (org.junit.Test)16 DataFrame (org.opensearch.ml.common.dataframe.DataFrame)8 Model (org.opensearch.ml.common.parameter.Model)8 MLTaskResponse (org.opensearch.ml.common.transport.MLTaskResponse)8 MLInput (org.opensearch.ml.common.parameter.MLInput)7 KMeansHelper.constructKMeansDataFrame (org.opensearch.ml.engine.helper.KMeansHelper.constructKMeansDataFrame)5 HashMap (java.util.HashMap)4 LinearRegressionHelper.constructLinearRegressionPredictionDataFrame (org.opensearch.ml.engine.helper.LinearRegressionHelper.constructLinearRegressionPredictionDataFrame)4 LinearRegressionHelper.constructLinearRegressionTrainDataFrame (org.opensearch.ml.engine.helper.LinearRegressionHelper.constructLinearRegressionTrainDataFrame)4 DataFrameInputDataset (org.opensearch.ml.common.dataset.DataFrameInputDataset)3 MLInputDataset (org.opensearch.ml.common.dataset.MLInputDataset)3 Input (org.opensearch.ml.common.parameter.Input)3 LocalSampleCalculatorInput (org.opensearch.ml.common.parameter.LocalSampleCalculatorInput)3 MLOutput (org.opensearch.ml.common.parameter.MLOutput)3 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 DefaultDataFrame (org.opensearch.ml.common.dataframe.DefaultDataFrame)2 MLPredictionTaskRequest (org.opensearch.ml.common.transport.prediction.MLPredictionTaskRequest)2 OpenSearchException (org.opensearch.OpenSearchException)1