Package org.apache.mahout.classifier.bayes.common

Examples of org.apache.mahout.classifier.bayes.common.BayesParameters


  }
 
  public static void main(String[] args) throws IOException {
    // test harness, delete me
    BayesFeatureDriver driver = new BayesFeatureDriver();
    BayesParameters p = new BayesParameters(1);
    Path input = new Path("/home/drew/mahout/bayes/20news-input");
    Path output = new Path("/home/drew/mahout/bayes/20-news-features");
    driver.runJob(input, output, p);
  }
View Full Code Here


      if (cmdLine.hasOption(helpOpt)) {
        CommandLineUtil.printHelp(group);
        return;
      }
     
      BayesParameters params = new BayesParameters();
      // Setting all default values
      int gramSize = 1;
      String classifierType = "bayes";     
      String dataSource = "hdfs";
      String defaultCat = "unknown";
      String encoding = "UTF-8";
      String alphaI = "1.0";
      String classificationMethod = "sequential";

      String modelBasePath = (String) cmdLine.getValue(pathOpt);
     
      if (cmdLine.hasOption(gramSizeOpt)) {
        gramSize = Integer.parseInt((String) cmdLine.getValue(gramSizeOpt));
       
      }
     
      if (cmdLine.hasOption(classifierType)) {
        classifierType = (String) cmdLine.getValue(typeOpt);
      }
     
      if (cmdLine.hasOption(dataSource)) {
        dataSource = (String) cmdLine.getValue(dataSource);
      }
     
      if (cmdLine.hasOption(defaultCatOpt)) {
        defaultCat = (String) cmdLine.getValue(defaultCatOpt);
      }
     
      if (cmdLine.hasOption(encodingOpt)) {
        encoding = (String) cmdLine.getValue(encodingOpt);
      }
     
      if (cmdLine.hasOption(alphaOpt)) {
        alphaI = (String) cmdLine.getValue(alphaOpt);
      }
     
      boolean verbose = cmdLine.hasOption(verboseOutputOpt);
     
      String testDirPath = (String) cmdLine.getValue(dirOpt);
     
      if (cmdLine.hasOption(methodOpt)) {
        classificationMethod = (String) cmdLine.getValue(methodOpt);
      }
     
      params.setGramSize(gramSize);
      params.set("verbose", Boolean.toString(verbose));
      params.set("basePath", modelBasePath);
      params.set("classifierType", classifierType);
      params.set("dataSource", dataSource);
      params.set("defaultCat", defaultCat);
      params.set("encoding", encoding);
      params.set("alpha_i", alphaI);
      params.set("testDirPath", testDirPath);
     
      if (classificationMethod.equalsIgnoreCase("sequential")) {
        classifySequential(params);
      } else if (classificationMethod.equalsIgnoreCase("mapreduce")) {
        classifyParallel(params);
View Full Code Here

    fs.copyFromLocalFile(new Path(tempInputFile.getAbsolutePath()), input);
  }

  @Test
  public void testSelfTestBayes() throws Exception {
    BayesParameters params = new BayesParameters(1);
    params.set("alpha_i", "1.0");
    params.set("dataSource", "hdfs");
    Path bayesInputPath = getTestTempFilePath("bayesinput");
    Path bayesModelPath = getTestTempDirPath("bayesmodel");
    TrainClassifier.trainNaiveBayes(bayesInputPath, bayesModelPath, params);
   
    params.set("verbose", "true");
    params.set("basePath", bayesModelPath.toString());
    params.set("classifierType", "bayes");
    params.set("dataSource", "hdfs");
    params.set("defaultCat", "unknown");
    params.set("encoding", "UTF-8");
    params.set("alpha_i", "1.0");
   
    Algorithm algorithm = new BayesAlgorithm();
    Datastore datastore = new InMemoryBayesDatastore(params);
    ClassifierContext classifier = new ClassifierContext(algorithm, datastore);
    classifier.initialize();
    ResultAnalyzer resultAnalyzer = new ResultAnalyzer(classifier.getLabels(), params.get("defaultCat"));
   
    for (String[] entry : ClassifierData.DATA) {
      List<String> document = new NGrams(entry[1], Integer.parseInt(params.get("gramSize")))
          .generateNGramsWithoutLabel();
      assertEquals(3, classifier.classifyDocument(document.toArray(new String[document.size()]),
        params.get("defaultCat"), 100).length);
      ClassifierResult result = classifier.classifyDocument(document.toArray(new String[document.size()]), params
          .get("defaultCat"));
      assertEquals(entry[0], result.getLabel());
      resultAnalyzer.addInstance(entry[0], result);
    }
    int[][] matrix = resultAnalyzer.getConfusionMatrix().getConfusionMatrix();
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        assertEquals(i == j ? 4 : 0, matrix[i][j]);
      }
    }
    params.set("testDirPath", bayesInputPath.toString());
    TestClassifier.classifyParallel(params);
    Configuration conf = new Configuration();
    Path outputFiles = getTestTempFilePath("bayesinput-output/part*");
    FileSystem fs = FileSystem.get(outputFiles.toUri(), conf);
    matrix = BayesClassifierDriver.readResult(fs, outputFiles, conf, params).getConfusionMatrix();
