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.clustering.dirichlet;
 
 import  org.apache.hadoop.fs.FileStatus;
 import  org.apache.hadoop.fs.FileSystem;
 import  org.apache.hadoop.fs.Path;
 import  org.apache.hadoop.io.SequenceFile;
 import  org.apache.hadoop.io.Text;
 import  org.apache.hadoop.io.WritableComparable;
 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.OutputLogFilter;
 import  org.apache.hadoop.mapred.Reporter;
 
 
 public class DirichletMapper extends MapReduceBase implements
     Mapper<WritableComparable<?>, Vector, Text, Vector> {
 
   private DirichletState<Vectorstate;
 
   @Override
   public void map(WritableComparable<?> keyVector v,
                   OutputCollector<Text, Vectoroutput, Reporter reporterthrows IOException {
     // compute a normalized vector of probabilities that v is described by each model
     Vector pi = normalizedProbabilities(v);
     // then pick one model by sampling a Multinomial distribution based upon them
     // see: http://en.wikipedia.org/wiki/Multinomial_distribution
     int k = UncommonDistributions.rMultinom(pi);
     output.collect(new Text(String.valueOf(k)), v);
   }
 
   public void configure(DirichletState<Vectorstate) {
     this. = state;
   }
 
   @Override
   public void configure(JobConf job) {
     super.configure(job);
      = getDirichletState(job);
   }
 
   public static DirichletState<VectorgetDirichletState(JobConf job) {
     String statePath = job.get(.);
     String modelFactory = job.get(.);
     String numClusters = job.get(.);
     String alpha_0 = job.get(.);
 
     try {
       DirichletState<Vectorstate = DirichletDriver.createState(modelFactory,
           Integer.parseInt(numClusters), Double.parseDouble(alpha_0));
       Path path = new Path(statePath);
       FileSystem fs = FileSystem.get(path.toUri(), job);
       FileStatus[] status = fs.listStatus(pathnew OutputLogFilter());
       for (FileStatus s : status) {
         SequenceFile.Reader reader = new SequenceFile.Reader(fss.getPath(),
             job);
         try {
           Text key = new Text();
           DirichletCluster<Vectorcluster = new DirichletCluster<Vector>();
           while (reader.next(keycluster)) {
             int index = Integer.parseInt(key.toString());
             state.getClusters().set(indexcluster);
             cluster = new DirichletCluster<Vector>();
           }
         } finally {
           reader.close();
         }
       }
       // TODO: with more than one mapper, they will all have different mixtures. Will this matter?
       state.setMixture(UncommonDistributions.rDirichlet(state.totalCounts()));
       return state;
     } catch (Exception e) {
       throw new IllegalStateException(e);
     }
   }

  
Compute a normalized vector of probabilities that v is described by each model using the mixture and the model pdfs

Parameters:
state the DirichletState<Vector> of this iteration
v an Vector
Returns:
the Vector of probabilities
  private static Vector normalizedProbabilities(DirichletState<VectorstateVector v) {
    Vector pi = new DenseVector(state.getNumClusters());
    double max = 0;
    for (int k = 0; k < state.getNumClusters(); k++) {
      double p = state.adjustedProbability(vk);
      pi.set(kp);
      if (max < p) {
        max = p;
      }
    }
    // normalize the probabilities by largest observed value
    pi.assign(new TimesFunction(), 1.0 / max);
    return pi;
  }
New to GrepCode? Check out our FAQ X