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());
}
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;
}
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;
}
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);
});
}
}
Aggregations