Example usage for org.apache.mahout.classifier.df.data DataConverter convert

List of usage examples for org.apache.mahout.classifier.df.data DataConverter convert

Introduction

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

Prototype

public Instance convert(CharSequence string) 

Source Link

Usage

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

License:Apache License

/**
 * Loads the data from a file//w ww . j av a2s.  c  o  m
 * 
 * @param fs
 *          file system
 * @param fpath
 *          data file path
 * @throws IOException
 *           if any problem is encountered
 */

public static Data loadData(Dataset dataset, String fpath) throws IOException {
    Scanner scanner = new Scanner(new File(fpath));

    List<Instance> instances = Lists.newArrayList();

    DataConverter converter = new DataConverter(dataset);

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (line.isEmpty()) {
            log.warn("{}: empty string", instances.size());
            continue;
        }

        Instance instance = converter.convert(line);
        if (instance == null) {
            // missing values found
            log.warn("{}: missing values", instances.size());
            continue;
        }

        instances.add(instance);
    }

    scanner.close();

    return new Data(dataset, instances);
}

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

License:Apache License

/**
 * Loads the data from a String array/*w w w . j a v  a2  s . c  o m*/
 */
public static Data loadData(Dataset dataset, String[] data) {
    List<Instance> instances = Lists.newArrayList();

    DataConverter converter = new DataConverter(dataset);

    for (String line : data) {
        if (line.isEmpty()) {
            log.warn("{}: empty string", instances.size());
            continue;
        }

        Instance instance = converter.convert(line);
        if (instance == null) {
            // missing values found
            log.warn("{}: missing values", instances.size());
            continue;
        }

        instances.add(instance);
    }

    return new Data(dataset, instances);
}

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  w  w  .j av a2  s  .  co  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;/*from   ww  w  .j a  va2s .  c  om*/
    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;//w w w  . ja  v a2s  .  co 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;/*from ww w . j  a v  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);
    }
}