List of usage examples for org.apache.mahout.math.hadoop DistributedRowMatrix iterateAll
@Override
public Iterator<MatrixSlice> iterateAll()
From source file:com.twitter.algebra.AlgebraCommon.java
License:Apache License
/*** * If the MapDir matrix is small, we can convert it to an in memory representation * and then run efficient centralized operations * //from w w w .j a v a 2 s . com * @param origMtx in MapDir format (generated by MatrixOutputFormat) * @return a dense matrix including the data * @throws IOException */ public static DenseMatrix toDenseMatrix(DistributedRowMatrix origMtx) throws IOException { MapDir mapDir = new MapDir(new Configuration(), origMtx.getRowPath()); DenseMatrix mtx = new DenseMatrix(origMtx.numRows(), origMtx.numCols()); Iterator<MatrixSlice> sliceIterator; try { sliceIterator = mapDir.iterateAll(); } catch (Exception e) { log.info(e.toString()); log.info("Input is not in matrix format, trying SequenceFileFormat instead ..."); sliceIterator = origMtx.iterateAll(); } while (sliceIterator.hasNext()) { MatrixSlice slice = sliceIterator.next(); // int r = slice.index(); // for (int c = 0; c < mtx.numCols(); c++) { // mtx.set(r, c, slice.get(c)); // } mtx.viewRow(slice.index()).assign(slice.vector()); } mapDir.close(); return mtx; }
From source file:com.twitter.algebra.matrix.format.MapDir.java
License:Apache License
public static void testIterator(DistributedRowMatrix origMtx, Path inPath) throws IOException { Configuration conf = new Configuration(); MapDir mapDir = new MapDir(conf, inPath); Iterator<MatrixSlice> sliceIterator = origMtx.iterateAll(); while (sliceIterator.hasNext()) { MatrixSlice slice = sliceIterator.next(); int index = slice.index(); System.out.println("A[" + index + "] = " + slice.vector()); IntWritable key = new IntWritable(index); VectorWritable vw = new VectorWritable(); vw = mapDir.get(key, vw);/*from w w w. jav a2 s .c o m*/ System.out.println("B[" + index + "] = " + vw); } mapDir.close(); }
From source file:org.qcri.pca.PCACommon.java
/*** * If the matrix is small, we can convert it to an in memory representation * and then run efficient centralized operations * /*from w w w. j av a 2 s .c om*/ * @param origMtx * @return a dense matrix including the data */ static DenseMatrix toDenseMatrix(DistributedRowMatrix origMtx) { DenseMatrix mtx = new DenseMatrix(origMtx.numRows(), origMtx.numCols()); Iterator<MatrixSlice> sliceIterator = origMtx.iterateAll(); while (sliceIterator.hasNext()) { MatrixSlice slice = sliceIterator.next(); mtx.viewRow(slice.index()).assign(slice.vector()); } return mtx; }