use of org.opensearch.ml.common.parameter.MLInput 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.MLInput in project ml-commons by opensearch-project.
the class RestMLPredictionAction method getRequest.
/**
* Creates a MLPredictionTaskRequest from a RestRequest
*
* @param request RestRequest
* @return MLPredictionTaskRequest
*/
@VisibleForTesting
MLPredictionTaskRequest getRequest(RestRequest request) throws IOException {
String algorithm = getAlgorithm(request);
String modelId = getParameterId(request, PARAMETER_MODEL_ID);
XContentParser parser = request.contentParser();
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
MLInput mlInput = MLInput.parse(parser, algorithm);
return new MLPredictionTaskRequest(modelId, mlInput);
}
use of org.opensearch.ml.common.parameter.MLInput in project ml-commons by opensearch-project.
the class MLEngine method train.
public static Model train(Input input) {
validateMLInput(input);
MLInput mlInput = (MLInput) input;
Trainable trainable = MLEngineClassLoader.initInstance(mlInput.getAlgorithm(), mlInput.getParameters(), MLAlgoParams.class);
if (trainable == null) {
throw new IllegalArgumentException("Unsupported algorithm: " + mlInput.getAlgorithm());
}
return trainable.train(mlInput.getDataFrame());
}
use of org.opensearch.ml.common.parameter.MLInput in project ml-commons by opensearch-project.
the class MLEngine method predict.
public static MLOutput predict(Input input, Model model) {
validateMLInput(input);
MLInput mlInput = (MLInput) input;
Predictable predictable = MLEngineClassLoader.initInstance(mlInput.getAlgorithm(), mlInput.getParameters(), MLAlgoParams.class);
if (predictable == null) {
throw new IllegalArgumentException("Unsupported algorithm: " + mlInput.getAlgorithm());
}
return predictable.predict(mlInput.getDataFrame(), model);
}
use of org.opensearch.ml.common.parameter.MLInput in project ml-commons by opensearch-project.
the class MLEngine method trainAndPredict.
public static MLOutput trainAndPredict(Input input) {
validateMLInput(input);
MLInput mlInput = (MLInput) input;
TrainAndPredictable trainAndPredictable = MLEngineClassLoader.initInstance(mlInput.getAlgorithm(), mlInput.getParameters(), MLAlgoParams.class);
if (trainAndPredictable == null) {
throw new IllegalArgumentException("Unsupported algorithm: " + mlInput.getAlgorithm());
}
return trainAndPredictable.trainAndPredict(mlInput.getDataFrame());
}
Aggregations