Example usage for org.apache.mahout.cf.taste.hadoop TasteHadoopUtils idToIndex

List of usage examples for org.apache.mahout.cf.taste.hadoop TasteHadoopUtils idToIndex

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.hadoop TasteHadoopUtils idToIndex.

Prototype

public static int idToIndex(long id) 

Source Link

Document

Maps a long to an int with range of 0 to Integer.MAX_VALUE-1

Usage

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);
    ////from www. ja 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.item.ToUserVectorsReducer.java

License:Apache License

@Override
protected void reduce(VarLongWritable userID, Iterable<VarLongWritable> itemPrefs, Context context)
        throws IOException, InterruptedException {
    Vector userVector = new RandomAccessSparseVector(Integer.MAX_VALUE, 100);
    for (VarLongWritable itemPref : itemPrefs) {
        int index = TasteHadoopUtils.idToIndex(itemPref.get());
        float value = itemPref instanceof EntityPrefWritable ? ((EntityPrefWritable) itemPref).getPrefValue()
                : 1.0f;//from w w w . j  ava  2 s  . c  o  m
        userVector.set(index, value);
    }

    if (userVector.getNumNondefaultElements() >= minPreferences) {
        userVectorWritable.set(userVector);
        userVectorWritable.setWritesLaxPrecision(true);
        context.getCounter(Counters.USERS).increment(1);
        context.write(userID, userVectorWritable);
    }
}

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());/*  w  ww.j  av a 2s .c om*/
        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);
}