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

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

Introduction

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

Prototype

boolean isSequentialAccess();

Source Link

Document

true if this implementation should be considered to be iterable in index order in an efficient way.

Usage

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

License:Apache License

static Matrix createRiIiMaybeTransposed(Vector ratingVector) {
    Preconditions.checkArgument(ratingVector.isSequentialAccess(),
            "Ratings should be iterable in Index or Sequential Order");

    double[][] RiIiMaybeTransposed = new double[ratingVector.getNumNondefaultElements()][1];
    int index = 0;
    for (Vector.Element elem : ratingVector.nonZeroes()) {
        RiIiMaybeTransposed[index++][0] = elem.get();
    }//from w  w w .  j a v a2s.  co  m
    return new DenseMatrix(RiIiMaybeTransposed, true);
}

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

License:Apache License

/** Y' (Cu - I) Y +  I */
private Matrix getYtransponseCuMinusIYPlusLambdaI(Vector userRatings) {
    Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");

    /* (Cu -I) Y */
    OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<Vector>(
            userRatings.getNumNondefaultElements());
    for (Element e : userRatings.nonZeroes()) {
        CuMinusIY.put(e.index(), Y.get(e.index()).times(confidence(e.get()) - 1));
    }//from  w w w .jav a2 s .  c om

    Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures);

    /* Y' (Cu -I) Y by outer products */
    for (Element e : userRatings.nonZeroes()) {
        for (Vector.Element feature : Y.get(e.index()).all()) {
            Vector partial = CuMinusIY.get(e.index()).times(feature.get());
            YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS);
        }
    }

    /* Y' (Cu - I) Y +  I  add lambda on the diagonal */
    for (int feature = 0; feature < numFeatures; feature++) {
        YtransponseCuMinusIY.setQuick(feature, feature,
                YtransponseCuMinusIY.getQuick(feature, feature) + lambda);
    }

    return YtransponseCuMinusIY;
}

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

License:Apache License

/** Y' Cu p(u) */
private Matrix getYtransponseCuPu(Vector userRatings) {
    Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");

    Vector YtransponseCuPu = new DenseVector(numFeatures);

    for (Element e : userRatings.nonZeroes()) {
        YtransponseCuPu.assign(Y.get(e.index()).times(confidence(e.get())), Functions.PLUS);
    }//from  w w  w .j  av a 2  s . c o  m

    return columnVectorAsMatrix(YtransponseCuPu);
}

From source file:com.skp.experiment.common.MathHelper.java

License:Apache License

public static String nice(Vector v) {
    if (!v.isSequentialAccess()) {
        v = new DenseVector(v);
    }//from w w  w  . ja  va2 s  .  c  om

    DecimalFormat df = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));

    StringBuilder buffer = new StringBuilder("[");
    String separator = "";
    for (Vector.Element e : v) {
        buffer.append(separator);
        if (Double.isNaN(e.get())) {
            buffer.append("  -  ");
        } else {
            if (e.get() >= 0) {
                buffer.append(' ');
            }
            buffer.append(df.format(e.get()));
        }
        separator = "\t";
    }
    buffer.append(" ]");
    return buffer.toString();
}

From source file:com.skp.experiment.math.als.hadoop.DistributedImplicitFeedbackAlternatingLeastSquaresSolver.java

License:Apache License

public Vector solve(Vector userRatings) throws IOException {
    Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");
    //Matrix sparseY = getSparseMatrix(userRatings);
    getSparseMatrix(userRatings);/*  w  w w .  java  2  s  . c  o  m*/
    /* Y' (Cu - I) Y +  I */
    /* Y' Cu p(u) */
    Vector YtransponseCuPu = new DenseVector(numFeatures);
    /* (Cu -I) Y */
    OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<Vector>();

    Iterator<Vector.Element> ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        CuMinusIY.put(e.index(), sparseY.get(e.index()).times(confidence(e.get()) - 1));
        /* Y' Cu p(u) */
        YtransponseCuPu.assign(sparseY.get(e.index()).times(confidence(e.get())), Functions.PLUS);
    }

    Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures);

    /* Y' (Cu -I) Y by outer products */
    ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        for (Vector.Element feature : sparseY.get(e.index())) {
            Vector partial = CuMinusIY.get(e.index()).times(feature.get());
            YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS);
        }
    }

    /* Y' (Cu - I) Y +  I  add lambda on the diagonal */
    for (int feature = 0; feature < numFeatures; feature++) {
        YtransponseCuMinusIY.setQuick(feature, feature,
                YtransponseCuMinusIY.getQuick(feature, feature) + lambda);
    }

    Matrix YtransposeCuPu = columnVectorAsMatrix(YtransponseCuPu);
    return solve(YtransposeY.plus(YtransponseCuMinusIY), YtransposeCuPu);
    //return YtransponseCuMinusIY;
}

From source file:com.skp.experiment.math.als.hadoop.ImplicitFeedbackAlternatingLeastSquaresReasonSolver.java

