List of usage examples for org.apache.mahout.math.hadoop.similarity.cooccurrence Vectors maybeSample
public static Vector maybeSample(Vector original, int sampleSize)
From source file:com.pocketx.gravity.recommender.cf.similarity.mapreduce.ToItemVectorsMapper.java
License:Apache License
@Override protected void map(VarLongWritable rowIndex, VectorWritable vectorWritable, Context ctx) throws IOException, InterruptedException { Vector userRatings = vectorWritable.get(); int numElementsBeforeSampling = userRatings.getNumNondefaultElements(); userRatings = Vectors.maybeSample(userRatings, sampleSize); int numElementsAfterSampling = userRatings.getNumNondefaultElements(); int column = TasteHadoopUtils.idToIndex(rowIndex.get()); VectorWritable itemVector = new VectorWritable(new RandomAccessSparseVector(Integer.MAX_VALUE, 1)); itemVector.setWritesLaxPrecision(true); ////ww w . j a v a 2 s .c o m Iterator<Vector.Element> iterator = userRatings.nonZeroes().iterator(); // while (iterator.hasNext()) { Vector.Element elem = iterator.next(); itemVector.get().setQuick(column, elem.get()); ctx.write(new IntWritable(elem.index()), itemVector); } ctx.getCounter(Elements.USER_RATINGS_USED).increment(numElementsAfterSampling); ctx.getCounter(Elements.USER_RATINGS_NEGLECTED) .increment(numElementsBeforeSampling - numElementsAfterSampling); }
From source file:org.hf.mls.mahout.cf.taste.hadoop.preparation.ToItemVectorsMapper.java
License:Apache License
@Override protected void map(VarLongWritable rowIndex, VectorWritable vectorWritable, Context ctx) throws IOException, InterruptedException { Vector userRatings = vectorWritable.get(); int numElementsBeforeSampling = userRatings.getNumNondefaultElements(); userRatings = Vectors.maybeSample(userRatings, sampleSize); int numElementsAfterSampling = userRatings.getNumNondefaultElements(); int column = TasteHadoopUtils.idToIndex(rowIndex.get()); itemVectorWritable.setWritesLaxPrecision(true); Vector itemVector = new RandomAccessSparseVector(Integer.MAX_VALUE, 1); for (Vector.Element elem : userRatings.nonZeroes()) { itemID.set(elem.index());//from www.j a v a2 s. c o m itemVector.setQuick(column, elem.get()); itemVectorWritable.set(itemVector); ctx.write(itemID, itemVectorWritable); // reset vector for reuse itemVector.setQuick(elem.index(), 0.0); } ctx.getCounter(Elements.USER_RATINGS_USED).increment(numElementsAfterSampling); ctx.getCounter(Elements.USER_RATINGS_NEGLECTED) .increment(numElementsBeforeSampling - numElementsAfterSampling); }