Search in sources :

Example 31 with DataFrame

use of org.opensearch.ml.common.dataframe.DataFrame 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 32 with DataFrame

use of org.opensearch.ml.common.dataframe.DataFrame in project ml-commons by opensearch-project.

the class SampleAlgoTest method constructDataFrame.

private DataFrame constructDataFrame(int dataSize) {
    ColumnMeta[] columnMetas = new ColumnMeta[] { new ColumnMeta("value", ColumnType.INTEGER) };
    DataFrame dataFrame = new DefaultDataFrame(columnMetas);
    for (int i = 0; i < dataSize; i++) {
        dataFrame.appendRow(new Object[] { i });
    }
    return dataFrame;
}
Also used : ColumnMeta(org.opensearch.ml.common.dataframe.ColumnMeta) DataFrame(org.opensearch.ml.common.dataframe.DataFrame) DefaultDataFrame(org.opensearch.ml.common.dataframe.DefaultDataFrame) DefaultDataFrame(org.opensearch.ml.common.dataframe.DefaultDataFrame)

Example 33 with DataFrame

use of org.opensearch.ml.common.dataframe.DataFrame in project ml-commons by opensearch-project.

the class KMeansHelper method constructKMeansDataFrame.

public static DataFrame constructKMeansDataFrame(int size) {
    ColumnMeta[] columnMetas = new ColumnMeta[] { new ColumnMeta("f1", ColumnType.DOUBLE), new ColumnMeta("f2", ColumnType.DOUBLE) };
    DataFrame dataFrame = DataFrameBuilder.emptyDataFrame(columnMetas);
    Random random = new Random(1);
    MultivariateNormalDistribution g1 = new MultivariateNormalDistribution(new JDKRandomGenerator(random.nextInt()), new double[] { 0.0, 0.0 }, new double[][] { { 2.0, 1.0 }, { 1.0, 2.0 } });
    MultivariateNormalDistribution g2 = new MultivariateNormalDistribution(new JDKRandomGenerator(random.nextInt()), new double[] { 10.0, 10.0 }, new double[][] { { 2.0, 1.0 }, { 1.0, 2.0 } });
    MultivariateNormalDistribution[] normalDistributions = new MultivariateNormalDistribution[] { g1, g2 };
    for (int i = 0; i < size; ++i) {
        int id = 0;
        if (Math.random() < 0.5) {
            id = 1;
        }
        double[] sample = normalDistributions[id].sample();
        dataFrame.appendRow(Arrays.stream(sample).boxed().toArray(Double[]::new));
    }
    return dataFrame;
}
Also used : ColumnMeta(org.opensearch.ml.common.dataframe.ColumnMeta) Random(java.util.Random) DataFrame(org.opensearch.ml.common.dataframe.DataFrame) JDKRandomGenerator(org.apache.commons.math3.random.JDKRandomGenerator) MultivariateNormalDistribution(org.apache.commons.math3.distribution.MultivariateNormalDistribution)

Example 34 with DataFrame

use of org.opensearch.ml.common.dataframe.DataFrame in project ml-commons by opensearch-project.

the class MLTrainAndPredictTaskRunner method startTrainAndPredictionTask.

/**
 * Start prediction task
 * @param request MLPredictionTaskRequest
 * @param listener Action listener
 */
public void startTrainAndPredictionTask(MLTrainingTaskRequest request, ActionListener<MLTaskResponse> listener) {
    MLInputDataType inputDataType = request.getMlInput().getInputDataset().getInputDataType();
    Instant now = Instant.now();
    MLTask mlTask = MLTask.builder().taskId(UUID.randomUUID().toString()).taskType(MLTaskType.TRAINING_AND_PREDICTION).inputType(inputDataType).functionName(request.getMlInput().getFunctionName()).state(MLTaskState.CREATED).workerNode(clusterService.localNode().getId()).createTime(now).lastUpdateTime(now).async(false).build();
    MLInput mlInput = request.getMlInput();
    if (mlInput.getInputDataset().getInputDataType().equals(MLInputDataType.SEARCH_QUERY)) {
        ActionListener<DataFrame> dataFrameActionListener = ActionListener.wrap(dataFrame -> {
            trainAndPredict(mlTask, dataFrame, request, listener);
        }, e -> {
            log.error("Failed to generate DataFrame from search query", e);
            handlePredictFailure(mlTask, listener, e, false);
        });
        mlInputDatasetHandler.parseSearchQueryInput(mlInput.getInputDataset(), new ThreadedActionListener<>(log, threadPool, TASK_THREAD_POOL, dataFrameActionListener, false));
    } else {
        DataFrame inputDataFrame = mlInputDatasetHandler.parseDataFrameInput(mlInput.getInputDataset());
        threadPool.executor(TASK_THREAD_POOL).execute(() -> {
            trainAndPredict(mlTask, inputDataFrame, request, listener);
        });
    }
}
Also used : MLInput(org.opensearch.ml.common.parameter.MLInput) Instant(java.time.Instant) MLInputDataType(org.opensearch.ml.common.dataset.MLInputDataType) DataFrame(org.opensearch.ml.common.dataframe.DataFrame) MLTask(org.opensearch.ml.common.parameter.MLTask)

Aggregations

DataFrame (org.opensearch.ml.common.dataframe.DataFrame)34 ColumnMeta (org.opensearch.ml.common.dataframe.ColumnMeta)10 DefaultDataFrame (org.opensearch.ml.common.dataframe.DefaultDataFrame)10 MLPredictionOutput (org.opensearch.ml.common.parameter.MLPredictionOutput)10 MLInput (org.opensearch.ml.common.parameter.MLInput)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 Model (org.opensearch.ml.common.parameter.Model)8 Row (org.opensearch.ml.common.dataframe.Row)7 DataFrameInputDataset (org.opensearch.ml.common.dataset.DataFrameInputDataset)7 MLInputDataset (org.opensearch.ml.common.dataset.MLInputDataset)7 KMeansHelper.constructKMeansDataFrame (org.opensearch.ml.engine.helper.KMeansHelper.constructKMeansDataFrame)7 HashMap (java.util.HashMap)6 ColumnValue (org.opensearch.ml.common.dataframe.ColumnValue)6 LinearRegressionHelper.constructLinearRegressionPredictionDataFrame (org.opensearch.ml.engine.helper.LinearRegressionHelper.constructLinearRegressionPredictionDataFrame)5 LinearRegressionHelper.constructLinearRegressionTrainDataFrame (org.opensearch.ml.engine.helper.LinearRegressionHelper.constructLinearRegressionTrainDataFrame)5 List (java.util.List)4 Before (org.junit.Before)4 Input (org.opensearch.ml.common.parameter.Input)4 LocalSampleCalculatorInput (org.opensearch.ml.common.parameter.LocalSampleCalculatorInput)4