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

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

Introduction

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

Prototype

int getNumNondefaultElements();

Source Link

Document

Return the number of values in the recipient which are not the default value.

Usage

From source file:org.gpfvic.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;// ww w . jav a  2s . 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.gpfvic.mahout.cf.taste.hadoop.item.UserVectorSplitterMapper.java

License:Apache License

private Vector maybePruneUserVector(Vector userVector) {
    if (userVector.getNumNondefaultElements() <= maxPrefsPerUserConsidered) {
        return userVector;
    }/*from w w w.  j  a  va2s.c  o m*/

    float smallestLargeValue = findSmallestLargeValue(userVector);

    // "Blank out" small-sized prefs to reduce the amount of partial products
    // generated later. They're not zeroed, but NaN-ed, so they come through
    // and can be used to exclude these items from prefs.
    for (Element e : userVector.nonZeroes()) {
        float absValue = Math.abs((float) e.get());
        if (absValue < smallestLargeValue) {
            e.set(Float.NaN);
        }
    }

    return userVector;
}

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 .ja 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);
}