Example usage for weka.classifiers Evaluation Evaluation

List of usage examples for weka.classifiers Evaluation Evaluation

Introduction

In this page you can find the example usage for weka.classifiers Evaluation Evaluation.

Prototype

public Evaluation(Instances data) throws Exception 

Source Link

Usage

From source file:org.conqat.engine.commons.machine_learning.BaseWekaClassifier.java

License:Apache License

/**
 * Evaluates a classifier using 5-fold cross validation and returns the
 * evaluation object. Use this method for debugging purpose to get
 * information about precision, recall, etc.
 *///from  w ww.j a v  a  2 s  . c o m
public Evaluation debugEvaluateClassifier() throws Exception, IOException {
    Instances data = wekaDataSetCreator.getDataSet();
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(wekaClassifier, data, 5, new Random(1));
    return eval;
}

From source file:org.conqat.engine.commons.machine_learning.BaseWekaClassifier.java

License:Apache License

/**
 * Evaluates a classifier using 5-fold cross validation and returns the
 * evaluation object. Use this method for debugging purpose to get
 * information about precision, recall, etc.
 *//* ww w . jav  a  2  s.c o  m*/
public Evaluation debugEvaluateClassifierOnce() throws Exception, IOException {
    Instances data = wekaDataSetCreator.getDataSet();
    Evaluation eval = new Evaluation(data);
    eval.evaluateModel(wekaClassifier, data);
    return eval;
}

From source file:org.dkpro.similarity.algorithms.ml.ClassifierSimilarityMeasure.java

License:Open Source License

public ClassifierSimilarityMeasure(WekaClassifier classifier, File trainArff, File testArff) throws Exception {
    CLASSIFIER = getClassifier(classifier);

    // Get all instances
    Instances train = getTrainInstances(trainArff);
    test = getTestInstances(testArff);/*from  w  ww . j av  a2s .c o  m*/

    // Apply log filter
    Filter logFilter = new LogFilter();
    logFilter.setInputFormat(train);
    train = Filter.useFilter(train, logFilter);
    logFilter.setInputFormat(test);
    test = Filter.useFilter(test, logFilter);

    Classifier clsCopy;
    try {
        // Copy the classifier
        clsCopy = AbstractClassifier.makeCopy(CLASSIFIER);

        // Build the classifier
        filteredClassifier = clsCopy;
        filteredClassifier.buildClassifier(train);

        Evaluation eval = new Evaluation(train);
        eval.evaluateModel(filteredClassifier, test);

        System.out.println(eval.toSummaryString());
        System.out.println(eval.toMatrixString());
    } catch (Exception e) {
        throw new SimilarityException(e);
    }
}

From source file:org.dkpro.similarity.algorithms.ml.LinearRegressionSimilarityMeasure.java

License:Open Source License

public LinearRegressionSimilarityMeasure(File trainArff, File testArff, boolean useLogFilter) throws Exception {
    // Get all instances
    Instances train = getTrainInstances(trainArff);
    test = getTestInstances(testArff);/* w  ww .jav  a 2 s. c o  m*/

    // Apply log filter
    if (useLogFilter) {
        Filter logFilter = new LogFilter();
        logFilter.setInputFormat(train);
        train = Filter.useFilter(train, logFilter);
        logFilter.setInputFormat(test);
        test = Filter.useFilter(test, logFilter);
    }

    Classifier clsCopy;
    try {
        // Copy the classifier
        clsCopy = AbstractClassifier.makeCopy(CLASSIFIER);

        // Build the classifier
        filteredClassifier = clsCopy;
        filteredClassifier.buildClassifier(train);

        Evaluation eval = new Evaluation(train);
        eval.evaluateModel(filteredClassifier, test);

        System.out.println(filteredClassifier.toString());
    } catch (Exception e) {
        throw new SimilarityException(e);
    }
}

From source file:org.openml.webapplication.evaluate.EvaluateBatchPredictions.java

License:Open Source License

