Example usage for org.apache.mahout.classifier.df DFUtils elapsedTime

List of usage examples for org.apache.mahout.classifier.df DFUtils elapsedTime

Introduction

In this page you can find the example usage for org.apache.mahout.classifier.df DFUtils elapsedTime.

Prototype

public static String elapsedTime(long milli) 

Source Link

Document

Formats a time interval in milliseconds to a String in the form "hours:minutes:seconds:millis"

Usage

From source file:bigimp.BuildForest.java

License:Apache License

private void buildForest() throws IOException, ClassNotFoundException, InterruptedException {
    // make sure the output path does not exist
    FileSystem ofs = outputPath.getFileSystem(getConf());
    if (ofs.exists(outputPath)) {
        log.error("Output path already exists");
        return;// www  .  j a  va 2  s .  c o  m
    }

    DecisionTreeBuilder treeBuilder = new DecisionTreeBuilder();
    if (m != null) {
        treeBuilder.setM(m);
    }
    treeBuilder.setComplemented(complemented);
    if (minSplitNum != null) {
        treeBuilder.setMinSplitNum(minSplitNum);
    }
    if (minVarianceProportion != null) {
        treeBuilder.setMinVarianceProportion(minVarianceProportion);
    }

    Builder forestBuilder;

    if (isPartial) {
        log.info("Partial Mapred implementation");
        forestBuilder = new PartialBuilder(treeBuilder, dataPath, datasetPath, seed, getConf());
    } else {
        log.info("InMem Mapred implementation");
        forestBuilder = new InMemBuilder(treeBuilder, dataPath, datasetPath, seed, getConf());
    }

    forestBuilder.setOutputDirName(outputPath.getName());

    log.info("Building the forest...");
    long time = System.currentTimeMillis();

    DecisionForest forest = forestBuilder.build(nbTrees);

    time = System.currentTimeMillis() - time;
    log.info("Build Time: {}", DFUtils.elapsedTime(time));
    log.info("Forest num Nodes: {}", forest.nbNodes());
    log.info("Forest mean num Nodes: {}", forest.meanNbNodes());
    log.info("Forest mean max Depth: {}", forest.meanMaxDepth());

    // store the decision forest in the output path
    Path forestPath = new Path(outputPath, "forest.seq");
    log.info("Storing the forest in: {}", forestPath);
    DFUtils.storeWritable(getConf(), forestPath, forest);
}

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

License:Apache License

private void sequential() throws IOException {

    log.info("Loading the forest...");
    MyDecisionForest forest = MyDecisionForest.load(modelPath);

    if (forest == null) {
        log.error("No Decision Forest found!");
        return;/*from   ww  w .j  av  a2s. c o m*/
    }

    // load the dataset
    Dataset dataset = MyDataset.load(datasetPath);
    DataConverter converter = new DataConverter(dataset);

    log.info("Sequential classification...");
    long time = System.currentTimeMillis();

    Random rng = RandomUtils.getRandom();

    //    List<double[]> resList = new ArrayList<double[]>();

    //----------------0711---------------
    ResultAnalyzer analyzer = new ResultAnalyzer(Arrays.asList(dataset.labels()), "unknown");
    //----------------0711---------------

    if (new File(dataPath).isDirectory()) {
        //the input is a directory of files
        testDirectory(outputPath, converter, forest, dataset, /*resList,*/ rng, analyzer);
    } else {
        // the input is one single file
        testFile(dataPath, outputPath, converter, forest, dataset, /*resList,*/ rng, analyzer);
    }

    time = System.currentTimeMillis() - time;
    log.info("Classification Time: {}", DFUtils.elapsedTime(time));
    log.info("{}", analyzer);

    //    if (analyze) {
    //      if (dataset.isNumerical(dataset.getLabelId())) {
    //        RegressionResultAnalyzer regressionAnalyzer = new RegressionResultAnalyzer();
    //        double[][] results = new double[resList.size()][2];
    //        regressionAnalyzer.setInstances(resList.toArray(results));
    //        log.info("{}", regressionAnalyzer);
    //      } else {
    //        ResultAnalyzer analyzer = new ResultAnalyzer(Arrays.asList(dataset.labels()), "unknown");
    //        for (double[] r : resList) {
    //          analyzer.addInstance(dataset.getLabelString(r[0]),
    //            new ClassifierResult(dataset.getLabelString(r[1]), 1.0));
    //        }
    //        log.info("{}", analyzer);
    //      }
    //    }
}

From source file:guipart.view.GUIOverviewController.java

