List of usage examples for org.apache.mahout.math SequentialAccessSparseVector SequentialAccessSparseVector
public SequentialAccessSparseVector(SequentialAccessSparseVector other)
From source file:org.qcri.pca.MahoutCompatibilityTest.java
License:Apache License
@Test public void testMAHOUT_1238() throws IOException { Vector v = new SequentialAccessSparseVector(5); v.set(1, 3.0);/*from ww w.j ava2s . c o m*/ v.set(3, 5.0); Vector view = new VectorView(v, 0, v.size()); doTestVectorWritableEquals(view); }
From source file:org.qcri.sparkpca.FileFormat.java
public static void convertFromCooToSeq(String inputPath, int cardinality, int base, String outputFolderPath) { try {/*ww w .j a v a 2 s . c o m*/ final Configuration conf = new Configuration(); final FileSystem fs = FileSystem.get(conf); SequenceFile.Writer writer = null; final IntWritable key = new IntWritable(); final VectorWritable value = new VectorWritable(); Vector vector = null; String thisLine; int lineNumber = 0; int prevRowID = -1; boolean first = true; File[] filePathList = null; File inputFile = new File(inputPath); if (inputFile.isFile()) // if it is a file { filePathList = new File[1]; filePathList[0] = inputFile; } else { filePathList = inputFile.listFiles(); } if (filePathList == null) { log.error("The path " + inputPath + " does not exist"); return; } for (File file : filePathList) { BufferedReader br = new BufferedReader(new FileReader(file)); String outputFileName = outputFolderPath + File.separator + file.getName() + ".seq"; writer = SequenceFile.createWriter(fs, conf, new Path(outputFileName), IntWritable.class, VectorWritable.class, CompressionType.BLOCK); while ((thisLine = br.readLine()) != null) { // while loop begins here String[] splitted = thisLine.split(","); int rowID = Integer.parseInt(splitted[0]); int colID = Integer.parseInt(splitted[1]); double element = Double.parseDouble(splitted[2]); if (first) { first = false; vector = new SequentialAccessSparseVector(cardinality); } else if (rowID != prevRowID) { key.set(prevRowID); value.set(vector); //System.out.println(vector); writer.append(key, value);//write last row vector = new SequentialAccessSparseVector(cardinality); } prevRowID = rowID; vector.set(colID - base, element); } } if (writer != null) //append last vector in last file { key.set(prevRowID); value.set(vector); writer.append(key, value);//write last row writer.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.swjtu.helloworldcn.APCMatrixInputReducer.java
License:Apache License
@Override protected void reduce(IntWritable row, Iterable<APCMatrixEntryWritable> values, Context context) throws IOException, InterruptedException { int size = context.getConfiguration().getInt(APCMatrixInputJob.MATRIX_DIMENSIONS, Integer.MAX_VALUE); RandomAccessSparseVector outS = new RandomAccessSparseVector(size, 100); RandomAccessSparseVector outA = new RandomAccessSparseVector(size, 100); RandomAccessSparseVector outR = new RandomAccessSparseVector(size, 100); Configuration conf = context.getConfiguration(); if (preference == null) { preference = Double.parseDouble(conf.get(APCMatrixInputJob.TEMPORARY_SAVE_PREFERENCE)); }//ww w . ja v a 2s .c om //System.out.println("pian du"+preference); for (APCMatrixEntryWritable element : values) { outS.setQuick(element.getCol(), element.getValS()); outA.setQuick(element.getCol(), 0.0); outR.setQuick(element.getCol(), 0.0); } //Place preferences on the diagonal of S outS.setQuick(row.get(), preference); SequentialAccessSparseVector outputS = new SequentialAccessSparseVector(outS); SequentialAccessSparseVector outputA = new SequentialAccessSparseVector(outA); SequentialAccessSparseVector outputR = new SequentialAccessSparseVector(outR); APCRowVectorWritable rowVectorWritable = new APCRowVectorWritable(outputA, outputR, outputS); //System.out.println(outputS); context.write(row, rowVectorWritable); }