private void doEvaluation() throws Exception {
    // set global evaluation
    Evaluation[] e = new Evaluation[bootstrap ? 2 : 1];

    for (int i = 0; i < e.length; ++i) {
        e[i] = new Evaluation(dataset);
        if (cost_matrix != null) {
            // TODO test
            e[i] = new Evaluation(dataset, InstancesHelper.doubleToCostMatrix(cost_matrix));
        } else {/*ww w.j a v a  2s.c  o  m*/
            e[i] = new Evaluation(dataset);
        }
    }

    // set local evaluations
    for (int i = 0; i < sampleEvaluation.length; ++i) {
        for (int j = 0; j < sampleEvaluation[i].length; ++j) {
            for (int k = 0; k < sampleEvaluation[i][j].length; ++k) {
                for (int m = 0; m < (bootstrap ? 2 : 1); ++m) {
                    if (cost_matrix != null) {
                        // TODO test
                        sampleEvaluation[i][j][k][m] = new Evaluation(dataset,
                                InstancesHelper.doubleToCostMatrix(cost_matrix));
                    } else {
                        sampleEvaluation[i][j][k][m] = new Evaluation(dataset);
                    }
                }
            }
        }
    }

    for (int i = 0; i < predictions.numInstances(); i++) {
        Instance prediction = predictions.instance(i);
        int repeat = ATT_PREDICTION_REPEAT < 0 ? 0 : (int) prediction.value(ATT_PREDICTION_REPEAT);
        int fold = ATT_PREDICTION_FOLD < 0 ? 0 : (int) prediction.value(ATT_PREDICTION_FOLD);
        int sample = ATT_PREDICTION_SAMPLE < 0 ? 0 : (int) prediction.value(ATT_PREDICTION_SAMPLE);
        int rowid = (int) prediction.value(ATT_PREDICTION_ROWID);

        predictionCounter.addPrediction(repeat, fold, sample, rowid);
        if (dataset.numInstances() <= rowid) {
            throw new RuntimeException("Making a prediction for row_id" + rowid
                    + " (0-based) while dataset has only " + dataset.numInstances() + " instances. ");
        }

        int bootstrap = 0;
        boolean measureGlobalScore = true;
        if (taskType == TaskType.LEARNINGCURVE && sample != predictionCounter.getSamples() - 1) {
            // for learning curves, we want the score of the last sample at global score
            measureGlobalScore = false;
        }

        if (taskType == TaskType.REGRESSION) {
            if (measureGlobalScore) {
                e[bootstrap].evaluateModelOnce(prediction.value(ATT_PREDICTION_PREDICTION),
                        dataset.instance(rowid));
            }
            sampleEvaluation[repeat][fold][sample][bootstrap]
                    .evaluateModelOnce(prediction.value(ATT_PREDICTION_PREDICTION), dataset.instance(rowid));
        } else {
            // TODO: catch error when no prob distribution is provided
            double[] confidences = InstancesHelper.predictionToConfidences(dataset, prediction,
                    ATT_PREDICTION_CONFIDENCE, ATT_PREDICTION_PREDICTION);

            if (measureGlobalScore) {
                e[bootstrap].evaluateModelOnceAndRecordPrediction(confidences, dataset.instance(rowid));
            }
            sampleEvaluation[repeat][fold][sample][bootstrap].evaluateModelOnceAndRecordPrediction(confidences,
                    dataset.instance(rowid));
        }
    }

    if (predictionCounter.check() == false) {
        throw new RuntimeException("Prediction count does not match: " + predictionCounter.getErrorMessage());
    }

    List<EvaluationScore> evaluationMeasuresList = new ArrayList<EvaluationScore>();
    Map<Metric, MetricScore> globalMeasures = Output.evaluatorToMap(e, nrOfClasses, taskType, bootstrap);
    for (Metric m : globalMeasures.keySet()) {
        MetricScore score = globalMeasures.get(m);
        DecimalFormat dm = MathHelper.defaultDecimalFormat;
        EvaluationScore em = new EvaluationScore(m.implementation, m.name,
                score.getScore() == null ? null : dm.format(score.getScore()), null,
                score.getArrayAsString(dm));
        evaluationMeasuresList.add(em);
    }
    for (int i = 0; i < sampleEvaluation.length; ++i) {
        for (int j = 0; j < sampleEvaluation[i].length; ++j) {
            for (int k = 0; k < sampleEvaluation[i][j].length; ++k) {
                Map<Metric, MetricScore> currentMeasures = Output.evaluatorToMap(sampleEvaluation[i][j][k],
                        nrOfClasses, taskType, bootstrap);
                for (Metric m : currentMeasures.keySet()) {
                    MetricScore score = currentMeasures.get(m);
                    DecimalFormat dm = MathHelper.defaultDecimalFormat;
                    EvaluationScore currentMeasure;
                    if (taskType == TaskType.LEARNINGCURVE) {
                        currentMeasure = new EvaluationScore(m.implementation, m.name,
                                score.getScore() == null ? null : dm.format(score.getScore()),
                                score.getArrayAsString(dm), i, j, k,
                                predictionCounter.getShadowTypeSize(i, j, k));
                    } else {
                        currentMeasure = new EvaluationScore(m.implementation, m.name,
                                score.getScore() == null ? null : dm.format(score.getScore()),
                                score.getArrayAsString(dm), i, j);
                    }
                    evaluationMeasuresList.add(currentMeasure);
                }
            }
        }
    }
    evaluationScores = evaluationMeasuresList.toArray(new EvaluationScore[evaluationMeasuresList.size()]);
}

