Examples of SparseVector


Examples of cc.mallet.types.SparseVector

  }

 
  public void testSetValue ()
  {
    SparseVector s = (SparseVector) s1.cloneMatrix ();
    s.setValue (5, 0.3);

    double[] ans = new double[] {1, 0.3, 3, 4, 5};
    for (int i = 0; i < s.numLocations(); i++) {
      assertTrue (s.valueAtLocation (i) == ans[i]);
    }
  }
View Full Code Here

Examples of cc.mallet.types.SparseVector

    }
  }

  public void testDenseSparseVector ()
  {
    SparseVector svDense = new SparseVector (null, dbl3);
    double sdot = svDense.dotProduct (svDense);
    double ddot = d1.dotProduct (d1);
    assertEquals (sdot, ddot, 0.0001);

    svDense.plusEqualsSparse (s1);
    checkAnswer (svDense, new double[] { 2.0, 2.5, 3.0, 5.7, 3.5,
                                         5.6, 0,   3,   0,   0,
                                         0,   0,   0,   4,   0,
                                         5, });

    svDense.plusEqualsSparse (s1, 2.0);
    checkAnswer (svDense, new double[] { 2.0, 2.5, 3.0, 7.7, 3.5,
                                         9.6, 0,   9,   0,   0,
                                         0,   0,   0,   12,   0,
                                         15, });
   
    double[] dbl4 = new double [dbl3.length + 1];
    for (int i = 0; i < dbl4.length; i++) dbl4[i] = 2.0;
    SparseVector sv4 = new SparseVector (null, dbl4);
    svDense.plusEqualsSparse (sv4);
    checkAnswer (svDense, new double[] { 4.04.5,    5.09.7,   5.5,
                                         11.6, 2.0,   11.02.0,   2.0,
                                         2,   2,   2,   14,   2.0,
                                         17, });
View Full Code Here

Examples of cc.mallet.types.SparseVector

  private static int[] idx2 = { 3, 7, 12, 15, 18 };

  public void testBinaryVector ()
  {
    SparseVector binary1 = new SparseVector (idxs, null, idxs.length, idxs.length,
                                             false, false, false);
    SparseVector binary2 = new SparseVector (idx2, null, idx2.length, idx2.length,
                                            false, false, false);

    assertEquals (3, binary1.dotProduct (binary2), 0.0001);
    assertEquals (3, binary2.dotProduct (binary1), 0.0001);

    assertEquals (15.0, binary1.dotProduct (s1), 0.0001);
    assertEquals (15.0, s1.dotProduct (binary1), 0.0001);

    assertEquals (9.0, binary2.dotProduct (s1), 0.0001);
    assertEquals (9.0, s1.dotProduct (binary2), 0.0001);

    SparseVector dblVec = (SparseVector) s1.cloneMatrix ();
    dblVec.plusEqualsSparse (binary1);
    checkAnswer (dblVec, new double[] { 2, 3, 4, 5, 6 });

    SparseVector dblVec2 = (SparseVector) s1.cloneMatrix ();
    dblVec2.plusEqualsSparse (binary2);
    checkAnswer (dblVec2, new double[] { 2, 2, 4, 4, 6 });
  }
View Full Code Here

Examples of cc.mallet.types.SparseVector

    checkAnswer (dblVec2, new double[] { 2, 2, 4, 4, 6 });
  }
 
  public void testCloneMatrixZeroed ()
  {
    SparseVector s = (SparseVector) s1.cloneMatrixZeroed ();
    for (int i = 0; i < s.numLocations(); i++) {
      assertTrue (s.valueAtLocation (i) == 0.0);
      assertTrue (s.indexAtLocation (i) == idxs [i]);
    }
  }
View Full Code Here

Examples of cc.mallet.types.SparseVector

    ByteArrayOutputStream baos = new ByteArrayOutputStream ();
    PrintStream out = new PrintStream (baos);
    PrintStream oldOut = System.out;
    System.setOut (out);

    SparseVector standard = new SparseVector (idxs, dbl2);
    standard.print ();
    assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.5\nSparseVector[7] = 2.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ());
    baos.reset ();

    SparseVector dense = new SparseVector (null, dbl2);
    dense.print ();
    assertEquals ("SparseVector[0] = 1.0\nSparseVector[1] = 1.5\nSparseVector[2] = 2.0\nSparseVector[3] = 1.0\nSparseVector[4] = 1.0\n", baos.toString ());
    baos.reset ();

    SparseVector binary = new SparseVector (idxs, null, idxs.length, idxs.length,
                                            false, false, false);
    binary.print ();
    assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.0\nSparseVector[7] = 1.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ());
    baos.reset ();
  }
View Full Code Here

Examples of cc.mallet.types.SparseVector

    assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.0\nSparseVector[7] = 1.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ());
    baos.reset ();
  }

  public void testExtendedDotProduct () {
    SparseVector v1 = new SparseVector (null, dbl3);
    SparseVector vInf = new SparseVector (null, dbl4);
    double dp = v1.dotProduct (vInf);
    assertTrue (!Double.isNaN(dp));
    dp = vInf.dotProduct (v1);
    assertTrue (!Double.isNaN(dp));
  }
View Full Code Here

