use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.
the class MLEngineTest method trainKMeansModel.
private Model trainKMeansModel() {
KMeansParams parameters = KMeansParams.builder().centroids(2).iterations(10).distanceType(KMeansParams.DistanceType.EUCLIDEAN).build();
DataFrame trainDataFrame = constructKMeansDataFrame(100);
MLInputDataset inputDataset = DataFrameInputDataset.builder().dataFrame(trainDataFrame).build();
Input mlInput = MLInput.builder().algorithm(FunctionName.KMEANS).parameters(parameters).inputDataset(inputDataset).build();
return MLEngine.train(mlInput);
}
use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.
the class MLPredictionTaskRequestTest method writeTo_Success.
@Test
public void writeTo_Success() throws IOException {
MLPredictionTaskRequest request = MLPredictionTaskRequest.builder().mlInput(mlInput).build();
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
request.writeTo(bytesStreamOutput);
request = new MLPredictionTaskRequest(bytesStreamOutput.bytes().streamInput());
assertEquals(FunctionName.KMEANS, request.getMlInput().getAlgorithm());
KMeansParams params = (KMeansParams) request.getMlInput().getParameters();
assertEquals(1, params.getCentroids().intValue());
MLInputDataset inputDataset = request.getMlInput().getInputDataset();
assertEquals(MLInputDataType.DATA_FRAME, inputDataset.getInputDataType());
DataFrame dataFrame = ((DataFrameInputDataset) inputDataset).getDataFrame();
assertEquals(1, dataFrame.size());
assertEquals(1, dataFrame.columnMetas().length);
assertEquals("key1", dataFrame.columnMetas()[0].getName());
assertEquals(ColumnType.DOUBLE, dataFrame.columnMetas()[0].getColumnType());
assertEquals(1, dataFrame.getRow(0).size());
assertEquals(2.00, dataFrame.getRow(0).getValue(0).getValue());
assertNull(request.getModelId());
}
use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.
the class KMeansTest method trainAndPredict.
@Test
public void trainAndPredict() {
KMeansParams parameters = KMeansParams.builder().distanceType(KMeansParams.DistanceType.EUCLIDEAN).iterations(10).centroids(2).build();
KMeans kMeans = new KMeans(parameters);
MLPredictionOutput output = (MLPredictionOutput) kMeans.trainAndPredict(trainDataFrame);
DataFrame predictions = output.getPredictionResult();
Assert.assertEquals(trainSize, predictions.size());
predictions.forEach(row -> Assert.assertTrue(row.getValue(0).intValue() == 0 || row.getValue(0).intValue() == 1));
parameters = parameters.toBuilder().distanceType(KMeansParams.DistanceType.COSINE).build();
kMeans = new KMeans(parameters);
output = (MLPredictionOutput) kMeans.trainAndPredict(trainDataFrame);
predictions = output.getPredictionResult();
Assert.assertEquals(trainSize, predictions.size());
parameters = parameters.toBuilder().distanceType(KMeansParams.DistanceType.L1).build();
kMeans = new KMeans(parameters);
output = (MLPredictionOutput) kMeans.trainAndPredict(trainDataFrame);
predictions = output.getPredictionResult();
Assert.assertEquals(trainSize, predictions.size());
}
use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.
the class RestMLTrainAndPredictIT method trainAndPredictKmeansWithEmptyParam.
private void trainAndPredictKmeansWithEmptyParam() throws IOException {
KMeansParams params = KMeansParams.builder().build();
trainAndPredictKmeansWithParmas(params, clusterCount -> assertEquals(2, clusterCount.size()));
}
use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.
the class RestMLTrainAndPredictIT method trainAndPredictKmeansWithCustomParam.
private void trainAndPredictKmeansWithCustomParam() throws IOException {
KMeansParams params = KMeansParams.builder().centroids(3).build();
trainAndPredictKmeansWithParmas(params, clusterCount -> assertTrue(clusterCount.size() >= 2));
}
Aggregations