From source file:org.openml.webapplication.evaluate.EvaluateStreamPredictions.java

License:Open Source License

private void doEvaluation() throws Exception {
    // set global evaluation
    Evaluation globalEvaluator = new Evaluation(datasetStructure);

    // we go through all the instances in one loop. 
    Instance currentInstance;/* w  w w.  ja  v  a  2  s .c  o  m*/
    for (int iInstanceNr = 0; ((currentInstance = datasetLoader
            .getNextInstance(datasetStructure)) != null); ++iInstanceNr) {
        Instance currentPrediction = predictionsLoader.getNextInstance(predictionsStructure);
        if (currentPrediction == null)
            throw new Exception("Could not find prediction for instance #" + iInstanceNr);

        int rowid = (int) currentPrediction.value(ATT_PREDICTION_ROWID);

        if (rowid != iInstanceNr) {
            throw new Exception("Predictions need to be done in the same order as the dataset. "
                    + "Could not find prediction for instance #" + iInstanceNr
                    + ". Found prediction for instance #" + rowid + " instead.");
        }

        double[] confidences = InstancesHelper.predictionToConfidences(datasetStructure, currentPrediction,
                ATT_PREDICTION_CONFIDENCE, ATT_PREDICTION_PREDICTION);
        // TODO: we might want to throw an error if the sum of confidences is not 1.0. Not now though. 
        confidences = InstancesHelper.toProbDist(confidences); // TODO: security, we might be more picky later on and requiring real prob distributions.
        try {
            globalEvaluator.evaluateModelOnceAndRecordPrediction(confidences, currentInstance);
        } catch (ArrayIndexOutOfBoundsException aiobe) {
            throw new Exception(
                    "ArrayIndexOutOfBoundsException: This is an error that occurs when the classifier returns negative values. ");
        }
    }

    globalMeasures = Output.evaluatorToMap(globalEvaluator, nrOfClasses, TaskType.TESTTHENTRAIN);
}

From source file:org.opentox.jaqpot3.qsar.trainer.MlrRegression.java

License:Open Source License

