@Test
  public void testAddToVector() {
    TextValueEncoder enc = new TextValueEncoder("text");
    Vector v1 = new DenseVector(200);
    enc.addToVector("test1 and more", v1);
    enc.flush(1, v1);
    // should set 6 distinct locations to 1
    assertEquals(6.0, v1.norm(1), 0);
    assertEquals(1.0, v1.maxValue(), 0);

    // now some fancy weighting
    StaticWordValueEncoder w = new StaticWordValueEncoder("text");
    w.setDictionary(ImmutableMap.<String, Double>of("word1", 3.0, "word2", 1.5));
    enc.setWordEncoder(w);

    // should set 6 locations to something
    Vector v2 = new DenseVector(200);
    enc.addToVector("test1 and more", v2);
    enc.flush(1, v2);

    // this should set the same 6 locations to the same values
    Vector v3 = new DenseVector(200);
    w.addToVector("test1", v3);
    w.addToVector("and", v3);
    w.addToVector("more", v3);
    assertEquals(0, v3.minus(v2).norm(1), 0);

    // moreover, the locations set in the unweighted case should be the same as in the weighted case
    assertEquals(v3.zSum(), v3.dot(v1), 0);
  }
 public NaiveBayesModel(
     Matrix weightMatrix,
     Vector weightsPerFeature,
     Vector weightsPerLabel,
     Vector thetaNormalizer,
     float alphaI) {
   this.weightsPerLabelAndFeature = weightMatrix;
   this.weightsPerFeature = weightsPerFeature;
   this.weightsPerLabel = weightsPerLabel;
   this.perlabelThetaNormalizer = thetaNormalizer;
   this.numFeatures = weightsPerFeature.getNumNondefaultElements();
   this.totalWeightSum = weightsPerLabel.zSum();
   this.alphaI = alphaI;
 }
예제 #3
0
 @Override
 public double apply(Vector f) {
   return f.zSum();
 }