Example usage for org.apache.mahout.math SequentialAccessSparseVector SequentialAccessSparseVector

List of usage examples for org.apache.mahout.math SequentialAccessSparseVector SequentialAccessSparseVector

Introduction

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

Prototype

private SequentialAccessSparseVector(int cardinality, OrderedIntDoubleMapping values) 

Source Link

Usage

From source file:com.innometrics.integration.app.recommender.ml.als.ALSWRFactorizer.java

License:Apache License

protected Vector sparseItemRatingVector(PreferenceArray prefs) {
    SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(Integer.MAX_VALUE, prefs.length());
    for (Preference preference : prefs) {
        ratings.set(userIndex(preference.getUserID()), preference.getValue());
    }/*w w  w  .j  a  v  a 2 s . c o m*/
    return ratings;
}

From source file:com.innometrics.integration.app.recommender.ml.als.ALSWRFactorizer.java

License:Apache License

protected Vector sparseUserRatingVector(PreferenceArray prefs) {
    SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(Integer.MAX_VALUE, prefs.length());
    for (Preference preference : prefs) {
        ratings.set(itemIndex(preference.getItemID()), preference.getValue());
    }//  w w  w  .  j ava2 s  .  co  m
    return ratings;
}

From source file:com.mozilla.grouperfish.transforms.coclustering.pig.storage.MahoutVectorStorage.java

License:Apache License

@Override
public void putNext(Tuple t) throws IOException {
    IntWritable outputKey = new IntWritable();
    VectorWritable outputValue = new VectorWritable();
    outputKey.set((Integer) t.get(0));
    Tuple currRow = (Tuple) t.get(1);//w ww .  j  av a  2s.  c  o m
    Vector currRowVector;
    if (dimensions == 0) {
        throw new IllegalArgumentException("Trying to create 0 dimension vector");
    }
    if (STORE_AS_DENSE) {
        currRowVector = new NamedVector(new DenseVector(dimensions), outputKey.toString());
    } else if (STORE_AS_SEQUENTIAL) {
        currRowVector = new NamedVector(new SequentialAccessSparseVector(dimensions, currRow.size()),
                outputKey.toString());
    } else {
        currRowVector = new NamedVector(new RandomAccessSparseVector(dimensions, currRow.size()),
                outputKey.toString());
    }
    for (int ii = 0; ii < currRow.size(); ii++) {
        Object o = currRow.get(ii);
        switch (currRow.getType(ii)) {
        case DataType.INTEGER:
        case DataType.LONG:
        case DataType.FLOAT:
        case DataType.DOUBLE:
            currRowVector.set(ii, (Double) o);
            break;
        case DataType.TUPLE:
            // If this is a tuple then we want to set column and element
            Tuple subt = (Tuple) o;
            currRowVector.set((Integer) subt.get(0), (Double) subt.get(1));
            break;
        default:
            throw new RuntimeException("Unexpected tuple form");
        }
    }
    outputValue.set(currRowVector);
    try {
        writer.write(outputKey, outputValue);
    } catch (InterruptedException e) {
        LOG.error("Interrupted while writing", e);
    }
}

From source file:edu.rosehulman.mahout.math.VectorWritable.java

License:Apache License

@Override
public void readFields(DataInput in) throws IOException {
    int flags = in.readByte();
    //Preconditions.checkArgument(flags >> NUM_FLAGS == 0, "Unknown flags set: %d", Integer.toString(flags, 2));
    boolean dense = (flags & FLAG_DENSE) != 0;
    boolean sequential = (flags & FLAG_SEQUENTIAL) != 0;
    boolean named = (flags & FLAG_NAMED) != 0;
    boolean laxPrecision = (flags & FLAG_LAX_PRECISION) != 0;

    int size = Varint.readUnsignedVarInt(in);
    Vector v;/*w  ww.  j  a  v a2  s  .  c  om*/
    if (dense) {
        double[] values = new double[size];
        for (int i = 0; i < size; i++) {
            values[i] = laxPrecision ? in.readFloat() : in.readDouble();
        }
        v = new DenseVector(values);
    } else {
        int numNonDefaultElements = Varint.readUnsignedVarInt(in);
        v = sequential ? new SequentialAccessSparseVector(size, numNonDefaultElements)
                : new RandomAccessSparseVector(size, numNonDefaultElements);
        if (sequential) {
            int lastIndex = 0;
            for (int i = 0; i < numNonDefaultElements; i++) {
                int delta = Varint.readUnsignedVarInt(in);
                int index = lastIndex + delta;
                lastIndex = index;
                double value = laxPrecision ? in.readFloat() : in.readDouble();
                v.setQuick(index, value);
            }
        } else {
            for (int i = 0; i < numNonDefaultElements; i++) {
                int index = Varint.readUnsignedVarInt(in);
                double value = laxPrecision ? in.readFloat() : in.readDouble();
                v.setQuick(index, value);
            }
        }
    }
    if (named) {
        String name = in.readUTF();
        v = new NamedVector(v, name);
    }
    vector = v;
}

From source file:org.codelibs.elasticsearch.taste.recommender.svd.ALSWRFactorizer.java

License:Apache License

protected Vector sparseItemRatingVector(final PreferenceArray prefs) {
    final SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(Integer.MAX_VALUE,
            prefs.length());/*from  w w  w .j  av a 2 s .  co  m*/
    for (final Preference preference : prefs) {
        ratings.set((int) preference.getUserID(), preference.getValue());
    }
    return ratings;
}

From source file:org.codelibs.elasticsearch.taste.recommender.svd.ALSWRFactorizer.java

License:Apache License

protected Vector sparseUserRatingVector(final PreferenceArray prefs) {
    final SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(Integer.MAX_VALUE,
            prefs.length());/*from w  w  w . j a v  a 2  s.  c o  m*/
    for (final Preference preference : prefs) {
        ratings.set((int) preference.getItemID(), preference.getValue());
    }
    return ratings;
}

From source file:org.plista.kornakapi.core.optimizer.ErrorALSWRFactorizer.java

License:Apache License

protected Vector sparseItemRatingVector(PreferenceArray prefs) {
    SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(Integer.MAX_VALUE, prefs.length());
    for (Preference preference : prefs) {
        ratings.set((int) preference.getUserID(), preference.getValue());
    }/*w  ww . j av a 2 s.  c  o m*/
    return ratings;
}

From source file:org.plista.kornakapi.core.optimizer.ErrorALSWRFactorizer.java

License:Apache License

protected Vector sparseUserRatingVector(PreferenceArray prefs) {
    SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(Integer.MAX_VALUE, prefs.length());
    for (Preference preference : prefs) {
        ratings.set((int) preference.getItemID(), preference.getValue());
    }/* www  .j av a2s . c  o m*/
    return ratings;
}