Example usage for org.apache.commons.lang3 ObjectUtils mode

List of usage examples for org.apache.commons.lang3 ObjectUtils mode

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ObjectUtils mode.

Prototype

public static <T> T mode(final T... items) 

Source Link

Document

Find the most frequently occurring item.

Usage

From source file:org.wso2.carbon.ml.siddhi.extension.PredictStreamProcessor.java

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor,
        StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater) {

    while (streamEventChunk.hasNext()) {

        StreamEvent event = streamEventChunk.next();
        String[] featureValues = new String[attributeIndexMap.size()];

        for (Map.Entry<Integer, int[]> entry : attributeIndexMap.entrySet()) {
            int featureIndex = entry.getKey();
            int[] attributeIndexArray = entry.getValue();
            Object dataValue = null;
            switch (attributeIndexArray[2]) {
            case 0:
                dataValue = event.getBeforeWindowData()[attributeIndexArray[3]];
                break;
            case 2:
                dataValue = event.getOutputData()[attributeIndexArray[3]];
                break;
            }//from   w  ww  . j a v a2s.com
            featureValues[featureIndex] = String.valueOf(dataValue);
        }

        if (featureValues != null) {
            try {
                Object[] predictionResults = new Object[modelHandlers.length];
                Object predictionResult = null;

                if (AlgorithmType.CLASSIFICATION.getValue().equals(algorithmClass)) {
                    for (int i = 0; i < modelHandlers.length; i++) {
                        predictionResults[i] = modelHandlers[i].predict(featureValues, outputType);
                    }
                    // Gets the majority vote
                    predictionResult = ObjectUtils.mode(predictionResults);
                } else if (AlgorithmType.NUMERICAL_PREDICTION.getValue().equals(algorithmClass)) {
                    double sum = 0;
                    for (int i = 0; i < modelHandlers.length; i++) {
                        sum += Double
                                .parseDouble(modelHandlers[i].predict(featureValues, outputType).toString());
                    }
                    // Gets the average value of predictions
                    predictionResult = sum / modelHandlers.length;
                } else if (AlgorithmType.ANOMALY_DETECTION.getValue().equals(algorithmClass)) {
                    for (int i = 0; i < modelHandlers.length; i++) {
                        predictionResults[i] = modelHandlers[i].predict(featureValues, outputType,
                                percentileValue);
                    }
                    // Gets the majority vote
                    predictionResult = ObjectUtils.mode(predictionResults);

                } else if (AlgorithmType.DEEPLEARNING.getValue().equals(algorithmClass)) {
                    // if H2O cluster is not available
                    if (deeplearningWithoutH2O) {
                        for (int i = 0; i < modelHandlers.length; i++) {
                            predictionResults[i] = modelHandlers[i].predict(featureValues, outputType,
                                    pojoPredictor[i]);
                        }
                        // Gets the majority vote
                        predictionResult = ObjectUtils.mode(predictionResults);
                    } else {
                        for (int i = 0; i < modelHandlers.length; i++) {
                            predictionResults[i] = modelHandlers[i].predict(featureValues, outputType);
                        }
                        // Gets the majority vote
                        predictionResult = ObjectUtils.mode(predictionResults);
                    }
                } else {
                    String msg = String.format(
                            "Error while predicting. Prediction is not supported for the algorithm class %s. ",
                            algorithmClass);
                    throw new ExecutionPlanRuntimeException(msg);
                }

                Object[] output = new Object[] { predictionResult };
                complexEventPopulater.populateComplexEvent(event, output);
            } catch (Exception e) {
                log.error("Error while predicting", e);
                throw new ExecutionPlanRuntimeException("Error while predicting", e);
            }
        }
    }
    nextProcessor.process(streamEventChunk);
}