@Override
public Model train(Instances data) throws JaqpotException {
    try {/*w  w w. jav  a 2s. co  m*/

        getTask().getMeta().addComment(
                "Dataset successfully retrieved and converted " + "into a weka.core.Instances object");
        UpdateTask firstTaskUpdater = new UpdateTask(getTask());
        firstTaskUpdater.setUpdateMeta(true);
        firstTaskUpdater.setUpdateTaskStatus(true);//TODO: Is this necessary?
        try {
            firstTaskUpdater.update();
        } catch (DbException ex) {
            throw new JaqpotException(ex);
        } finally {
            try {
                firstTaskUpdater.close();
            } catch (DbException ex) {
                throw new JaqpotException(ex);
            }
        }

        Instances trainingSet = data;
        getTask().getMeta().addComment("The downloaded dataset is now preprocessed");
        firstTaskUpdater = new UpdateTask(getTask());
        firstTaskUpdater.setUpdateMeta(true);
        firstTaskUpdater.setUpdateTaskStatus(true);//TODO: Is this necessary?
        try {
            firstTaskUpdater.update();
        } catch (DbException ex) {
            throw new JaqpotException(ex);
        } finally {
            try {
                firstTaskUpdater.close();
            } catch (DbException ex) {
                throw new JaqpotException(ex);
            }
        }

        /* SET CLASS ATTRIBUTE */
        Attribute target = trainingSet.attribute(targetUri.toString());
        if (target == null) {
            throw new BadParameterException("The prediction feature you provided was not found in the dataset");
        } else {
            if (!target.isNumeric()) {
                throw new QSARException("The prediction feature you provided is not numeric.");
            }
        }
        trainingSet.setClass(target);
        /* Very important: place the target feature at the end! (target = last)*/
        int numAttributes = trainingSet.numAttributes();
        int classIndex = trainingSet.classIndex();
        Instances orderedTrainingSet = null;
        List<String> properOrder = new ArrayList<String>(numAttributes);
        for (int j = 0; j < numAttributes; j++) {
            if (j != classIndex) {
                properOrder.add(trainingSet.attribute(j).name());
            }
        }
        properOrder.add(trainingSet.attribute(classIndex).name());
        try {
            orderedTrainingSet = InstancesUtil.sortByFeatureAttrList(properOrder, trainingSet, -1);
        } catch (JaqpotException ex) {
            logger.error("Improper dataset - training will stop", ex);
            throw ex;
        }
        orderedTrainingSet.setClass(orderedTrainingSet.attribute(targetUri.toString()));

        /* START CONSTRUCTION OF MODEL */
        Model m = new Model(Configuration.getBaseUri().augment("model", getUuid().toString()));
        m.setAlgorithm(getAlgorithm());
        m.setCreatedBy(getTask().getCreatedBy());
        m.setDataset(datasetUri);
        m.addDependentFeatures(dependentFeature);
        try {
            dependentFeature.loadFromRemote();
        } catch (ServiceInvocationException ex) {
            Logger.getLogger(MlrRegression.class.getName()).log(Level.SEVERE, null, ex);
        }

        Set<LiteralValue> depFeatTitles = null;
        if (dependentFeature.getMeta() != null) {
            depFeatTitles = dependentFeature.getMeta().getTitles();
        }

        String depFeatTitle = dependentFeature.getUri().toString();
        if (depFeatTitles != null) {
            depFeatTitle = depFeatTitles.iterator().next().getValueAsString();
            m.getMeta().addTitle("MLR model for " + depFeatTitle)
                    .addDescription("MLR model for the prediction of " + depFeatTitle + " (uri: "
                            + dependentFeature.getUri() + " ).");
        } else {
            m.getMeta().addTitle("MLR model for the prediction of the feature with URI " + depFeatTitle)
                    .addComment("No name was found for the feature " + depFeatTitle);
        }

        /*
         * COMPILE THE LIST OF INDEPENDENT FEATURES with the exact order in which
         * these appear in the Instances object (training set).
         */
        m.setIndependentFeatures(independentFeatures);

        /* CREATE PREDICTED FEATURE AND POST IT TO REMOTE SERVER */
        String predictionFeatureUri = null;
        Feature predictedFeature = publishFeature(m, dependentFeature.getUnits(),
                "Predicted " + depFeatTitle + " by MLR model", datasetUri, featureService);
        m.addPredictedFeatures(predictedFeature);
        predictionFeatureUri = predictedFeature.getUri().toString();

        getTask().getMeta().addComment("Prediction feature " + predictionFeatureUri + " was created.");

        firstTaskUpdater = new UpdateTask(getTask());
        firstTaskUpdater.setUpdateMeta(true);
        firstTaskUpdater.setUpdateTaskStatus(true);//TODO: Is this necessary?
        try {
            firstTaskUpdater.update();
        } catch (DbException ex) {
            throw new JaqpotException(ex);
        } finally {
            try {
                firstTaskUpdater.close();
            } catch (DbException ex) {
                throw new JaqpotException(ex);
            }
        }

        /* ACTUAL TRAINING OF THE MODEL USING WEKA */
        LinearRegression linreg = new LinearRegression();
        String[] linRegOptions = { "-S", "1", "-C" };

        try {
            linreg.setOptions(linRegOptions);
            linreg.buildClassifier(orderedTrainingSet);

        } catch (final Exception ex) {// illegal options or could not build the classifier!
            String message = "MLR Model could not be trained";
            logger.error(message, ex);
            throw new JaqpotException(message, ex);
        }

        try {
            // evaluate classifier and print some statistics
            Evaluation eval = new Evaluation(orderedTrainingSet);
            eval.evaluateModel(linreg, orderedTrainingSet);
            String stats = eval.toSummaryString("\nResults\n======\n", false);

            ActualModel am = new ActualModel(linreg);
            am.setStatistics(stats);
            m.setActualModel(am);
        } catch (NotSerializableException ex) {
            String message = "Model is not serializable";
            logger.error(message, ex);
            throw new JaqpotException(message, ex);
        } catch (final Exception ex) {// illegal options or could not build the classifier!
            String message = "MLR Model could not be trained";
            logger.error(message, ex);
            throw new JaqpotException(message, ex);
        }

        m.getMeta().addPublisher("OpenTox").addComment("This is a Multiple Linear Regression Model");

        //save the instances being predicted to abstract trainer for calculating DoA
        predictedInstances = orderedTrainingSet;
        excludeAttributesDoA.add(dependentFeature.getUri().toString());

        return m;
    } catch (QSARException ex) {
        String message = "QSAR Exception: cannot train MLR model";
        logger.error(message, ex);
        throw new JaqpotException(message, ex);
    }
}

