Example usage for org.apache.mahout.math DenseVector minus

List of usage examples for org.apache.mahout.math DenseVector minus

Introduction

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

Prototype

@Override
    public Vector minus(Vector that) 

Source Link

Usage

From source file:org.qcri.pca.ReconstructionErrJob.java

/**
 * Refer to {@link ReconstructionErrJob} for explanation of the job. In short:
 * /*  w ww  .ja va  2 s  . c  o m*/
 * X = Y * Y2X
 * 
 * Err = (X - Xm) * C' - (Y - Ym)
 * 
 * @param matrixY
 *          the input matrix Y
 * @param matrixY2X
 *          the in-memory matrix to generate X
 * @param matrixC
 *          the in-memory matrix to reconstruct Y
 * @param C_central
 *          the central version of matrixC
 * @param Ym
 *          the mean vector of Y
 * @param Xm
 *          = Ym * matrixY2X
 * @param conf
 *          the configuration
 * @param tmpPath
 *          the temporary path
 * @param id
 *          the unique id to name the files in HDFS
 * @return the norm-2 of the the Err matrix 
 * @throws IOException
 * @throws InterruptedException
 * @throws ClassNotFoundException
 */
public double reconstructionErr(DistributedRowMatrix matrixY, DistributedRowMatrix matrixY2X,
        DistributedRowMatrix matrixC, Matrix C_central, Vector Ym, DenseVector Xm, final float ERR_SAMPLE_RATE,
        Configuration conf, Path tmpPath, String id)
        throws IOException, InterruptedException, ClassNotFoundException {
    DenseVector Zm = new DenseVector(C_central.numRows());
    PCACommon.vectorTimesMatrixTranspose(Xm, (DenseMatrix) C_central, Zm);
    Zm = (DenseVector) Zm.minus(Ym);

    Path resPath = new Path(tmpPath, "reconstructionErr" + id);
    FileSystem fs = FileSystem.get(resPath.toUri(), conf);
    if (!fs.exists(resPath)) {
        Path ZmPath = PCACommon.toDistributedVector(Zm, tmpPath, "Zm" + id, conf);
        Path YmPath = PCACommon.toDistributedVector(Ym, tmpPath, "Ymforerr" + id, conf);
        run(conf, matrixY.getRowPath(), matrixY2X.getRowPath(), matrixY2X.numRows(), matrixY2X.numCols(),
                matrixC.getRowPath(), ZmPath.toString(), YmPath.toString(), resPath, ERR_SAMPLE_RATE);
    } else {
        log.warn("---------- Skip ReconstructionErrJob - already exists: " + resPath);
    }
    loadResults(resPath, conf);

    log.info("0 is reconstruction err, 1 is Y norm (err/norm), " + "2 is Y-Ym norm (err/norm)");
    log.info("The error of 0 is " + reconstructionError);
    log.info("The error of 1 is " + yNorm + " (" + reconstructionError / yNorm + ")");
    log.info("The error of 2 is " + centralizedYNorm + " (" + reconstructionError / centralizedYNorm + ")");
    double error = reconstructionError / centralizedYNorm;
    return error;
}

From source file:org.qcri.pca.SSVDErrDriver.java

private void run(Configuration conf, Path input, Path vPath, Path output) throws Exception {
    int round = 0;
    DistributedRowMatrix Ye = new DistributedRowMatrix(input, getTempPath(), 1, D);
    Ye.setConf(conf);//from  w w w  .j  a v  a  2  s  . c om
    DistributedRowMatrix V = new DistributedRowMatrix(vPath, getTempPath(), D, d);
    V.setConf(conf);
    DenseMatrix V_central = PCACommon.toDenseMatrix(V);

    DenseVector Ym = new DenseVector(Ye.numCols());
    MeanAndSpanJob masJob = new MeanAndSpanJob();
    masJob.compuateMeanAndSpan(Ye.getRowPath(), output, Ym, MeanAndSpanJob.NORMALIZE_MEAN, conf, "" + round);

    DenseVector Xm = new DenseVector(d);
    PCACommon.denseVectorTimesMatrix(Ym, V_central, Xm);
    DenseVector Zm = new DenseVector(D);
    PCACommon.vectorTimesMatrixTranspose(Xm, V_central, Zm);
    Zm = (DenseVector) Zm.minus(Ym);
    ReconstructionErrJob errJob = new ReconstructionErrJob();
    errJob.reconstructionErr(Ye, V, V, V_central, Ym, Xm, ERR_SAMPLE_RATE, conf, getTempPath(), "" + round);
}

From source file:root.hap.availability.AvailabilityReducer.java

License:Apache License

private void updateAvailability(Context context, DenseVector A, DenseVector R, DenseVector S, DenseVector P,
        DenseVector C, int reducerColNum, int reducerLevelNum, int N, String availability)
        throws IOException, InterruptedException {

    DenseVector oldA = A.clone();/*from   www  .jav  a  2  s.  com*/

    // get positive values into RPositive
    DenseVector RPositive = R.clone();
    for (int rowNum = 0; rowNum < N; rowNum++) {
        double RValue = RPositive.get(rowNum);
        if (RValue < 0) {
            RPositive.setQuick(rowNum, 0);
        }
    }

    // reset diagonal value from R
    RPositive.setQuick(reducerColNum, R.get(reducerColNum));

    // sum R Positive values
    double RPSum = RPositive.zSum();

    double CVal = C.get(0);
    double PVal = P.get(0);

    double CHat = CVal + PVal;

    // sum together CHat and RPSum into a vector
    A.assign(CHat + RPSum);

    A = (DenseVector) A.minus(RPositive);

    for (int rowNum = 0; rowNum < N; rowNum++) {

        if (rowNum == reducerColNum) {
            continue;
        }

        double value = A.get(rowNum) < 0 ? A.get(rowNum) : 0;

        A.setQuick(rowNum, value);
    }

    double lambda = context.getConfiguration().getFloat("lambda", 0);
    A = (DenseVector) A.times(1 - lambda);
    oldA = (DenseVector) oldA.times(lambda);
    A = (DenseVector) A.plus(oldA);

    VectorWritable AWritable = new VectorWritable(A);

    context.write(new Text(reducerColNum + "\t" + reducerLevelNum + "\t" + availability), AWritable);

}