@FXML
void handleClassifyRF(ActionEvent event) throws IOException {

    String outputFile = "data/out";

    Path dataPath = new Path(textFieldCSVRF.getText()); // test data path
    Path datasetPath = new Path(textFieldDatasetRF.getText()); //info file about data set
    Path modelPath = new Path(textFieldModelRF.getText()); // path where the forest is stored
    Path outputPath = new Path(outputFile); // path to predictions file, if null do not output the predictions

    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(conf);

    FileSystem outFS = FileSystem.get(conf);

    System.out.println("Loading the forest");
    DecisionForest forest = DecisionForest.load(conf, modelPath);

    if (forest == null)
        System.err.println("No decision forest found!");

    // load the dataset
    Dataset dataset = Dataset.load(conf, datasetPath);
    DataConverter converter = new DataConverter(dataset);

    System.out.println("Sequential classification");
    long time = System.currentTimeMillis();

    Random rng = RandomUtils.getRandom();

    List<double[]> resList = Lists.newArrayList();
    if (fs.getFileStatus(dataPath).isDir()) {
        //the input is a directory of files
        Utils.rfTestDirectory(outputPath, converter, forest, dataset, resList, rng, fs, dataPath, outFS,
                guiPart);/*from www  .jav  a 2  s . co  m*/
    } else {
        // the input is one single file
        Utils.rfTestFile(dataPath, outputPath, converter, forest, dataset, resList, rng, outFS, fs, guiPart);
    }

    time = System.currentTimeMillis() - time;
    //log.info("Classification Time: {}", DFUtils.elapsedTime(time));
    System.out.println("Classification time: " + DFUtils.elapsedTime(time));

    if (dataset.isNumerical(dataset.getLabelId())) {

        RegressionResultAnalyzer regressionAnalyzer = new RegressionResultAnalyzer();
        double[][] results = new double[resList.size()][2];
        regressionAnalyzer.setInstances(resList.toArray(results));
        //log.info("{}", regressionAnalyzer);
        System.out.println(regressionAnalyzer.toString());

    } else {
        ResultAnalyzer analyzer = new ResultAnalyzer(Arrays.asList(dataset.labels()), "unknown");
        for (double[] r : resList) {
            analyzer.addInstance(dataset.getLabelString(r[0]),
                    new ClassifierResult(dataset.getLabelString(r[1]), 1.0));
        }
        //log.info("{}", analyzer);
        System.out.println(analyzer.toString());
        textAnalyze.setText(analyzer.toString());
    }

}

From source file:imageClassify.TestForest.java

License:Apache License

private void sequential() throws IOException {

    log.info("Loading the forest...");
    DecisionForest forest = DecisionForest.load(getConf(), modelPath);

    if (forest == null) {
        log.error("No Decision Forest found!");
        return;/*w  w  w  .jav a 2s. c  om*/
    }

    // load the dataset
    Dataset dataset = Dataset.load(getConf(), datasetPath);
    DataConverter converter = new DataConverter(dataset);

    log.info("Sequential classification...");
    long time = System.currentTimeMillis();

    Random rng = RandomUtils.getRandom();

    List<double[]> resList = Lists.newArrayList();
    if (dataFS.getFileStatus(dataPath).isDir()) {
        //the input is a directory of files
        testDirectory(outputPath, converter, forest, dataset, resList, rng);
    } else {
        // the input is one single file
        testFile(dataPath, outputPath, converter, forest, dataset, resList, rng);
    }

    time = System.currentTimeMillis() - time;
    log.info("Classification Time: {}", DFUtils.elapsedTime(time));

    if (analyze) {
        if (dataset.isNumerical(dataset.getLabelId())) {
            RegressionResultAnalyzer regressionAnalyzer = new RegressionResultAnalyzer();
            double[][] results = new double[resList.size()][2];
            regressionAnalyzer.setInstances(resList.toArray(results));
            log.info("{}", regressionAnalyzer);
        } else {
            ResultAnalyzer analyzer = new ResultAnalyzer(Arrays.asList(dataset.labels()), "unknown");
            for (double[] r : resList) {
                analyzer.addInstance(dataset.getLabelString(r[0]),
                        new ClassifierResult(dataset.getLabelString(r[1]), 1.0));
            }
            log.info("{}", analyzer);
        }
    }
}

From source file:javaapplication3.RunRandomForestSeq.java

public static void main(String[] args) throws IOException {

    String outputFile = "data/out";
    String inputFile = "data/DataFraud1MTest.csv";
    String modelFile = "data/forest.seq";
    String infoFile = "data/DataFraud1M.info";

    Path dataPath = new Path(inputFile); // test data path
    Path datasetPath = new Path(infoFile);
    Path modelPath = new Path(modelFile); // path where the forest is stored
    Path outputPath = new Path(outputFile); // path to predictions file, if null do not output the predictions

    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(conf);

    FileSystem outFS = FileSystem.get(conf);

    //log.info("Loading the forest...");
    System.out.println("Loading the forest");
    DecisionForest forest = DecisionForest.load(conf, modelPath);

    if (forest == null)
        System.err.println("No decision forest found!");
    //log.error("No Decision Forest found!");

    // load the dataset
    Dataset dataset = Dataset.load(conf, datasetPath);
    DataConverter converter = new DataConverter(dataset);

    //log.info("Sequential classification...");
    System.out.println("Sequential classification");
    long time = System.currentTimeMillis();

    Random rng = RandomUtils.getRandom();

    List<double[]> resList = Lists.newArrayList();
    if (fs.getFileStatus(dataPath).isDir()) {
        //the input is a directory of files
        testDirectory(outputPath, converter, forest, dataset, resList, rng, fs, dataPath, outFS);
    } else {/*from  ww w .  j a  v  a2s .co  m*/
        // the input is one single file
        testFile(dataPath, outputPath, converter, forest, dataset, resList, rng, outFS, fs);
    }

    time = System.currentTimeMillis() - time;
    //log.info("Classification Time: {}", DFUtils.elapsedTime(time));
    System.out.println("Classification time: " + DFUtils.elapsedTime(time));

    if (dataset.isNumerical(dataset.getLabelId())) {

        RegressionResultAnalyzer regressionAnalyzer = new RegressionResultAnalyzer();
        double[][] results = new double[resList.size()][2];
        regressionAnalyzer.setInstances(resList.toArray(results));
        //log.info("{}", regressionAnalyzer);
        System.out.println(regressionAnalyzer.toString());

    } else {
        ResultAnalyzer analyzer = new ResultAnalyzer(Arrays.asList(dataset.labels()), "unknown");
        for (double[] r : resList) {
            analyzer.addInstance(dataset.getLabelString(r[0]),
                    new ClassifierResult(dataset.getLabelString(r[1]), 1.0));
        }
        //log.info("{}", analyzer);
        System.out.println(analyzer.toString());
    }

}