From source file:org.opentox.jaqpot3.qsar.trainer.PLSTrainer.java

License:Open Source License

@Override
public Model train(Instances data) throws JaqpotException {
    Model model = new Model(Configuration.getBaseUri().augment("model", getUuid().toString()));

    data.setClass(data.attribute(targetUri.toString()));

    Boolean targetURIIncluded = false;
    for (Feature tempFeature : independentFeatures) {
        if (StringUtils.equals(tempFeature.getUri().toString(), targetUri.toString())) {
            targetURIIncluded = true;/*  w  ww  .  j  av  a  2s.  c  o  m*/
            break;
        }
    }
    if (!targetURIIncluded) {
        independentFeatures.add(new Feature(targetUri));
    }
    model.setIndependentFeatures(independentFeatures);

    /*
     * Train the PLS filter
     */
    PLSFilter pls = new PLSFilter();
    try {
        pls.setInputFormat(data);
        pls.setOptions(new String[] { "-C", Integer.toString(numComponents), "-A", pls_algorithm, "-P",
                preprocessing, "-U", doUpdateClass });
        PLSFilter.useFilter(data, pls);
    } catch (Exception ex) {
        Logger.getLogger(PLSTrainer.class.getName()).log(Level.SEVERE, null, ex);
    }

    PLSModel actualModel = new PLSModel(pls);
    try {

        PLSClassifier cls = new PLSClassifier();
        cls.setFilter(pls);
        cls.buildClassifier(data);

        // evaluate classifier and print some statistics
        Evaluation eval = new Evaluation(data);
        eval.evaluateModel(cls, data);
        String stats = eval.toSummaryString("", false);

        ActualModel am = new ActualModel(actualModel);
        am.setStatistics(stats);

        model.setActualModel(am);
    } catch (NotSerializableException ex) {
        Logger.getLogger(PLSTrainer.class.getName()).log(Level.SEVERE, null, ex);
        throw new JaqpotException(ex);
    } catch (Exception ex) {
        Logger.getLogger(PLSTrainer.class.getName()).log(Level.SEVERE, null, ex);
        throw new JaqpotException(ex);
    }

    model.setDataset(datasetUri);
    model.setAlgorithm(Algorithms.plsFilter());
    model.getMeta().addTitle("PLS Model for " + datasetUri);

    Set<Parameter> parameters = new HashSet<Parameter>();
    Parameter targetPrm = new Parameter(Configuration.getBaseUri().augment("parameter", RANDOM.nextLong()),
            "target", new LiteralValue(targetUri.toString(), XSDDatatype.XSDstring))
                    .setScope(Parameter.ParameterScope.MANDATORY);
    Parameter nComponentsPrm = new Parameter(Configuration.getBaseUri().augment("parameter", RANDOM.nextLong()),
            "numComponents", new LiteralValue(numComponents, XSDDatatype.XSDpositiveInteger))
                    .setScope(Parameter.ParameterScope.MANDATORY);
    Parameter preprocessingPrm = new Parameter(
            Configuration.getBaseUri().augment("parameter", RANDOM.nextLong()), "preprocessing",
            new LiteralValue(preprocessing, XSDDatatype.XSDstring)).setScope(Parameter.ParameterScope.OPTIONAL);
    Parameter algorithmPrm = new Parameter(Configuration.getBaseUri().augment("parameter", RANDOM.nextLong()),
            "algorithm", new LiteralValue(pls_algorithm, XSDDatatype.XSDstring))
                    .setScope(Parameter.ParameterScope.OPTIONAL);
    Parameter doUpdatePrm = new Parameter(Configuration.getBaseUri().augment("parameter", RANDOM.nextLong()),
            "doUpdateClass", new LiteralValue(doUpdateClass, XSDDatatype.XSDboolean))
                    .setScope(Parameter.ParameterScope.OPTIONAL);

    parameters.add(targetPrm);
    parameters.add(nComponentsPrm);
    parameters.add(preprocessingPrm);
    parameters.add(doUpdatePrm);
    parameters.add(algorithmPrm);
    model.setParameters(parameters);

    for (int i = 0; i < numComponents; i++) {
        Feature f = publishFeature(model, "", "PLS-" + i, datasetUri, featureService);
        model.addPredictedFeatures(f);
    }

    //save the instances being predicted to abstract trainer for calculating DoA
    predictedInstances = data;
    //in pls target is not excluded

    return model;
}

