Example usage for org.apache.mahout.classifier.df.data Dataset getLabel

List of usage examples for org.apache.mahout.classifier.df.data Dataset getLabel

Introduction

In this page you can find the example usage for org.apache.mahout.classifier.df.data Dataset getLabel.

Prototype

public double getLabel(Instance instance) 

Source Link

Usage

From source file:com.wsc.myexample.decisionForest.MyTestForest.java

License:Apache License

private void testFile(String inPath, String outPath, DataConverter converter, MyDecisionForest forest,
        Dataset dataset, /*List<double[]> results,*/ Random rng, ResultAnalyzer analyzer) throws IOException {
    // create the predictions file
    DataOutputStream ofile = null;

    if (outPath != null) {
        ofile = new DataOutputStream(new FileOutputStream(outPath));
    }/*from  w ww  .ja  va2s  .c  o  m*/

    DataInputStream input = new DataInputStream(new FileInputStream(inPath));
    try {
        Scanner scanner = new Scanner(input);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.isEmpty()) {
                continue; // skip empty lines
            }

            Instance instance = converter.convert(line);
            if (instance == null)
                continue;

            double prediction = forest.classify(dataset, rng, instance);

            if (ofile != null) {
                ofile.writeChars(Double.toString(prediction)); // write the prediction
                ofile.writeChar('\n');
            }

            //        results.add(new double[] {dataset.getLabel(instance), prediction});

            analyzer.addInstance(dataset.getLabelString(dataset.getLabel(instance)),
                    new ClassifierResult(dataset.getLabelString(prediction), 1.0));
        }

        scanner.close();
    } finally {
        Closeables.closeQuietly(input);
        ofile.close();
    }
}

From source file:guipart.view.Utils.java

static void rfTestFile(Path inPath, Path outPath, DataConverter converter, DecisionForest forest,
        Dataset dataset, Collection<double[]> results, Random rng, FileSystem outFS, FileSystem dataFS,
        GUIPart guiPart) throws IOException {
    // create the predictions file
    FSDataOutputStream ofile = null;/*  w  w w.j  av a  2s .  co m*/
    String gender;

    if (outPath != null) {
        ofile = outFS.create(outPath);
    }

    FSDataInputStream input = dataFS.open(inPath);
    try {
        Scanner scanner = new Scanner(input, "UTF-8");

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.isEmpty()) {
                continue; // skip empty lines
            }
            System.out.println(line);

            String[] split = line.split(",");

            if (split[1].contentEquals("1"))
                gender = "male";
            else
                gender = "female";

            Instance instance = converter.convert(line);

            double prediction = forest.classify(dataset, rng, instance);

            boolean booltemp = prediction != 0;

            System.out.println("Preditction: " + prediction);

            if (ofile != null) {
                ofile.writeChars(Double.toString(prediction)); // write the prediction
                ofile.writeChar('\n');
            }

            Person temp = new Person(Integer.parseInt(split[0]), Integer.parseInt(split[4]),
                    Integer.parseInt(split[7]), booltemp, gender, Integer.parseInt(split[5]),
                    Integer.parseInt(split[6]), Integer.parseInt(split[3]));

            guiPart.addPerson(temp);

            results.add(new double[] { dataset.getLabel(instance), prediction });
        }

        scanner.close();
    } finally {
        Closeables.close(input, true);
    }
}

From source file:imageClassify.TestForest.java

License:Apache License

private void testFile(Path inPath, Path outPath, DataConverter converter, DecisionForest forest,
        Dataset dataset, Collection<double[]> results, Random rng) throws IOException {
    // create the predictions file
    FSDataOutputStream ofile = null;//from   w  w w  .  j  ava2  s  .  c  o m

    if (outPath != null) {
        ofile = outFS.create(outPath);
    }

    FSDataInputStream input = dataFS.open(inPath);
    try {
        Scanner scanner = new Scanner(input, "UTF-8");

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.isEmpty()) {
                continue; // skip empty lines
            }

            Instance instance = converter.convert(line);
            double prediction = forest.classify(dataset, rng, instance);

            if (ofile != null) {
                ofile.writeChars(Double.toString(prediction)); // write the prediction
                ofile.writeChar('\n');
            }

            results.add(new double[] { dataset.getLabel(instance), prediction });
        }

        scanner.close();
    } finally {
        Closeables.close(input, true);
    }
}

From source file:javaapplication3.RunRandomForestSeq.java

private static void testFile(Path inPath, Path outPath, DataConverter converter, DecisionForest forest,
        Dataset dataset, Collection<double[]> results, Random rng, FileSystem outFS, FileSystem dataFS)
        throws IOException {
    // create the predictions file
    FSDataOutputStream ofile = null;/*w  w w . j  av a 2 s .c o  m*/

    if (outPath != null) {
        ofile = outFS.create(outPath);
    }

    FSDataInputStream input = dataFS.open(inPath);
    try {
        Scanner scanner = new Scanner(input, "UTF-8");

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.isEmpty()) {
                continue; // skip empty lines
            }

            Instance instance = converter.convert(line);
            double prediction = forest.classify(dataset, rng, instance);

            if (ofile != null) {
                ofile.writeChars(Double.toString(prediction)); // write the prediction
                ofile.writeChar('\n');
            }

            results.add(new double[] { dataset.getLabel(instance), prediction });
        }

        scanner.close();
    } finally {
        Closeables.close(input, true);
    }
}