Examples of cc.mallet.types.SparseVector

    int instClust;
    double instClustDist, instDist;
    double deltaMeans = Double.MAX_VALUE;
    double deltaPoints = (double) instances.size();
    int iterations = 0;
    SparseVector clusterMean;

    for (int c = 0; c < numClusters; c++) {
      instanceClusters.add(c, new InstanceList(instancePipe));
    }

    logger.info("Entering KMeans iteration");

    while (deltaMeans > MEANS_TOLERANCE && iterations < MAX_ITER
        && deltaPoints > instances.size() * POINTS_TOLERANCE) {

      iterations++;
      deltaPoints = 0;

      // For each instance, measure its distance to the current cluster
      // means, and subsequently assign it to the closest cluster
      // by adding it to an corresponding instance list
      // The mean of each cluster InstanceList is then updated.
      for (int n = 0; n < instances.size(); n++) {

        instClust = 0;
        instClustDist = Double.MAX_VALUE;

        for (int c = 0; c < numClusters; c++) {
          instDist = metric.distance(clusterMeans.get(c),
              (SparseVector) instances.get(n).getData());

          if (instDist < instClustDist) {
            instClust = c;
            instClustDist = instDist;
          }
        }
        // Add to closest cluster & label it such
        instanceClusters.get(instClust).add(instances.get(n));

        if (clusterLabels[n] != instClust) {
          clusterLabels[n] = instClust;
          deltaPoints++;
        }

      }

      deltaMeans = 0;

      for (int c = 0; c < numClusters; c++) {

        if (instanceClusters.get(c).size() > 0) {
          clusterMean = VectorStats.mean(instanceClusters.get(c));

          deltaMeans += metric.distance(clusterMeans.get(c), clusterMean);

          clusterMeans.set(c, clusterMean);

          instanceClusters.set(c, new InstanceList(instancePipe));

        } else {

          logger.info("Empty cluster found.");

          switch (emptyAction) {
            case EMPTY_ERROR:
              return null;
            case EMPTY_DROP:
              logger.fine("Removing cluster " + c);
              clusterMeans.remove(c);
              instanceClusters.remove(c);
              for (int n = 0; n < instances.size(); n++) {

                assert (clusterLabels[n] != c) : "Cluster size is "
                    + instanceClusters.get(c).size()
                    + "+ yet clusterLabels[n] is " + clusterLabels[n];

                if (clusterLabels[n] > c)
                  clusterLabels[n]--;
              }

              numClusters--;
              c--; // <-- note this trickiness. bad style? maybe.
              // it just means now that we've deleted the entry,
              // we have to repeat the index to get the next entry.
              break;

            case EMPTY_SINGLE:

              // Get the instance the furthest from any centroid
              // and make it a new centroid.

              double newCentroidDist = 0;
              int newCentroid = 0;
              InstanceList cacheList = null;

              for (int clusters = 0; clusters < clusterMeans.size(); clusters++) {
                SparseVector centroid = clusterMeans.get(clusters);
                InstanceList centInstances = instanceClusters.get(clusters);

                // Dont't create new empty clusters.

                if (centInstances.size() <= 1)
View Full Code Here

Examples of cc.mallet.types.SparseVector

    // cluster assignments.

    ArrayList<Instance> instances = new ArrayList<Instance>(instList.size());
    for (int i = 0; i < instList.size(); i++) {
      Instance ins = instList.get(i);
      SparseVector sparse = (SparseVector) ins.getData();
      if (sparse.numLocations() == 0)
        continue;

      instances.add(ins);
    }

    // Add next center that has the MAX of the MIN of the distances from
    // each of the previous j-1 centers (idea from Andrew Moore tutorial,
    // not sure who came up with it originally)

    for (int i = 0; i < numClusters; i++) {
      double max = 0;
      int selected = 0;
      for (int k = 0; k < instances.size(); k++) {
        double min = Double.MAX_VALUE;
        Instance ins = instances.get(k);
        SparseVector inst = (SparseVector) ins.getData();
        for (int j = 0; j < clusterMeans.size(); j++) {
          SparseVector centerInst = clusterMeans.get(j);
          double dist = metric.distance(centerInst, inst);
          if (dist < min)
            min = dist;

        }
View Full Code Here

Examples of cc.mallet.types.SparseVector

    {
      // Do the defaults first
      defaultConstraints = new SparseVector [templates.length];
      defaultExpectations = new SparseVector [templates.length];
      for (int tidx = 0; tidx < templates.length; tidx++) {
        SparseVector defaults = templates[tidx].getDefaultWeights();
        defaultConstraints[tidx] = (SparseVector) defaults.cloneMatrixZeroed ();
        defaultExpectations[tidx] = (SparseVector) defaults.cloneMatrixZeroed ();
      }

      // And now the others
      constraints = new SparseVector [templates.length][];
      expectations = new SparseVector [templates.length][];
View Full Code Here

Examples of cc.mallet.types.SparseVector

        throw new IllegalArgumentException("Argument is not of the " +
                                           " correct dimensions");
      int idx = 0;
      for (int tidx = 0; tidx < templates.length; tidx++) {
        ACRF.Template tmpl = templates [tidx];
        SparseVector defaults = tmpl.getDefaultWeights ();
        double[] values = defaults.getValues();
        System.arraycopy (values, 0, buf, idx, values.length);
        idx += values.length;
      }

      for (int tidx = 0; tidx < templates.length; tidx++) {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.