From source file:org.opentox.jaqpot3.qsar.trainer.SvmRegression.java

License:Open Source License

@Override
public Model train(Instances data) throws JaqpotException {
    try {//ww w. j a  v a  2s. c  om
        Attribute target = data.attribute(targetUri.toString());
        if (target == null) {
            throw new QSARException("The prediction feature you provided was not found in the dataset");
        } else {
            if (!target.isNumeric()) {
                throw new QSARException("The prediction feature you provided is not numeric.");
            }
        }
        data.setClass(target);
        //data.deleteAttributeAt(0);//remove the first attribute, i.e. 'compound_uri' or 'URI'
        /* Very important: place the target feature at the end! (target = last)*/
        int numAttributes = data.numAttributes();
        int classIndex = data.classIndex();
        Instances orderedTrainingSet = null;
        List<String> properOrder = new ArrayList<String>(numAttributes);
        for (int j = 0; j < numAttributes; j++) {
            if (j != classIndex) {
                properOrder.add(data.attribute(j).name());
            }
        }
        properOrder.add(data.attribute(classIndex).name());
        try {
            orderedTrainingSet = InstancesUtil.sortByFeatureAttrList(properOrder, data, -1);
        } catch (JaqpotException ex) {
            logger.error(null, ex);
        }
        orderedTrainingSet.setClass(orderedTrainingSet.attribute(targetUri.toString()));

        getTask().getMeta()
                .addComment("Dataset successfully retrieved and converted into a weka.core.Instances object");
        UpdateTask firstTaskUpdater = new UpdateTask(getTask());
        firstTaskUpdater.setUpdateMeta(true);
        firstTaskUpdater.setUpdateTaskStatus(true);//TODO: Is this necessary?
        try {
            firstTaskUpdater.update();
        } catch (DbException ex) {
            throw new JaqpotException(ex);
        } finally {
            try {
                firstTaskUpdater.close();
            } catch (DbException ex) {
                throw new JaqpotException(ex);
            }
        }

        Model m = new Model(Configuration.getBaseUri().augment("model", getUuid().toString()));

        // INITIALIZE THE REGRESSOR regressor
        SVMreg regressor = new SVMreg();
        final String[] regressorOptions = { "-P", Double.toString(epsilon), "-T", Double.toString(tolerance) };
        Kernel svm_kernel = null;
        if (kernel.equalsIgnoreCase("rbf")) {
            RBFKernel rbf_kernel = new RBFKernel();
            rbf_kernel.setGamma(Double.parseDouble(Double.toString(gamma)));
            rbf_kernel.setCacheSize(Integer.parseInt(Integer.toString(cacheSize)));
            svm_kernel = rbf_kernel;
        } else if (kernel.equalsIgnoreCase("polynomial")) {
            PolyKernel poly_kernel = new PolyKernel();
            poly_kernel.setExponent(Double.parseDouble(Integer.toString(degree)));
            poly_kernel.setCacheSize(Integer.parseInt(Integer.toString(cacheSize)));
            poly_kernel.setUseLowerOrder(true);
            svm_kernel = poly_kernel;
        } else if (kernel.equalsIgnoreCase("linear")) {
            PolyKernel poly_kernel = new PolyKernel();
            poly_kernel.setExponent((double) 1.0);
            poly_kernel.setCacheSize(Integer.parseInt(Integer.toString(cacheSize)));
            poly_kernel.setUseLowerOrder(true);
            svm_kernel = poly_kernel;
        }

        try {
            regressor.setOptions(regressorOptions);
        } catch (final Exception ex) {
            throw new QSARException("Bad options in SVM trainer for epsilon = {" + epsilon + "} or "
                    + "tolerance = {" + tolerance + "}.", ex);
        }
        regressor.setKernel(svm_kernel);
        // START TRAINING & CREATE MODEL
        try {
            regressor.buildClassifier(orderedTrainingSet);

            // evaluate classifier and print some statistics
            Evaluation eval = new Evaluation(orderedTrainingSet);
            eval.evaluateModel(regressor, orderedTrainingSet);
            String stats = eval.toSummaryString("", false);

            ActualModel am = new ActualModel(regressor);
            am.setStatistics(stats);
            m.setActualModel(am);
            // m.setStatistics(stats);
        } catch (NotSerializableException ex) {
            String message = "Model is not serializable";
            logger.error(message, ex);
            throw new JaqpotException(message, ex);
        } catch (final Exception ex) {
            throw new QSARException("Unexpected condition while trying to train "
                    + "the model. Possible explanation : {" + ex.getMessage() + "}", ex);
        }

        m.setAlgorithm(getAlgorithm());
        m.setCreatedBy(getTask().getCreatedBy());
        m.setDataset(datasetUri);
        m.addDependentFeatures(dependentFeature);
        try {
            dependentFeature.loadFromRemote();
        } catch (ServiceInvocationException ex) {
            java.util.logging.Logger.getLogger(SvmRegression.class.getName()).log(Level.SEVERE, null, ex);
        }
        m.addDependentFeatures(dependentFeature);

        m.setIndependentFeatures(independentFeatures);

        String predictionFeatureUri = null;
        Feature predictedFeature = publishFeature(m, dependentFeature.getUnits(),
                "Feature created as prediction feature for SVM model " + m.getUri(), datasetUri,
                featureService);
        m.addPredictedFeatures(predictedFeature);
        predictionFeatureUri = predictedFeature.getUri().toString();

        getTask().getMeta().addComment("Prediction feature " + predictionFeatureUri + " was created.");

        /* SET PARAMETERS FOR THE TRAINED MODEL */
        m.setParameters(new HashSet<Parameter>());
        Parameter<String> kernelParam = new Parameter("kernel", new LiteralValue<String>(kernel))
                .setScope(Parameter.ParameterScope.OPTIONAL);
        kernelParam.setUri(Services.anonymous().augment("parameter", RANDOM.nextLong()));
        Parameter<Double> costParam = new Parameter("cost", new LiteralValue<Double>(cost))
                .setScope(Parameter.ParameterScope.OPTIONAL);
        costParam.setUri(Services.anonymous().augment("parameter", RANDOM.nextLong()));
        Parameter<Double> gammaParam = new Parameter("gamma", new LiteralValue<Double>(gamma))
                .setScope(Parameter.ParameterScope.OPTIONAL);
        gammaParam.setUri(Services.anonymous().augment("parameter", RANDOM.nextLong()));
        Parameter<Double> epsilonParam = new Parameter("espilon", new LiteralValue<Double>(epsilon))
                .setScope(Parameter.ParameterScope.OPTIONAL);
        epsilonParam.setUri(Services.anonymous().augment("parameter", RANDOM.nextLong()));
        Parameter<Integer> degreeParam = new Parameter("degree", new LiteralValue<Integer>(degree))
                .setScope(Parameter.ParameterScope.OPTIONAL);
        degreeParam.setUri(Services.anonymous().augment("parameter", RANDOM.nextLong()));
        Parameter<Double> toleranceParam = new Parameter("tolerance", new LiteralValue<Double>(tolerance))
                .setScope(Parameter.ParameterScope.OPTIONAL);
        toleranceParam.setUri(Services.anonymous().augment("parameter", RANDOM.nextLong()));

        m.getParameters().add(kernelParam);
        m.getParameters().add(costParam);
        m.getParameters().add(gammaParam);
        m.getParameters().add(epsilonParam);
        m.getParameters().add(degreeParam);
        m.getParameters().add(toleranceParam);

        //save the instances being predicted to abstract trainer for calculating DoA
        predictedInstances = orderedTrainingSet;
        excludeAttributesDoA.add(dependentFeature.getUri().toString());

        return m;
    } catch (QSARException ex) {
        logger.debug(null, ex);
        throw new JaqpotException(ex);
    }
}