View Full Code Here

    }
  }

  @Test
  public void testSelfTestCBayes() throws Exception {
    BayesParameters params = new BayesParameters(1);
    params.set("alpha_i", "1.0");
    params.set("dataSource", "hdfs");
    Path bayesInputPath = getTestTempFilePath("bayesinput");
    Path bayesModelPath = getTestTempDirPath("cbayesmodel");
    TrainClassifier.trainCNaiveBayes(bayesInputPath, bayesModelPath, params);
   
    params.set("verbose", "true");
    params.set("basePath", bayesModelPath.toString());
    params.set("classifierType", "cbayes");
    params.set("dataSource", "hdfs");
    params.set("defaultCat", "unknown");
    params.set("encoding", "UTF-8");
    params.set("alpha_i", "1.0");
   
    Algorithm algorithm = new CBayesAlgorithm();
    Datastore datastore = new InMemoryBayesDatastore(params);
    ClassifierContext classifier = new ClassifierContext(algorithm, datastore);
    classifier.initialize();
    ResultAnalyzer resultAnalyzer = new ResultAnalyzer(classifier.getLabels(), params.get("defaultCat"));
    for (String[] entry : ClassifierData.DATA) {
      List<String> document = new NGrams(entry[1], Integer.parseInt(params.get("gramSize")))
          .generateNGramsWithoutLabel();
      assertEquals(3, classifier.classifyDocument(document.toArray(new String[document.size()]),
        params.get("defaultCat"), 100).length);
      ClassifierResult result = classifier.classifyDocument(document.toArray(new String[document.size()]), params
          .get("defaultCat"));
      assertEquals(entry[0], result.getLabel());
      resultAnalyzer.addInstance(entry[0], result);
    }
    int[][] matrix = resultAnalyzer.getConfusionMatrix().getConfusionMatrix();
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        assertEquals(i == j ? 4 : 0, matrix[i][j]);
      }
    }
    params.set("testDirPath", bayesInputPath.toString());
    TestClassifier.classifyParallel(params);
    Configuration conf = new Configuration();
    Path outputFiles = getTestTempFilePath("bayesinput-output/part*");
    FileSystem fs = FileSystem.get(outputFiles.toUri(), conf);
    matrix = BayesClassifierDriver.readResult(fs, outputFiles, conf, params).getConfusionMatrix();
