Examples of SparseMatrix


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

    Matrix em = model.getEmissionMatrix();
    Matrix tr = model.getTransitionMatrix();
    // allocate the sparse data structures
    RandomAccessSparseVector sparseIp = new RandomAccessSparseVector(model
        .getNrOfHiddenStates());
    SparseMatrix sparseEm = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfOutputStates());
    SparseMatrix sparseTr = new SparseMatrix(model.getNrOfHiddenStates(), model.getNrOfHiddenStates());
    // now transfer the values
    for (int i = 0; i < model.getNrOfHiddenStates(); ++i) {
      double value = ip.getQuick(i);
      if (value > threshold) {
        sparseIp.setQuick(i, value);
      }
      for (int j = 0; j < model.getNrOfHiddenStates(); ++j) {
        value = tr.getQuick(i, j);
        if (value > threshold) {
          sparseTr.setQuick(i, j, value);
        }
      }

      for (int j = 0; j < model.getNrOfOutputStates(); ++j) {
        value = em.getQuick(i, j);
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.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

  @Test
  public void addLambdaTimesNuiTimesE() {
    int nui = 5;
    double lambda = 0.2;
    Matrix matrix = new SparseMatrix(5, 5);

    AlternatingLeastSquaresSolver.addLambdaTimesNuiTimesE(matrix, lambda, nui);

    for (int n = 0; n < 5; n++) {
      assertEquals(1.0, matrix.getQuick(n, n), EPSILON);
    }
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

    Matrix em = model.getEmissionMatrix();
    Matrix tr = model.getTransitionMatrix();
    // allocate the sparse data structures
    RandomAccessSparseVector sparseIp = new RandomAccessSparseVector(model
        .getNrOfHiddenStates());
    SparseMatrix sparseEm = new SparseMatrix(new int[]{
        model.getNrOfHiddenStates(), model.getNrOfOutputStates()});
    SparseMatrix sparseTr = new SparseMatrix(new int[]{
        model.getNrOfHiddenStates(), model.getNrOfHiddenStates()});
    // now transfer the values
    for (int i = 0; i < model.getNrOfHiddenStates(); ++i) {
      double value = ip.getQuick(i);
      if (value > threshold) {
        sparseIp.setQuick(i, value);
      }
      for (int j = 0; j < model.getNrOfHiddenStates(); ++j) {
        value = tr.getQuick(i, j);
        if (value > threshold) {
          sparseTr.setQuick(i, j, value);
        }
      }

      for (int j = 0; j < model.getNrOfOutputStates(); ++j) {
        value = em.getQuick(i, j);
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

      randomVectorValues.add(values);
      randomVectors.add(v);
    }
    vectors = new Vector[3][numVectors];
    clusters = new Vector[numClusters];
    clusterDistances = new SparseMatrix(numClusters, numClusters);
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

    return 0;
  }

  private Matrix readMatrix(Path dir) throws IOException {

    Matrix matrix = new SparseMatrix(new int[] { Integer.MAX_VALUE, Integer.MAX_VALUE });

    FileSystem fs = dir.getFileSystem(getConf());
    for (FileStatus seqFile : fs.globStatus(new Path(dir, "part-*"))) {
      Path path = seqFile.getPath();
      SequenceFile.Reader reader = null;
      try {
        reader = new SequenceFile.Reader(fs, path, getConf());
        IntWritable key = new IntWritable();
        VectorWritable value = new VectorWritable();
        while (reader.next(key, value)) {
          int row = key.get();
          Iterator<Vector.Element> elementsIterator = value.get().iterateNonZero();
          while (elementsIterator.hasNext()) {
            Vector.Element element = elementsIterator.next();
            matrix.set(row, element.index(), element.get());
          }
        }
      } finally {
        IOUtils.quietClose(reader);
      }
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

        labelCount = value.get().size();
      }
    }

    // read the class matrix
    Matrix matrix = new SparseMatrix(new int[] {labelCount, featureCount});
    for (Pair<IntWritable,VectorWritable> record
         : new SequenceFileIterable<IntWritable,VectorWritable>(classVectorPath, true, conf)) {
      IntWritable label = record.getFirst();
      VectorWritable value = record.getSecond();
      matrix.assignRow(label.get(), value.get());
    }
   
    model.setWeightMatrix(matrix);

    // read theta normalizer
View Full Code Here

Examples of org.apache.mahout.math.SparseMatrix

  @Test
  public void addLambdaTimesNuiTimesE() {
    int nui = 5;
    double lambda = 0.2;
    Matrix matrix = new SparseMatrix(new int[] { 5, 5 });

    solver.addLambdaTimesNuiTimesE(matrix, lambda, nui);

    for (int n = 0; n < 5; n++) {
      assertEquals(1.0, matrix.getQuick(n, n), EPSILON);
    }
  }
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.