Package org.apache.mahout.math

Examples of org.apache.mahout.math.Vector


    Path path = new Path("testdata/clusters/part-00000");
    SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
        path, Text.class, Cluster.class);
   
    for (int i = 0; i < k; i++) {
      Vector vec = vectors.get(i);
      Cluster cluster = new Cluster(vec, i, new EuclideanDistanceMeasure());
      writer.append(new Text(cluster.getIdentifier()), cluster);
    }
    writer.close();
   
View Full Code Here


    FeatureVectorEncoder[] encoder = new FeatureVectorEncoder[FIELDS];
    for (int i = 0; i < FIELDS; i++) {
      encoder[i] = new ConstantValueEncoder("v" + i);
    }
    long t0 = System.currentTimeMillis();
    Vector v = new DenseVector(1000);
    ByteBuffer buf = ByteBuffer.wrap(FileUtils
        .readFileToByteArray(new File(args[1])));
    FastLine line = FastLine.read(buf);
    while (line != null) {
      v.assign(0);
      for (int i = 0; i < FIELDS; i++) {
        encoder[i].addToVector((byte[]) null, line.getDouble(i), v);
      }
      line = FastLine.read(buf);
    }
View Full Code Here

    StringReader in = new StringReader("text to magically vectorize");
    TokenStream ts = analyzer.tokenStream("body", in);
    TermAttribute termAtt = ts.addAttribute(TermAttribute.class);

    Vector v1 = new RandomAccessSparseVector(100);                  
    while (ts.incrementToken()) {
      char[] termBuffer = termAtt.termBuffer();
      int termLen = termAtt.termLength();
      String w = new String(termBuffer, 0, termLen);                
      encoder.addToVector(w, 1, v1);                                
View Full Code Here

    int[] bumps = {1, 2, 5};
    for (File file : files) {
      String ng = file.getParentFile().getName();
      int actual = newsGroups.intern(ng);

      Vector v = encodeFeatureVector(file);
      learningAlgorithm.train(actual, v);

      k++;

      int bump = bumps[(int) Math.floor(step) % bumps.length];
View Full Code Here

    encoder.setTraceDictionary(traceDictionary);
    bias.setTraceDictionary(traceDictionary);

    for (File file : permute(files, rand).subList(0, 500)) {
      traceDictionary.clear();
      Vector v = encodeFeatureVector(file);
      md.update(v, traceDictionary, model);
    }

    List<String> ngNames = Lists.newArrayList(newsGroups.values());
    List<ModelDissector.Weight> weights = md.summary(100);
View Full Code Here

      }
    } finally {
      reader.close();
    }

    Vector v = new RandomAccessSparseVector(FEATURES);
    bias.addToVector((byte[]) null, 1, v);
    encoder.flush(1, v);
    return v;
  }
View Full Code Here

  @Override
  protected void reduce(Text tag,
                        Iterable<VectorWritable> values,
                        Context context) throws IOException,
                                        InterruptedException {
    Vector vector = null;
    for (VectorWritable partialVector : values) {
      if (vector == null) {
        vector = partialVector.get().like();
      }
      partialVector.get().addTo(vector);
View Full Code Here

  private Map<Long, Double> interactionCache = Maps.newHashMap();

  private FeatureEncoder encoder = new FeatureEncoder();

  public List<ScoredItem> topItems(User u, int limit) {
    Vector userVector = new RandomAccessSparseVector(model.numFeatures());
    encoder.addUserFeatures(u, userVector);
    double userScore = model.classifyScalarNoLink(userVector);

    PriorityQueue<ScoredItem> r = new PriorityQueue<ScoredItem>();
    for (Item item : items) {
      Double itemScore = itemCache.get(item);
      if (itemScore == null) {
        Vector v = new RandomAccessSparseVector(model.numFeatures());
        encoder.addItemFeatures(item, v);
        itemScore = model.classifyScalarNoLink(v);
        itemCache.put(item, itemScore);
      }

      long code = encoder.interactionHash(u, item);
      Double interactionScore = interactionCache.get(code);
      if (interactionScore == null) {
        Vector v = new RandomAccessSparseVector(model.numFeatures());
        encoder.addInteractions(u, item, v);
        interactionScore = model.classifyScalarNoLink(v);
        interactionCache.put(code, interactionScore);
      }
      double score = userScore + itemScore + interactionScore;
View Full Code Here

    FeatureVectorEncoder[] encoder = new FeatureVectorEncoder[FIELDS];
    for (int i = 0; i < FIELDS; i++) {
      encoder[i] = new ConstantValueEncoder("v" + i);
    }
    long t0 = System.currentTimeMillis();
    Vector v = new DenseVector(1000);
    BufferedReader in = new BufferedReader(new FileReader(args[1]));
    String line = in.readLine();
    while (line != null) {
      v.assign(0);
      Line x = new Line(line);
      for (int i = 0; i < FIELDS; i++) {
        encoder[i].addToVector((byte[]) null, x.getDouble(i), v);
      }
      line = in.readLine();
View Full Code Here

public class VectorExamplesTest extends TamingTextTestJ4 {
  @Test
  public void testProgrammatic() throws Exception {
    //<start id="vec.examples.programmatic"/>
    double[] vals = new double[]{0.3, 1.8, 200.228};
    Vector dense = new DenseVector(vals);//<co id="vec.exam.dense"/>
    assertTrue(dense.size() == 3);
    Vector sparseSame = new SequentialAccessSparseVector(3);//<co id="vec.exam.sparse.same"/>
    Vector sparse = new SequentialAccessSparseVector(3000);//<co id="vec.exam.sparse"/>
    for (int i = 0; i < vals.length; i++) {//<co id="vec.exam.assign.sparse"/>
      sparseSame.set(i, vals[i]);
      sparse.set(i, vals[i]);
    }
    assertFalse(dense.equals(sparse));//<co id="vec.exam.notequals.d.s"/>
    assertEquals(dense, sparseSame);//<co id="vec.exam.equals.d.s"/>
    assertFalse(sparse.equals(sparseSame));
    /*
<calloutlist>
    <callout arearefs="vec.exam.dense"><para>Create a <classname>DenseVector</classname> with a label of "my-dense" and 3 values.  The cardinality of this vector is 3 </para></callout>
    <callout arearefs="vec.exam.sparse.same"><para>Create a <classname>SparseVector</classname> with a label of my-sparse-same that has cardinality of 3</para></callout>

View Full Code Here

TOP

Related Classes of org.apache.mahout.math.Vector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.