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 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.mapred.JobConf;
 import  org.apache.hadoop.mapred.MapReduceBase;
 import  org.apache.hadoop.mapred.Mapper;
 import  org.apache.hadoop.mapred.OutputCollector;
 import  org.apache.hadoop.mapred.Reporter;
 import  org.apache.hadoop.util.GenericsUtil;
 import java.util.Map;
 public class BayesThetaNormalizerMapper extends MapReduceBase implements
     Mapper<StringTuple, DoubleWritable, StringTuple, DoubleWritable> {
   private static final Logger log = LoggerFactory.getLogger(BayesThetaNormalizerMapper.class);
   private Map<StringDoublelabelWeightSum = null;
   private double sigma_jSigma_k = 0.0;
   private double vocabCount = 0.0;
   private double alpha_i = 1.0;

We need to calculate the thetaNormalization factor of each label

key The label,feature pair
value The tfIdf of the pair
   public void map(StringTuple key, DoubleWritable value,
                   OutputCollector<StringTuple, DoubleWritable> output, Reporter reporter)
       throws IOException {
     String label = key.stringAt(1);
     reporter.setStatus("Bayes Theta Normalizer Mapper: " + label);
     double weight = Math.log((value.get() + ) / (.get(label) + ));
     StringTuple thetaNormalizerTuple = new StringTuple(.);
     output.collect(thetaNormalizerTuplenew DoubleWritable(weight));
   public void configure(JobConf job) {
     try {
       if ( == null) {
          = new HashMap<StringDouble>();
         DefaultStringifier<Map<StringDouble>> mapStringifier = new DefaultStringifier<Map<StringDouble>>(
             job, GenericsUtil.getClass());
         String labelWeightSumString = mapStringifier.toString();
         labelWeightSumString = job.get("cnaivebayes.sigma_k",
          = mapStringifier.fromString(labelWeightSumString);
         DefaultStringifier<Doublestringifier = new DefaultStringifier<Double>(
             job, GenericsUtil.getClass());
         String sigma_jSigma_kString = stringifier.toString();
         sigma_jSigma_kString = job.get("cnaivebayes.sigma_jSigma_k",
          = stringifier.fromString(sigma_jSigma_kString);
         String vocabCountString = stringifier.toString();
         vocabCountString = job.get("cnaivebayes.vocabCount"vocabCountString);
          = stringifier.fromString(vocabCountString);
         Parameters params = Parameters.fromString(job.get("bayes.parameters"""));
          = Double.valueOf(params.get("alpha_i""1.0"));
     } catch (IOException ex) {
       .warn(ex.toString(), ex);
New to GrepCode? Check out our FAQ X