View Full Code Here

    return reducerOutput;
  }

  @Test
  public void testNoFilters() throws Exception {
    BayesParameters bp = new BayesParameters();
    bp.setGramSize(1);
    bp.setMinDF(1);
    DummyOutputCollector<StringTuple,DoubleWritable> reduceOutput = runMapReduce(bp);

    assertCounts(reduceOutput,
        17, /* df: 13 unique term/label pairs */
        14, /* fc: 12 unique features across all labels */
 
View Full Code Here

        17  /* wt: 13 unique term/label pairs */);
  }

  @Test
  public void testMinSupport() throws Exception {
    BayesParameters bp = new BayesParameters();
    bp.setGramSize(1);
    bp.setMinSupport(2);
    DummyOutputCollector<StringTuple,DoubleWritable> reduceOutput = runMapReduce(bp);
   
    assertCounts(reduceOutput,
        5, /* df: 5 unique term/label pairs */
        2, /* fc: 'big' and 'cool' appears more than 2 times */
 
View Full Code Here

   
  }

  @Test
  public void testMinDf() throws Exception {
    BayesParameters bp = new BayesParameters();
    bp.setGramSize(1);
    bp.setMinDF(2);
    DummyOutputCollector<StringTuple,DoubleWritable> reduceOutput = runMapReduce(bp);
   
    // 13 unique term/label pairs. 3 labels
    // should be a df and fc for each pair, no filtering
    assertCounts(reduceOutput,
View Full Code Here

   
  }

  @Test
  public void testMinBoth() throws Exception {
    BayesParameters bp = new BayesParameters();
    bp.setGramSize(1);
    bp.setMinSupport(3);
    bp.setMinDF(2);
    DummyOutputCollector<StringTuple,DoubleWritable> reduceOutput = runMapReduce(bp);
   
    // 13 unique term/label pairs. 3 labels
    // should be a df and fc for each pair, no filtering
    assertCounts(reduceOutput,
View Full Code Here

      }
     
      String classifierType = (String) cmdLine.getValue(typeOpt);
      String dataSourceType = (String) cmdLine.getValue(dataSourceOpt);
     
      BayesParameters params = new BayesParameters();
      // Setting all the default parameter values
      params.setGramSize(1);
      params.setMinDF(1);
      params.set("alpha_i","1.0");
      params.set("dataSource", "hdfs");
     
      if (cmdLine.hasOption(gramSizeOpt)) {
        params.setGramSize(Integer.parseInt((String) cmdLine.getValue(gramSizeOpt)));
      }
     
      if (cmdLine.hasOption(minDfOpt)) {
        params.setMinDF(Integer.parseInt((String) cmdLine.getValue(minDfOpt)));
      }
     
      if (cmdLine.hasOption(minSupportOpt)) {
        params.setMinSupport(Integer.parseInt((String) cmdLine.getValue(minSupportOpt)));
      }
     
      if (cmdLine.hasOption(skipCleanupOpt)) {
        params.setSkipCleanup(true);
      }
     
      if (cmdLine.hasOption(alphaOpt)) {
        params.set("alpha_i",(String) cmdLine.getValue(alphaOpt));
      }
     
      if (cmdLine.hasOption(dataSourceOpt)) {
        params.set("dataSource", dataSourceType);
      }

      Path inputPath = new Path((String) cmdLine.getValue(inputDirOpt));
      Path outputPath = new Path((String) cmdLine.getValue(outputOpt));
      if ("cbayes".equalsIgnoreCase(classifierType)) {
View Full Code Here

    if (cmdLine.hasOption(gramSizeOpt)) {
      gramSize = Integer.parseInt((String) cmdLine.getValue(gramSizeOpt));
     
    }
   
    BayesParameters params = new BayesParameters(gramSize);
   
    String modelBasePath = (String) cmdLine.getValue(pathOpt);
   
    log.info("Loading model from: {}", params.print());
   
    Algorithm algorithm;
    Datastore datastore;
   
    String classifierType = (String) cmdLine.getValue(typeOpt);
View Full Code Here

TOP

Related Classes of org.apache.mahout.classifier.bayes.common.BayesParameters

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.