Examples of SparseMatrix


Examples of org.apache.mahout.math.SparseMatrix

  public ClosestCentroidBenchmark(VectorBenchmarks mark) {
    this.mark = mark;
  }

  public void benchmark(DistanceMeasure measure) throws IOException {
    SparseMatrix clusterDistances = new SparseMatrix(mark.numClusters, mark.numClusters);
    for (int i = 0; i < mark.numClusters; i++) {
      for (int j = 0; j < mark.numClusters; j++) {
        double distance = Double.POSITIVE_INFINITY;
        if (i != j) {
          distance = measure.distance(mark.clusters[i], mark.clusters[j]);
        }
        clusterDistances.setQuick(i, j, distance);
      }
    }

    long distanceCalculations = 0;
    TimingStatistics stats = new TimingStatistics();
    for (int l = 0; l < mark.loop; l++) {
      TimingStatistics.Call call = stats.newCall(mark.leadTimeUsec);
      for (int i = 0; i < mark.numVectors; i++) {
        Vector vector = mark.vectors[1][mark.vIndex(i)];
        double minDistance = Double.MAX_VALUE;
        for (int k = 0; k < mark.numClusters; k++) {
          double distance = measure.distance(vector, mark.clusters[k]);
          distanceCalculations++;
          if (distance < minDistance) {
            minDistance = distance;
          }
        }
      }
      if (call.end(mark.maxTimeUsec)) {
        break;
      }
    }
    mark.printStats(stats, measure.getClass().getName(), "Closest C w/o Elkan's trick", "distanceCalculations = "
        + distanceCalculations);

    distanceCalculations = 0;
    stats = new TimingStatistics();
    Random rand = RandomUtils.getRandom();
    for (int l = 0; l < mark.loop; l++) {
      TimingStatistics.Call call = stats.newCall(mark.leadTimeUsec);
      for (int i = 0; i < mark.numVectors; i++) {
        Vector vector = mark.vectors[1][mark.vIndex(i)];
        int closestCentroid = rand.nextInt(mark.numClusters);
        double dist = measure.distance(vector, mark.clusters[closestCentroid]);
        distanceCalculations++;
        for (int k = 0; k < mark.numClusters; k++) {
          if (closestCentroid != k) {
            double centroidDist = clusterDistances.getQuick(k, closestCentroid);
            if (centroidDist < 2 * dist) {
              dist = measure.distance(vector, mark.clusters[k]);
              closestCentroid = k;
              distanceCalculations++;
            }
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

      }
    }
  }

  public Matrix buildPreferenceVectorForUser(long realId) throws TasteException {
    Matrix ids = new SparseMatrix(1, dataModel.getNumItems());
    for (Preference pref : dataModel.getPreferencesFromUser(realId)) {
      int itemidx = itemIndex(pref.getItemID());
      ids.setQuick(0, itemidx, pref.getValue());
    }
    return ids;
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

    return ids;
  }

  private Matrix buildConfidenceMatrixForItem(long itemId) throws TasteException {
    PreferenceArray prefs = dataModel.getPreferencesForItem(itemId);
    Matrix confidenceMatrix = new SparseMatrix(dataModel.getNumUsers(), dataModel.getNumUsers());
    for (Preference pref : prefs) {
      long userId = pref.getUserID();
      int userIdx = userIndex(userId);
      confidenceMatrix.setQuick(userIdx, userIdx, 1);
    }
    return new DiagonalMatrix(confidenceMatrix);
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

    return new DiagonalMatrix(confidenceMatrix);
  }

  private Matrix buildConfidenceMatrixForUser(long userId) throws TasteException {
    PreferenceArray prefs = dataModel.getPreferencesFromUser(userId);
    Matrix confidenceMatrix = new SparseMatrix(dataModel.getNumItems(), dataModel.getNumItems());
    for (Preference pref : prefs) {
      long itemId = pref.getItemID();
      int itemIdx = itemIndex(itemId);
      confidenceMatrix.setQuick(itemIdx, itemIdx, 1);
    }
    return new DiagonalMatrix(confidenceMatrix);
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

    }
    return new DiagonalMatrix(confidenceMatrix);
  }

  private Matrix buildPreferenceVectorForItem(long realId) throws TasteException {
    Matrix ids = new SparseMatrix(1, dataModel.getNumUsers());
    for (Preference pref : dataModel.getPreferencesForItem(realId)) {
      int useridx = userIndex(pref.getUserID());
      ids.setQuick(0, useridx, pref.getValue());
    }
    return ids;
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

    }

    Preconditions.checkNotNull(scoresPerFeature);
    Preconditions.checkNotNull(scoresPerLabel);

    Matrix scoresPerLabelAndFeature = new SparseMatrix(scoresPerLabel.size(), scoresPerFeature.size());
    for (Pair<IntWritable,VectorWritable> entry : new SequenceFileDirIterable<IntWritable,VectorWritable>(
        new Path(base, TrainNaiveBayesJob.SUMMED_OBSERVATIONS), PathType.LIST, PathFilters.partFilter(), conf)) {
      scoresPerLabelAndFeature.assignRow(entry.getFirst().get(), entry.getSecond().get());
    }

    Vector perlabelThetaNormalizer = scoresPerLabel.like();
    /* for (Pair<Text,VectorWritable> entry : new SequenceFileDirIterable<Text,VectorWritable>(
        new Path(base, TrainNaiveBayesJob.THETAS), PathType.LIST, PathFilters.partFilter(), conf)) {
View Full Code Here

Examples of org.ar.redoexperiments.tools.SparseMatrix

     * Constant matrix, holding on column j the credit fraction each
     * superConcept of concept j gets from j, namely 1 / Lj, where Lj is the
     * total number of superConcepts of j; this means that concept j splits
     * its score equally amongst all its superConcepts.
     */
    S = new SparseMatrix(n);

    // assign a temporary unique id to each concept, for the purpose of
    // handling matrix h with appropriate indices
    int index = 0;
    for (OWLClass concept : v) {
View Full Code Here

Examples of org.fnlp.ml.types.sv.SparseMatrix

   * @param w
   * @param h
   * @return 误差
   */
  float computeObjective(SparseMatrix v, SparseMatrix w, SparseMatrix h) {
    SparseMatrix matrixWH = w.mutiplyMatrix(h);
    SparseMatrix diff = v.clone();
    diff.minus(matrixWH);
    return diff.l2Norm();

  }
View Full Code Here

Examples of org.fnlp.ml.types.sv.SparseMatrix

    int[] dimWH = { m, n };
    int[] dimVWH = { m, n };
    int[] dimHWVWH = { r, n };

    SparseMatrix matrixWH = new SparseMatrix(dimWH);
    SparseMatrix matrixVWH = new SparseMatrix(dimVWH);
    SparseMatrix matrixHWVWH = new SparseMatrix(dimHWVWH);
    matrixWH = w.mutiplyMatrix(h);

     TLongFloatIterator itV = v.vector.iterator();

     TLongFloatIterator itH = h.vector.iterator();
    for (int i = v.vector.size(); i-- > 0;) {
      itV.advance();
      matrixVWH.set(itV.key(),
          itV.value() / (matrixWH.elementAt(itV.key()) + eps));
    }

    SparseMatrix matrixTranW = w.trans();

    SparseMatrix matrixWVWH = matrixTranW.mutiplyMatrix(matrixVWH);
    for (int i = h.vector.size(); i-- > 0;) {
      itH.advance();
      matrixHWVWH.set(itH.key(),
          itH.value() * matrixWVWH.elementAt(itH.key()));
    }
    return matrixHWVWH;
  }
View Full Code Here

Examples of org.fnlp.ml.types.sv.SparseMatrix

  SparseMatrix updateW() {

    int[] dimVWH = { m, n };
    int[] dimWVWHH = { m, r };

    SparseMatrix matrixVWH = new SparseMatrix(dimVWH);
    SparseMatrix matrixWVWHH = new SparseMatrix(dimWVWHH);
    SparseMatrix matrixWH = w.mutiplyMatrix(h);
     TLongFloatIterator itV = v.vector.iterator();
    TLongFloatIterator itW = w.vector.iterator();
    for (int i = v.vector.size(); i-- > 0;) {
      itV.advance();
      matrixVWH.set(itV.key(),
          itV.value() / (matrixWH.elementAt(itV.key()) + eps));
    }
    SparseMatrix matrixTranH = h.trans();

    SparseMatrix matrixVWHH = matrixVWH.mutiplyMatrix(matrixTranH);
    for (int i = w.vector.size(); i-- > 0;) {
      itW.advance();
      matrixWVWHH.set(itW.key(),
          itW.value() * matrixVWHH.elementAt(itW.key()));
    }
    return matrixWVWHH;
  }
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.