You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ss...@apache.org on 2014/04/15 07:22:32 UTC
svn commit: r1587423 - in
/mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes:
ComplementaryNaiveBayesClassifier.java NaiveBayesModel.java
StandardNaiveBayesClassifier.java training/TrainNaiveBayesJob.java
Author: ssc
Date: Tue Apr 15 05:22:31 2014
New Revision: 1587423
URL: http://svn.apache.org/r1587423
Log:
MAHOUT-1504 fixed minor style issues
Modified:
mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/ComplementaryNaiveBayesClassifier.java
mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/NaiveBayesModel.java
mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/StandardNaiveBayesClassifier.java
mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/training/TrainNaiveBayesJob.java
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/ComplementaryNaiveBayesClassifier.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/ComplementaryNaiveBayesClassifier.java?rev=1587423&r1=1587422&r2=1587423&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/ComplementaryNaiveBayesClassifier.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/ComplementaryNaiveBayesClassifier.java Tue Apr 15 05:22:31 2014
@@ -18,10 +18,7 @@
package org.apache.mahout.classifier.naivebayes;
-/**
- * Class implementing the Naive Bayes Classifier Algorithm
- *
- */
+/** Implementation of the Naive Bayes Classifier Algorithm */
public class ComplementaryNaiveBayesClassifier extends AbstractNaiveBayesClassifier {
public ComplementaryNaiveBayesClassifier(NaiveBayesModel model) {
super(model);
@@ -30,13 +27,13 @@ public class ComplementaryNaiveBayesClas
@Override
public double getScoreForLabelFeature(int label, int feature) {
NaiveBayesModel model = getModel();
- double weight=computeWeight(model.featureWeight(feature), model.weight(label, feature),
+ double weight = computeWeight(model.featureWeight(feature), model.weight(label, feature),
model.totalWeightSum(), model.labelWeight(label), model.alphaI(), model.numFeatures());
- // http://people.csail.mit.edu/jrennie/papers/icml03-nb.pdf - Section 3.2, Weight Magnitude Errors
- return weight/model.thetaNormalizer(label);
+ // see http://people.csail.mit.edu/jrennie/papers/icml03-nb.pdf - Section 3.2, Weight Magnitude Errors
+ return weight / model.thetaNormalizer(label);
}
- // http://people.csail.mit.edu/jrennie/papers/icml03-nb.pdf - Section 3.1, Skewed Data bias
+ // see http://people.csail.mit.edu/jrennie/papers/icml03-nb.pdf - Section 3.1, Skewed Data bias
public static double computeWeight(double featureWeight, double featureLabelWeight,
double totalWeight, double labelWeight, double alphaI, double numFeatures) {
double numerator = featureWeight - featureLabelWeight + alphaI;
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/NaiveBayesModel.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/NaiveBayesModel.java?rev=1587423&r1=1587422&r2=1587423&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/NaiveBayesModel.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/NaiveBayesModel.java Tue Apr 15 05:22:31 2014
@@ -33,12 +33,11 @@ import org.apache.mahout.math.VectorWrit
import com.google.common.base.Preconditions;
import com.google.common.io.Closeables;
-/** NaiveBayesModel holds the weight Matrix, the feature and label sums and the weight normalizer vectors.*/
+/** NaiveBayesModel holds the weight matrix, the feature and label sums and the weight normalizer vectors.*/
public class NaiveBayesModel {
private final Vector weightsPerLabel;
private final Vector perlabelThetaNormalizer;
- // private final double minThetaNormalizer;
private final Vector weightsPerFeature;
private final Matrix weightsPerLabelAndFeature;
private final float alphaI;
@@ -57,7 +56,6 @@ public class NaiveBayesModel {
this.numFeatures = weightsPerFeature.getNumNondefaultElements();
this.totalWeightSum = weightsPerLabel.zSum();
this.alphaI = alphaI;
-// this.minThetaNormalizer = thetaNormalizer.maxValue();
}
public double labelWeight(int label) {
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/StandardNaiveBayesClassifier.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/StandardNaiveBayesClassifier.java?rev=1587423&r1=1587422&r2=1587423&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/StandardNaiveBayesClassifier.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/StandardNaiveBayesClassifier.java Tue Apr 15 05:22:31 2014
@@ -18,7 +18,7 @@
package org.apache.mahout.classifier.naivebayes;
-/** Class implementing the Naive Bayes Classifier Algorithm */
+/** Implementation of the Naive Bayes Classifier Algorithm */
public class StandardNaiveBayesClassifier extends AbstractNaiveBayesClassifier {
public StandardNaiveBayesClassifier(NaiveBayesModel model) {
@@ -28,9 +28,8 @@ public class StandardNaiveBayesClassifie
@Override
public double getScoreForLabelFeature(int label, int feature) {
NaiveBayesModel model = getModel();
- // Standard Naive Bayes does not use weight normalization
- // uncomment following line for weight normalized NB
- // weight=weight/model.thetaNormalizer(label);
+ // Standard Naive Bayes does not use weight normalization, uncomment following line for weight normalized NB
+ // weight = weight / model.thetaNormalizer(label);
return computeWeight(model.weight(label, feature), model.labelWeight(label), model.alphaI(), model.numFeatures());
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/training/TrainNaiveBayesJob.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/training/TrainNaiveBayesJob.java?rev=1587423&r1=1587422&r2=1587423&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/training/TrainNaiveBayesJob.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/classifier/naivebayes/training/TrainNaiveBayesJob.java Tue Apr 15 05:22:31 2014
@@ -43,9 +43,7 @@ import org.apache.mahout.math.VectorWrit
import com.google.common.base.Splitter;
-/**
- * This class trains a Naive Bayes Classifier (Parameters for both Naive Bayes and Complementary Naive Bayes)
- */
+/** Trains a Naive Bayes Classifier (parameters for both Naive Bayes and Complementary Naive Bayes) */
public final class TrainNaiveBayesJob extends AbstractJob {
private static final String TRAIN_COMPLEMENTARY = "trainComplementary";
private static final String ALPHA_I = "alphaI";
@@ -93,13 +91,12 @@ public final class TrainNaiveBayesJob ex
}
long labelSize = createLabelIndex(labPath);
float alphaI = Float.parseFloat(getOption(ALPHA_I));
- //boolean trainComplementary = Boolean.parseBoolean(getOption(TRAIN_COMPLEMENTARY)); //always result to false
boolean trainComplementary = hasOption(TRAIN_COMPLEMENTARY);
HadoopUtil.setSerializations(getConf());
HadoopUtil.cacheFiles(labPath, getConf());
- //add up all the vectors with the same labels, while mapping the labels into our index
+ // Add up all the vectors with the same labels, while mapping the labels into our index
Job indexInstances = prepareJob(getInputPath(),
getTempPath(SUMMED_OBSERVATIONS),
SequenceFileInputFormat.class,
@@ -115,7 +112,7 @@ public final class TrainNaiveBayesJob ex
if (!succeeded) {
return -1;
}
- //sum up all the weights from the previous step, per label and per feature
+ // Sum up all the weights from the previous step, per label and per feature
Job weightSummer = prepareJob(getTempPath(SUMMED_OBSERVATIONS),
getTempPath(WEIGHTS),
SequenceFileInputFormat.class,
@@ -133,11 +130,10 @@ public final class TrainNaiveBayesJob ex
return -1;
}
- //put the per label and per feature vectors into the cache
+ // Put the per label and per feature vectors into the cache
HadoopUtil.cacheFiles(getTempPath(WEIGHTS), getConf());
- // calculate the per label theta normalizers, write out to LABEL_THETA_NORMALIZER vector --
- // Rennie 3.2
+ // Calculate the per label theta normalizers, write out to LABEL_THETA_NORMALIZER vector -- Rennie 3.2
Job thetaSummer = prepareJob(getTempPath(SUMMED_OBSERVATIONS),
getTempPath(THETAS),
SequenceFileInputFormat.class,
@@ -156,10 +152,10 @@ public final class TrainNaiveBayesJob ex
return -1;
}
- //put the per label theta normalizers into the cache
+ // Put the per label theta normalizers into the cache
HadoopUtil.cacheFiles(getTempPath(THETAS), getConf());
- //validate our model and then write it out to the official output
+ // Validate our model and then write it out to the official output
getConf().setFloat(ThetaMapper.ALPHA_I, alphaI);
NaiveBayesModel naiveBayesModel = BayesUtils.readModelFromDir(getTempPath(), getConf());
naiveBayesModel.validate();