Example usage for weka.classifiers.evaluation Evaluation toMatrixString

List of usage examples for weka.classifiers.evaluation Evaluation toMatrixString

Introduction

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

Prototype

public String toMatrixString() throws Exception 

Source Link

Document

Calls toMatrixString() with a default title.

Usage

From source file:gov.va.chir.tagline.TagLineEvaluator.java

License:Open Source License

public void evaluate(final ClassifierType type, final String... options) throws Exception {
    Classifier model = null;//  www  .  j  a  va  2 s  . co m

    if (type == null) {
        throw new IllegalArgumentException("Classifier type must be specified");
    }

    if (type.equals(ClassifierType.J48)) {
        model = new J48();
    } else if (type.equals(ClassifierType.LMT)) {
        model = new LMT();
    } else if (type.equals(ClassifierType.RandomForest)) {
        model = new RandomForest();
    } else if (type.equals(ClassifierType.SVM)) {
        model = new LibSVM();
    } else {
        throw new IllegalArgumentException(String.format("Classifier type not supported (%s)", type));
    }

    if (model != null) {
        // Set classifier options
        if (options != null && options.length > 0) {
            if (model instanceof AbstractClassifier) {
                ((AbstractClassifier) model).setOptions(options);
            }
        }

        fc.setClassifier(model);

        final Attribute attrDocId = instances.attribute(DatasetUtil.DOC_ID);

        if (attrDocId == null) {
            throw new IllegalStateException(String.format("%s attribute must exist", DatasetUtil.DOC_ID));
        }
        final List<Set<Object>> foldDocIds = getFoldDocIds(attrDocId);

        final RemoveWithValues rmv = new RemoveWithValues();

        // RemoveWithValues filter is not zero-based!
        rmv.setAttributeIndex(String.valueOf(attrDocId.index() + 1));
        rmv.setModifyHeader(false);

        final Evaluation eval = new Evaluation(instances);

        // Perform cross-validation
        for (int i = 0; i < numFolds; i++) {
            rmv.setNominalIndicesArr(getAttributeIndexValues(attrDocId, foldDocIds.get(i)));

            rmv.setInvertSelection(false);
            rmv.setInputFormat(instances); // Must be called AFTER all options
            final Instances train = Filter.useFilter(instances, rmv);

            rmv.setInvertSelection(true);
            rmv.setInputFormat(instances); // Must be called AFTER all options
            final Instances test = Filter.useFilter(instances, rmv);

            fc.buildClassifier(train);
            eval.evaluateModel(fc, test);
        }

        evaluationSummary = String.format("%s%s%s%s%s", eval.toSummaryString(),
                System.getProperty("line.separator"), eval.toMatrixString(),
                System.getProperty("line.separator"), eval.toClassDetailsString());
    }
}