From source file:org.processmining.analysis.clusteranalysis.DecisionAnalyzer.java

License:Open Source License

/**
 * Creates an evaluation overview of the built classifier.
 * //from   w  w w.  j a  v  a  2s  . com
 * @return the panel to be displayed as result evaluation view for the
 *         current decision point
 */
protected JPanel createEvaluationVisualization(Instances data) {
    // build text field to display evaluation statistics
    JTextPane statistic = new JTextPane();

    try {
        // build evaluation statistics
        Evaluation evaluation = new Evaluation(data);
        evaluation.evaluateModel(myClassifier, data);
        statistic.setText(evaluation.toSummaryString() + "\n\n" + evaluation.toClassDetailsString() + "\n\n"
                + evaluation.toMatrixString());

    } catch (Exception ex) {
        ex.printStackTrace();
        return createMessagePanel("Error while creating the decision tree evaluation view");
    }

    statistic.setFont(new Font("Courier", Font.PLAIN, 14));
    statistic.setEditable(false);
    statistic.setCaretPosition(0);

    JPanel resultViewPanel = new JPanel();
    resultViewPanel.setLayout(new BoxLayout(resultViewPanel, BoxLayout.PAGE_AXIS));
    resultViewPanel.add(new JScrollPane(statistic));

    return resultViewPanel;
}