Example usage for org.apache.mahout.math Vector getElement

List of usage examples for org.apache.mahout.math Vector getElement

Introduction

In this page you can find the example usage for org.apache.mahout.math Vector getElement.

Prototype

Element getElement(int index);

Source Link

Document

Return an object of Vector.Element representing an element of this Vector.

Usage

From source file:at.illecker.hadoop.rootbeer.examples.matrixmultiplication.DistributedRowMatrix.java

License:Apache License

public int printDistributedRowMatrix() {
    System.out.println("RowPath: " + this.rowPath);
    Iterator<MatrixSlice> iterator = this.iterateAll();
    int count = 0;
    while (iterator.hasNext()) {
        MatrixSlice slice = iterator.next();
        Vector v = slice.vector();
        int size = v.size();
        for (int i = 0; i < size; i++) {
            Element e = v.getElement(i);
            count++;/*from   ww w.  j  ava 2  s  . co  m*/
            System.out.print(e.get() + " ");
        }
        System.out.println();
    }
    return count;
}

From source file:at.illecker.hadoop.rootbeer.examples.matrixmultiplication.DistributedRowMatrix.java

License:Apache License

public double[][] toDoubleArray() {
    final double[][] matrix = new double[this.numRows][this.numCols];
    Iterator<MatrixSlice> iterator = this.iterateAll();
    int i = 0;//from  w w  w .  ja  v a 2 s . com
    while (iterator.hasNext()) {
        Vector rowVector = iterator.next().vector();
        for (int j = 0; j < rowVector.size(); j++) {
            matrix[i][j] = rowVector.getElement(j).get();
        }
        i++;
    }
    return matrix;
}

From source file:at.illecker.hadoop.rootbeer.examples.matrixmultiplication.DistributedRowMatrix.java

License:Apache License

public boolean verify(DistributedRowMatrix other) {

    Iterator<MatrixSlice> iteratorThis = this.iterateAll();
    Iterator<MatrixSlice> iteratorOther = other.iterateAll();

    while (iteratorThis.hasNext()) {
        Vector thisVector = iteratorThis.next().vector();
        Vector otherVector = iteratorOther.next().vector();

        for (int j = 0; j < thisVector.size(); j++) {
            if (thisVector.getElement(j).get() != otherVector.getElement(j).get()) {
                // System.out.println("Verify failed!");
                // System.out.println("  Vector1: " + thisVector.toString());
                // System.out.println("  Vector2: " + otherVector.toString());
                return false;
            }/*from w ww.  j av  a  2  s . co  m*/
        }
    }
    return true;
}

From source file:at.illecker.hama.rootbeer.examples.matrixmultiplication.compositeinput.util.DistributedRowMatrix.java

License:Apache License

public boolean verify(DistributedRowMatrix other) {

    Iterator<MatrixSlice> iteratorThis = this.iterateAll();
    Iterator<MatrixSlice> iteratorOther = other.iterateAll();

    while (iteratorThis.hasNext()) {
        Vector thisVector = iteratorThis.next().vector();
        Vector otherVector = iteratorOther.next().vector();

        if (thisVector.size() != otherVector.size()) {
            return false;
        }/*  ww w  . j  a v a 2  s  . co m*/

        for (int j = 0; j < thisVector.size(); j++) {
            if (thisVector.getElement(j).get() != otherVector.getElement(j).get()) {
                // System.out.println("Verify failed!");
                // System.out.println("  Vector1: " + thisVector.toString());
                // System.out.println("  Vector2: " + otherVector.toString());
                return false;
            }
        }
    }
    return true;
}

From source file:com.chimpler.example.eigenface.Helper.java

License:Apache License

public static double[][] readMatrixSequenceFile(String fileName) throws Exception {
    Configuration configuration = new Configuration();
    FileSystem fs = FileSystem.get(configuration);
    Reader matrixReader = new SequenceFile.Reader(fs, new Path(fileName), configuration);

    List<double[]> rows = new ArrayList<double[]>();
    IntWritable key = new IntWritable();
    VectorWritable value = new VectorWritable();
    while (matrixReader.next(key, value)) {
        Vector vector = value.get();
        double[] row = new double[vector.size()];
        for (int i = 0; i < vector.getNumNondefaultElements(); i++) {
            Element element = vector.getElement(i);
            row[element.index()] = element.get();
        }//from w w w .  ja  va2 s.c  om
        rows.add(row);
    }
    return rows.toArray(new double[rows.size()][]);
}

From source file:nl.gridline.zieook.inx.movielens.items.ItemBasedSortSimilaritiesMapper.java

License:Apache License

@Override
protected void map(IntWritable key, VectorWritable value, Context context)
        throws IOException, InterruptedException {
    int maxIndex = -1;

    Vector similarityMatrixRow = value.get();
    /* remove self similarity */

    similarityMatrixRow.set(key.get(), Double.NEGATIVE_INFINITY);

    ///* w w w .j  a  va 2  s. co m*/
    // determine maximum index
    //
    Iterator<Element> it = similarityMatrixRow.iterateNonZero();

    while (it.hasNext()) {
        Element e = it.next();

        // e.index() // == item id

        if (e.index() > maxIndex) {
            maxIndex = e.index();
        }
    }

    // System.out.println(String.format("key: %d maxIndex: %d", key.get(), maxIndex));

    if (maxIndex > 0) {

        RecommendationElement[] itemBasedRecommendations = new RecommendationElement[maxIndex];

        for (int i = 0; i < maxIndex; i++) {
            Element element = similarityMatrixRow.getElement(i);

            double similarityValue = Double.NEGATIVE_INFINITY;

            if (element != null) {
                similarityValue = element.get();
            }

            itemBasedRecommendations[i] = new RecommendationElement(i, similarityValue);
        }

        Arrays.sort(itemBasedRecommendations, new SimilarityComparator());

        RecommendationElementArray array = new RecommendationElementArray(itemBasedRecommendations);

        context.write(new VarIntWritable(key.get()), array);

    }
}