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

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


      int gramSize = 1;
      if (cmdLine.hasOption(gramSizeOpt)) {
        gramSize = Integer.parseInt((String) cmdLine.getValue(gramSizeOpt));
       
      }
      BayesParameters params = new BayesParameters(gramSize);
     
      String modelBasePath = (String) cmdLine.getValue(pathOpt);
     
      String classifierType = (String) cmdLine.getValue(typeOpt);
      String dataSource = (String) cmdLine.getValue(dataSourceOpt);
     
      String defaultCat = "unknown";
      if (cmdLine.hasOption(defaultCatOpt)) {
        defaultCat = (String) cmdLine.getValue(defaultCatOpt);
      }
     
      String encoding = "UTF-8";
      if (cmdLine.hasOption(encodingOpt)) {
        encoding = (String) cmdLine.getValue(encodingOpt);
      }
     
      String alphaI = "1.0";
      if (cmdLine.hasOption(alphaOpt)) {
        alphaI = (String) cmdLine.getValue(alphaOpt);
      }
     
      boolean verbose = cmdLine.hasOption(verboseOutputOpt);
     
      String testDirPath = (String) cmdLine.getValue(dirOpt);
     
      String classificationMethod = "sequential";
      if (cmdLine.hasOption(methodOpt)) {
        classificationMethod = (String) cmdLine.getValue(methodOpt);
      }
     
      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


    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

      }
     
      String classifierType = (String) cmdLine.getValue(typeOpt);
      String dataSourceType = (String) cmdLine.getValue(dataSourceOpt);
     
      BayesParameters params = new BayesParameters(Integer.parseInt((String) cmdLine.getValue(gramSizeOpt)));
     
      String alphaI = "1.0";
      if (cmdLine.hasOption(alphaOpt)) {
        alphaI = (String) cmdLine.getValue(alphaOpt);
      }
     
      params.set("alpha_i", alphaI);
     
      if (dataSourceType.equals("hbase")) {
        params.set("dataSource", "hbase");
      } else {
        params.set("dataSource", "hdfs");
      }
     
      if (classifierType.equalsIgnoreCase("bayes")) {
        log.info("Training Bayes Classifier");
        trainNaiveBayes((String) cmdLine.getValue(inputDirOpt), (String) cmdLine
View Full Code Here

 
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    algorithm = new BayesAlgorithm();
    store = new InMemoryBayesDatastore(new BayesParameters(1));
    // String[] labels = new String[]{"a", "b", "c", "d", "e"};
    // long[] labelCounts = new long[]{6, 20, 60, 100, 200};
    // String[] features = new String[]{"aa", "bb", "cc", "dd", "ee"};
    store.setSigmaJSigmaK(100.0);
   
View Full Code Here

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    algorithm = new CBayesAlgorithm();
    store = new InMemoryBayesDatastore(new BayesParameters(1));
    // String[] labels = new String[]{"a", "b", "c", "d", "e"};
    // long[] labelCounts = new long[]{6, 20, 60, 100, 200};
    // String[] features = new String[]{"aa", "bb", "cc", "dd", "ee"};
    store.setSigmaJSigmaK(500.0);
   
View Full Code Here

    BayesFeatureMapper mapper = new BayesFeatureMapper();
    JobConf conf = new JobConf();
    conf.set("io.serializations",
      "org.apache.hadoop.io.serializer.JavaSerialization,"
          + "org.apache.hadoop.io.serializer.WritableSerialization");
    conf.set("bayes.parameters", new BayesParameters(3).toString());
    mapper.configure(conf);
   
    DummyOutputCollector<StringTuple,DoubleWritable> output = new DummyOutputCollector<StringTuple,DoubleWritable>();
    mapper.map(new Text("foo"), new Text("big brown shoe"), output,
      Reporter.NULL);
View Full Code Here

    super.setUp();
    ClassifierData.writeDataToFile("testdata/bayesinput", ClassifierData.DATA);
  }
 
  public void testSelfTestBayes() throws InvalidDatastoreException, IOException {
    BayesParameters params = new BayesParameters(1);
    params.set("alpha_i", "1.0");
    params.set("dataSource", "hdfs");
    TrainClassifier.trainNaiveBayes("testdata/bayesinput", "testdata/bayesmodel", params);
   
    params.set("verbose", "true");
    params.set("basePath", "testdata/bayesmodel");
    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++) {
        if (i == j)
          assertEquals(4, matrix[i][j]);
        else
          assertEquals(0, matrix[i][j]);
      }
    }
    params.set("testDirPath", "testdata/bayesinput");
    TestClassifier.classifyParallel(params);
    Configuration conf = new Configuration();
    Path outputFiles = new Path("testdata/bayesinput-output/part*");
    FileSystem fs = FileSystem.get(outputFiles.toUri(), conf);
    matrix = BayesClassifierDriver.readResult(fs, outputFiles, conf, params).getConfusionMatrix();
View Full Code Here

      }
    }
  }
 
  public void testSelfTestCBayes() throws InvalidDatastoreException, IOException {
    BayesParameters params = new BayesParameters(1);
    params.set("alpha_i", "1.0");
    params.set("dataSource", "hdfs");
    TrainClassifier.trainCNaiveBayes("testdata/bayesinput", "testdata/cbayesmodel", params);
   
    params.set("verbose", "true");
    params.set("basePath", "testdata/cbayesmodel");
    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++) {
        if (i == j)
          assertEquals(4, matrix[i][j]);
        else
          assertEquals(0, matrix[i][j]);
      }
    }
    params.set("testDirPath", "testdata/bayesinput");
    TestClassifier.classifyParallel(params);
    Configuration conf = new Configuration();
    Path outputFiles = new Path("testdata/bayesinput-output/part*");
    FileSystem fs = FileSystem.get(outputFiles.toUri(), conf);
    matrix = BayesClassifierDriver.readResult(fs, outputFiles, conf, params).getConfusionMatrix();
View Full Code Here

      }
     
      String input = cmdLine.getValue(inputOpt).toString();
      String output = cmdLine.getValue(outputOpt).toString();
     
      job.runJob(input, output, new BayesParameters(1));
    } catch (OptionException e) {
      log.error(e.getMessage());
      CommandLineUtil.printHelp(group);
    }
  }
View Full Code Here

      }
     
      Path input = new Path(cmdLine.getValue(inputOpt).toString());
      Path output = new Path(cmdLine.getValue(outputOpt).toString());
     
      job.runJob(input, output, new BayesParameters(1));
    } catch (OptionException e) {
      log.error(e.getMessage());
      CommandLineUtil.printHelp(group);
    }
  }
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.