Example usage for org.apache.commons.math3.stat.regression ModelSpecificationException getMessage

List of usage examples for org.apache.commons.math3.stat.regression ModelSpecificationException getMessage

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.regression ModelSpecificationException getMessage.

Prototype

@Override
public String getMessage() 

Source Link

Usage

From source file:org.wso2.carbon.ml.model.spark.algorithms.SupervisedModel.java

/**
 * @param modelID   Model ID//  www.j  a  v a2  s .  com
 * @param workflow  Workflow ID
 * @param sparkConf Spark configuration
 * @throws ModelServiceException
 */
public void buildModel(String modelID, MLWorkflow workflow, SparkConf sparkConf) throws ModelServiceException {
    try {
        sparkConf.setAppName(modelID);
        // create a new java spark context
        JavaSparkContext sc = new JavaSparkContext(sparkConf);
        // parse lines in the dataset
        String datasetURL = workflow.getDatasetURL();
        JavaRDD<String> lines = sc.textFile(datasetURL);
        // get header line
        String headerRow = lines.take(1).get(0);
        // get column separator
        String columnSeparator = MLModelUtils.getColumnSeparator(datasetURL);
        // apply pre processing
        JavaRDD<double[]> features = preProcess(sc, workflow, lines, headerRow, columnSeparator);
        // generate train and test datasets by converting tokens to labeled points
        int responseIndex = MLModelUtils.getFeatureIndex(workflow.getResponseVariable(), headerRow,
                columnSeparator);
        TokensToLabeledPoints tokensToLabeledPoints = new TokensToLabeledPoints(responseIndex);
        JavaRDD<LabeledPoint> labeledPoints = features.map(tokensToLabeledPoints);
        JavaRDD<LabeledPoint> trainingData = labeledPoints.sample(false, workflow.getTrainDataFraction(),
                RANDOM_SEED);
        JavaRDD<LabeledPoint> testingData = labeledPoints.subtract(trainingData);
        // build a machine learning model according to user selected algorithm
        SUPERVISED_ALGORITHM supervisedAlgorithm = SUPERVISED_ALGORITHM.valueOf(workflow.getAlgorithmName());
        switch (supervisedAlgorithm) {
        case LOGISTIC_REGRESSION:
            buildLogisticRegressionModel(modelID, trainingData, testingData, workflow);
            break;
        case DECISION_TREE:
            buildDecisionTreeModel(modelID, trainingData, testingData, workflow);
            break;
        default:
            throw new AlgorithmNameException("Incorrect algorithm name");
        }
        // stop spark context
        sc.stop();
    } catch (ModelSpecificationException e) {
        throw new ModelServiceException(
                "An error occurred while building supervised machine learning model: " + e.getMessage(), e);
    }
}