License:Apache License

/** Y' (Cu - I) Y +  I */
private Matrix YtransponseCuMinusIYPlusLambdaI(Vector userRatings) {
    Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");

    /* (Cu -I) Y */
    OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<Vector>();
    Iterator<Vector.Element> ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        CuMinusIY.put(e.index(), Y.get(e.index()).times(confidence(e.get()) - 1));
    }/*from   w w  w.java2 s. com*/

    Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures);

    /* Y' (Cu -I) Y by outer products */
    ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        for (Vector.Element feature : Y.get(e.index())) {
            Vector partial = CuMinusIY.get(e.index()).times(feature.get());
            YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS);
        }
    }

    /* Y' (Cu - I) Y +  I  add lambda on the diagonal */
    for (int feature = 0; feature < numFeatures; feature++) {
        YtransponseCuMinusIY.setQuick(feature, feature,
                YtransponseCuMinusIY.getQuick(feature, feature) + lambda);
    }

    return YtransponseCuMinusIY;
}

From source file:com.skp.experiment.math.als.hadoop.ImplicitFeedbackAlternatingLeastSquaresSolver.java

License:Apache License

/** Y' (Cu - I) Y +  I */
private Matrix YtransponseCuMinusIYPlusLambdaI(Vector userRatings) {
    Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");

    /* (Cu -I) Y */
    OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<Vector>();
    Iterator<Vector.Element> ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        CuMinusIY.put(e.index(), Y.viewRow(e.index()).times(confidence(e.get()) - 1));
        //CuMinusIY.put(e.index(), Y.get(e.index()).times(confidence(e.get()) - 1));
    }/*from  ww w .j  a v a2s.c o m*/

    Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures);

    /* Y' (Cu -I) Y by outer products */
    ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        //for (Vector.Element feature : Y.get(e.index())) {
        for (Vector.Element feature : Y.viewRow(e.index())) {
            Vector partial = CuMinusIY.get(e.index()).times(feature.get());
            YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS);
        }
    }

    /* Y' (Cu - I) Y +  I  add lambda on the diagonal */
    for (int feature = 0; feature < numFeatures; feature++) {
        YtransponseCuMinusIY.setQuick(feature, feature,
                YtransponseCuMinusIY.getQuick(feature, feature) + lambda);
    }

    return YtransponseCuMinusIY;
}

From source file:com.skp.experiment.math.als.hadoop.ImplicitFeedbackAlternatingLeastSquaresSolver.java

License:Apache License

/** Y' Cu p(u) */
private Matrix YtransponseCuPu(Vector userRatings) {
    Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");

    Vector YtransponseCuPu = new DenseVector(numFeatures);

    Iterator<Vector.Element> ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        //YtransponseCuPu.assign(Y.get(e.index()).times(confidence(e.get())), Functions.PLUS);
        YtransponseCuPu.assign(Y.viewRow(e.index()).times(confidence(e.get())), Functions.PLUS);
    }//  ww w  . ja  v a 2s . co  m

    return columnVectorAsMatrix(YtransponseCuPu);
}

From source file:com.ydy.cf.solver.impl.AlternatingLeastSquaresImplicitSolver.java

License:Apache License

/** Y' (Cu - I) Y +  I */
private Matrix YtransponseCuMinusIYPlusLambdaI(Vector userRatings) {
    Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");

    /* (Cu -I) Y */
    OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<Vector>();
    Iterator<Vector.Element> ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        Vector curYRow = Y.viewRow(e.index());
        CuMinusIY.put(e.index(), curYRow.times(confidence(e.get()) - 1));
    }//from  w w  w. java  2s . co m

    Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures);

    /* Y' (Cu -I) Y by outer products */
    ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        for (Vector.Element feature : Y.viewRow(e.index())) {
            Vector partial = CuMinusIY.get(e.index()).times(feature.get());
            YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS);
        }
    }

    /* Y' (Cu - I) Y +  I  add lambda on the diagonal */
    for (int feature = 0; feature < numFeatures; feature++) {
        YtransponseCuMinusIY.setQuick(feature, feature,
                YtransponseCuMinusIY.getQuick(feature, feature) + lambda);
    }

    return YtransponseCuMinusIY;
}

From source file:com.ydy.cf.solver.impl.AlternatingLeastSquaresImplicitSolver.java

License:Apache License

/** Y' Cu p(u) */
private Matrix YtransponseCuPu(Vector userRatings) {
    Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");

    Vector YtransponseCuPu = new DenseVector(numFeatures);

    Iterator<Vector.Element> ratings = userRatings.iterateNonZero();
    while (ratings.hasNext()) {
        Vector.Element e = ratings.next();
        Vector curYRow = Y.viewRow(e.index());
        YtransponseCuPu.assign(curYRow.times(confidence(e.get())), Functions.PLUS);
    }/*w  ww  . j  a  v a  2 s.c om*/

    return columnVectorAsMatrix(YtransponseCuPu);
}