Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.mahout.classifier.bayes.mapreduce.bayes;
 
 import  org.apache.hadoop.conf.Configuration;
 import  org.apache.hadoop.fs.FileStatus;
 import  org.apache.hadoop.fs.FileSystem;
 import  org.apache.hadoop.fs.Path;
 import  org.apache.hadoop.io.DoubleWritable;
 import  org.apache.hadoop.io.SequenceFile;
 import  org.apache.hadoop.mapred.FileInputFormat;
 import  org.apache.hadoop.mapred.FileOutputFormat;
 import  org.apache.hadoop.mapred.JobClient;
 import  org.apache.hadoop.mapred.JobConf;
 import  org.apache.hadoop.mapred.KeyValueTextInputFormat;
 import  org.apache.hadoop.mapred.SequenceFileOutputFormat;
 
 import java.util.Map;

Create and run the Bayes Classifier
 
 public class BayesClassifierDriver {
 
   private static final Logger log = LoggerFactory.getLogger(BayesClassifierDriver.class);
   private BayesClassifierDriver() {
   }

  
Run the job

Parameters:
params The Job parameters containing the gramSize, input output folders, defaultCat, encoding
 
   public static final void runJob(BayesParameters paramsthrows IOException {
     JobClient client = new JobClient();
     JobConf conf = new JobConf(BayesClassifierDriver.class);
     conf.setJobName("Bayes Classifier Driver running over input: " + params.get("testDirPath"));
     conf.setOutputKeyClass(StringTuple.class);
     conf.setOutputValueClass(DoubleWritable.class);
 
     FileInputFormat.setInputPaths(confnew Path(params.get("testDirPath")));
     Path outPath = new Path(params.get("testDirPath") + "-output");
     FileOutputFormat.setOutputPath(confoutPath);
 
     conf.setInputFormat(KeyValueTextInputFormat.class);
     conf.setMapperClass(BayesClassifierMapper.class);
     conf.setCombinerClass(BayesClassifierReducer.class);
     conf.setReducerClass(BayesClassifierReducer.class);
     conf.setOutputFormat(SequenceFileOutputFormat.class);
     
     conf.set("io.serializations",
         "org.apache.hadoop.io.serializer.JavaSerialization,org.apache.hadoop.io.serializer.WritableSerialization");
 
     FileSystem dfs = FileSystem.get(outPath.toUri(), conf);
     if (dfs.exists(outPath)) {
       dfs.delete(outPathtrue);
     }
     conf.set("bayes.parameters"params.toString());
 
     client.setConf(conf);
     JobClient.runJob(conf);
 
     Path outputFiles = new Path(outPath.toString() + "/part*");
     ConfusionMatrix matrix = readResult(dfsoutputFilesconfparams);
     .info("{}",matrix.summarize());
   }
 
   private static ConfusionMatrix readResult(FileSystem fs, Path pathPattern, Configuration confBayesParameters params)
       throws IOException {
    
     StringTuple key = new StringTuple();
     DoubleWritable value = new DoubleWritable();
     String defaultLabel = params.get("defaultCat");
     FileStatus[] outputFiles = fs.globStatus(pathPattern);
     Map<StringMap<StringInteger>> confusionMatrix = new HashMap<StringMap<StringInteger>>();
     
     for (FileStatus fileStatus : outputFiles) {
       Path path = fileStatus.getPath();
       SequenceFile.Reader reader = new SequenceFile.Reader(fspathconf);
      while (reader.next(keyvalue)) {        
        String correctLabel = key.stringAt(1);
        String classifiedLabel = key.stringAt(2);
        Map<StringIntegerrowMatrix = confusionMatrix.get(correctLabel);
        if(rowMatrix == null)
          rowMatrix = new HashMap<StringInteger>();        
        Integer count = Double.valueOf(value.get()).intValue();
        rowMatrix.put(classifiedLabelcount);
        confusionMatrix.put(correctLabelrowMatrix);
        
      }
    }
    
    ConfusionMatrix matrix = new ConfusionMatrix(confusionMatrix.keySet(), defaultLabel);
    for(Map.Entry<StringMap<StringInteger>> correctLabelSet:confusionMatrix.entrySet())
    {
      Map<StringIntegerrowMatrix = correctLabelSet.getValue();
      for(Map.Entry<StringIntegerclassifiedLabelSet : rowMatrix.entrySet())
      {
        matrix.addInstance(correctLabelSet.getKey(), classifiedLabelSet.getKey());
        matrix.putCount(correctLabelSet.getKey(), classifiedLabelSet.getKey(), classifiedLabelSet.getValue());
      }
    }
    return matrix;
    
  }
New to GrepCode? Check out our FAQ X