Example usage for weka.classifiers.trees.j48 Distribution matrix

List of usage examples for weka.classifiers.trees.j48 Distribution matrix

Introduction

In this page you can find the example usage for weka.classifiers.trees.j48 Distribution matrix.

Prototype

public final double[][] matrix() 

Source Link

Document

Returns matrix with distribution of class values.

Usage

From source file:cs.man.ac.uk.classifiers.GetAUC.java

License:Open Source License

/**
 * Computes the AUC for the supplied stream learner.
 * @return the AUC as a double value.//from   w  w  w. j a  v  a  2  s.  co  m
 */
private static double validate5x2CVStream() {
    try {
        // Other options
        int runs = 5;
        int folds = 2;
        double AUC_SUM = 0;

        // perform cross-validation
        for (int i = 0; i < runs; i++) {
            // randomize data
            int seed = i + 1;
            Random rand = new Random(seed);
            Instances randData = new Instances(data);
            randData.randomize(rand);

            if (randData.classAttribute().isNominal()) {
                System.out.println("Stratifying...");
                randData.stratify(folds);
            }

            for (int n = 0; n < folds; n++) {
                Instances train = randData.trainCV(folds, n);
                Instances test = randData.testCV(folds, n);

                Distribution testDistribution = new Distribution(test);

                ArffSaver trainSaver = new ArffSaver();
                trainSaver.setInstances(train);
                trainSaver.setFile(new File(trainPath));
                trainSaver.writeBatch();

                ArffSaver testSaver = new ArffSaver();
                testSaver.setInstances(test);

                double[][] dist = testDistribution.matrix();
                int negativeClassSize = (int) dist[0][0];
                int positiveClassSize = (int) dist[0][1];
                double balance = (double) positiveClassSize / (double) negativeClassSize;

                String tempTestPath = testPath.replace(".arff",
                        "_" + positiveClassSize + "_" + negativeClassSize + "_" + balance + "_1.0.arff");// [Test-n-Set-n]_[+]_[-]_[K]_[L];
                testSaver.setFile(new File(tempTestPath));
                testSaver.writeBatch();

                ARFFFile file = new ARFFFile(tempTestPath, CLASS_INDEX, new DebugLogger(false));
                file.createMetaData();

                HoeffdingTreeTester streamClassifier = new HoeffdingTreeTester(trainPath, tempTestPath,
                        CLASS_INDEX, new String[] { "0", "1" }, new DebugLogger(true));

                streamClassifier.train();

                System.in.read();

                //AUC_SUM += streamClassifier.getROCExternalData("",(int)testDistribution.perClass(1),(int)testDistribution.perClass(0));
                streamClassifier.testStatic(homeDirectory + "/FuckSakeTest.txt");

                String[] files = Common.getFilePaths(scratch);
                for (int j = 0; j < files.length; j++)
                    Common.fileDelete(files[j]);
            }
        }

        return AUC_SUM / ((double) runs * (double) folds);
    } catch (Exception e) {
        System.out.println("Exception validating data!");
        e.printStackTrace();
        return 0;
    }
}

From source file:org.knime.knip.suise.node.port.WekaClassifierPortObject.java

License:Open Source License

/**
 * {@inheritDoc}/*  w  ww. j av  a2s .  c  o  m*/
 */
@Override
public JComponent[] getViews() {

    JPanel panel = new JPanel(new GridLayout(1, 2));

    // classifier info
    JPanel classifierInfoPanel = new JPanel();
    String text = m_classifier.getClass().getSimpleName() + "\n";
    text += m_classifier.toString();
    classifierInfoPanel.add(new JTextArea(text));
    classifierInfoPanel.setBorder(BorderFactory.createTitledBorder("Weka Classifier Details"));
    panel.add(new JScrollPane(classifierInfoPanel));

    // training instances info
    if (m_trainingInstances.numInstances() > 0) {
        JPanel dataInfoPanel = new JPanel();
        text = "Training instances\n\n";
        Distribution distr = new Distribution(1, m_trainingInstances.numClasses());
        for (Instance i : m_trainingInstances) {
            try {
                distr.add(0, i);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        text += "Class-distribution: " + Arrays.toString(distr.matrix()[0]) + " (labels: "
                + Arrays.toString(m_modelspec.getClassLabels()) + ")" + "\n\n";
        text += m_trainingInstances.toSummaryString();

        dataInfoPanel.add(new JTextArea(text));
        dataInfoPanel.setBorder(BorderFactory.createTitledBorder("Training Instances Details"));
        panel.add(new JScrollPane(dataInfoPanel));
    }

    panel.setName("Weka Outport");
    return new JComponent[] { panel };
}