use of org.opensearch.ml.common.dataframe.ColumnMeta in project ml-commons by opensearch-project.
the class BatchRandomCutForest method process.
private List<Map<String, Object>> process(DataFrame dataFrame, RandomCutForest forest, Integer actualTrainingDataSize) {
List<Double> pointList = new ArrayList<>();
ColumnMeta[] columnMetas = dataFrame.columnMetas();
List<Map<String, Object>> predictResult = new ArrayList<>();
for (int rowNum = 0; rowNum < dataFrame.size(); rowNum++) {
for (int i = 0; i < columnMetas.length; i++) {
Row row = dataFrame.getRow(rowNum);
ColumnValue value = row.getValue(i);
pointList.add(value.doubleValue());
}
double[] point = pointList.stream().mapToDouble(d -> d).toArray();
pointList.clear();
double anomalyScore = forest.getAnomalyScore(point);
if (actualTrainingDataSize == null || rowNum < actualTrainingDataSize) {
forest.update(point);
}
Map<String, Object> result = new HashMap<>();
result.put("score", anomalyScore);
result.put("anomalous", anomalyScore > anomalyScoreThreshold);
predictResult.add(result);
}
return predictResult;
}
use of org.opensearch.ml.common.dataframe.ColumnMeta in project ml-commons by opensearch-project.
the class BatchRandomCutForestTest method constructRCFDataFrame.
private DataFrame constructRCFDataFrame(boolean predict) {
ColumnMeta[] columnMetas = new ColumnMeta[] { new ColumnMeta("value", ColumnType.INTEGER) };
DataFrame dataFrame = new DefaultDataFrame(columnMetas);
for (int i = 0; i < dataSize; i++) {
if (predict && i % 100 == 0) {
dataFrame.appendRow(new Object[] { ThreadLocalRandom.current().nextInt(100, 1000) });
} else {
dataFrame.appendRow(new Object[] { ThreadLocalRandom.current().nextInt(1, 10) });
}
}
return dataFrame;
}
use of org.opensearch.ml.common.dataframe.ColumnMeta 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.ColumnMeta 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;
}
Aggregations