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

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

Introduction

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

Prototype

public Vector assign(DenseVector vector) 

Source Link

Usage

From source file:edu.indiana.d2i.htrc.vecproj.RandomProjection.java

License:Apache License

public RandomProjection(int originalDim /* d */, int reducedDim /* k */) {
    this.originalDim = originalDim;
    this.reducedDim = reducedDim;

    // generate random matrix, use a O(kd) algorithm [Achlioptas, 2000]
    random = Functions.random();/*from ww  w . j  a va  2  s  .  c  o m*/
    DoubleFunctionWrapper randomWrapper = new DoubleFunctionWrapper();
    basis = new DenseMatrix(reducedDim, originalDim);
    for (int i = 0; i < reducedDim; i++) {
        DenseVector projection = new DenseVector(originalDim);
        projection.assign(randomWrapper);
        basis.assignRow(i, projection);
    }
}

From source file:edu.indiana.d2i.htrc.vecproj.TestRandomProjection.java

License:Apache License

@Test
public void testProject() {
    long t0 = System.nanoTime();
    random = Functions.random();//from w ww .  j a va2s .  c  om
    for (int i = 0; i < 100; i++) { // test 100 times
        DenseVector vector = new DenseVector(originalDim);
        vector.assign(random);
        Vector projected = projector.project(vector);
        Assert.assertEquals(reducedDim, projected.size());
    }
    long t1 = System.nanoTime();
    System.out.println("elapsed " + ((double) (t1 - t0)) / 1e9);
}

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

/**
 * Computes the column-wise mean and span of a DistributedRowMatrix
 * @throws IOException /*w  w  w.  j av  a2 s .c o  m*/
 * 
 */
public Path compuateMeanAndSpan(Path inputPath, Path outputPath, DenseVector resMean, boolean normalizeMean,
        Configuration conf, String id) throws IOException {
    Path meanSpanDirPath = new Path(outputPath, "meanAndSpan" + id);
    FileSystem fs = FileSystem.get(inputPath.toUri(), conf);
    meanSpanDirPath = fs.makeQualified(meanSpanDirPath);
    if (!fs.exists(meanSpanDirPath)) {
        Path rowPath = fs.makeQualified(inputPath);
        run(conf, rowPath, meanSpanDirPath);
    } else {
        log.warn("--------- Skip MeanAndSpanJob - already exists" + meanSpanDirPath);
    }
    Path meanSpanPath = getMeanSpanPath(meanSpanDirPath);
    loadResults(meanSpanPath, normalizeMean, conf);
    resMean.assign(getMeanVector());
    return meanSpanPath;
}

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 ww  w. j a  va2 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);

}

From source file:root.input.SimilarityMatrixConverter.java

License:Apache License

/**
 * This method allows the Job to act as a {@link ToolRunner} and 
 * interface properly with the Driver./*from  ww  w .  j av a 2 s. c  om*/
 * 
 * @param args Configuration arguments
 * @return Exit status
 * @see ToolRunner
 */
@Override
public int run(String[] args) throws Exception {

    addArguments();

    if (parseArguments(args) == null) {
        return -1;
    }

    initArguments();

    Configuration conf = getConf();

    int numLevels = Integer.valueOf(levels);

    URI workingURI = new URI(conf.get("fs.default.name"));
    URI inputURI = new URI(inputSimMat);

    FileSystem workingFS = FileSystem.get(workingURI, conf);
    FileSystem inputFS = FileSystem.get(inputURI, conf);

    Path in = new Path(inputSimMat);
    Path out = new Path(outputDirectory + "/similarityMatrix");

    Scanner reader = new Scanner(new BufferedReader(new InputStreamReader(inputFS.open(in))));

    SequenceFile.Writer writer = new SequenceFile.Writer(workingFS, conf, out, Text.class, Text.class);

    int counter = 0;

    int N = 0;

    Text outSKey = new Text();
    Text outRKey = new Text();
    Text outAKey = new Text();
    Text outCKey = new Text();
    Text outTKey = new Text();
    Text outPKey = new Text();

    while (reader.hasNext()) {

        String line = reader.nextLine();
        String[] vecLine = line.split(",");
        N = vecLine.length;
        double[] vecVal = new double[N];

        for (int i = 0; i < N; i++) {
            vecVal[i] = Double.valueOf(vecLine[i]);
        }

        DenseVector vectorS = new DenseVector(vecVal);
        DenseVector out0VecN = new DenseVector(N);
        out0VecN.assign(0.0);
        DenseVector out0Vec1 = new DenseVector(1);
        out0Vec1.assign(0.0);
        DenseVector outTVec = new DenseVector(1);
        outTVec.assign(TAU_INIT);

        VectorWritable vectorWritable = new VectorWritable(vectorS);
        VectorWritable out0VecNWritable = new VectorWritable(out0VecN);
        VectorWritable out0Vec1Writable = new VectorWritable(out0Vec1);
        VectorWritable outTVecWritable = new VectorWritable(outTVec);

        outSKey = new Text(counter + "\t0\tS");
        writer.append(outSKey, vectorWritable);

        for (int i = 0; i < numLevels; i++) {
            outRKey = new Text(counter + "\t" + i + "\tR");
            outAKey = new Text(counter + "\t" + i + "\tA");
            writer.append(outRKey, out0VecNWritable);
            writer.append(outAKey, out0VecNWritable);
        }

        for (int i = 0; i < numLevels; i++) {
            outCKey = new Text(counter + "\t" + i + "\tC");
            outTKey = new Text(counter + "\t" + i + "\tT");
            outPKey = new Text(counter + "\t" + i + "\tP");
            writer.append(outCKey, out0Vec1Writable);
            writer.append(outTKey, outTVecWritable);
            writer.append(outPKey, out0Vec1Writable);
        }

        counter++;

    }

    reader.close();
    writer.close();

    return 0;

}

From source file:root.input.SimilarityMatrixConverterJob.java

License:Apache License

/**
 * {@inheritDoc}/*from  w w w.jav  a 2 s .c  o  m*/
 */
@Override
public int run(String[] args) throws Exception {

    constructParameterList();

    if (parseArguments(args) == null) {
        return -1;
    }

    initializeConfigurationParameters();

    printJobHeader();

    Configuration conf = getConf();

    int numLevels = Integer.valueOf(levels);

    URI workingURI = new URI(conf.get("fs.default.name"));
    URI inputURI = new URI(inputSimMat);

    FileSystem workingFS = FileSystem.get(workingURI, conf);
    FileSystem inputFS = FileSystem.get(inputURI, conf);

    Path in = new Path(inputSimMat);
    Path out = new Path(outputDirectory + "/part-m-00000");

    Scanner reader = new Scanner(new BufferedReader(new InputStreamReader(inputFS.open(in))));

    SequenceFile.Writer writer = new SequenceFile.Writer(workingFS, conf, out, Text.class,
            VectorWritable.class);

    int counter = 0;

    int N = 0;

    Text outSKey = new Text();
    Text outRKey = new Text();
    Text outAKey = new Text();
    Text outCKey = new Text();
    Text outTKey = new Text();
    Text outPKey = new Text();

    while (reader.hasNext()) {

        String line = reader.nextLine();
        String[] vecLine = line.split(",");
        N = vecLine.length;
        double[] vecVal = new double[N];

        for (int i = 0; i < N; i++) {
            vecVal[i] = Double.valueOf(vecLine[i]);
        }

        DenseVector vectorS = new DenseVector(vecVal);
        DenseVector out0VecN = new DenseVector(N);
        out0VecN.assign(0.0);
        DenseVector out0Vec1 = new DenseVector(1);
        out0Vec1.assign(0.0);
        DenseVector outTVec = new DenseVector(1);
        outTVec.assign(TAU_INIT);

        VectorWritable vectorWritable = new VectorWritable(vectorS);
        VectorWritable out0VecNWritable = new VectorWritable(out0VecN);
        VectorWritable out0Vec1Writable = new VectorWritable(out0Vec1);
        VectorWritable outTVecWritable = new VectorWritable(outTVec);

        outSKey = new Text(counter + "\t0\tS");
        writer.append(outSKey, vectorWritable);

        for (int i = 0; i < numLevels; i++) {
            outRKey = new Text(counter + "\t" + i + "\tR");
            outAKey = new Text(counter + "\t" + i + "\tA");
            writer.append(outRKey, out0VecNWritable);
            writer.append(outAKey, out0VecNWritable);
        }

        for (int i = 0; i < numLevels; i++) {
            outCKey = new Text(counter + "\t" + i + "\tC");
            outTKey = new Text(counter + "\t" + i + "\tT");
            outPKey = new Text(counter + "\t" + i + "\tP");
            writer.append(outCKey, out0Vec1Writable);
            writer.append(outTKey, outTVecWritable);
            writer.append(outPKey, out0Vec1Writable);
        }

        counter++;

    }

    reader.close();
    writer.close();

    return 0;

}

From source file:root.input.util.SimilarityMatrixMapper.java

License:Apache License

@Override
protected void map(WritableComparable<?> key, VectorWritable value, Context context)
        throws IOException, InterruptedException {

    smatDiagScale = context.getConfiguration().getLong("diagScale", -1);
    int levels = context.getConfiguration().getInt("numLevels", -1);

    String keyName = key.toString().substring(1);
    Vector valVec = value.get();//from  w  w  w  . j a  v  a  2s .  co m

    int N = seedVectors.size();

    DenseVector outSVec = new DenseVector(N);
    outSVec.assign(0.0);
    DenseVector out0Vec = new DenseVector(N);
    out0Vec.assign(0.0);

    Text outSKey = new Text(keyName + "\t0\tS");
    Text outRKey = new Text();
    Text outAKey = new Text();

    for (NamedVector seedVector : seedVectors) {
        double distance = measure.distance(seedVector, valVec);

        String seedVectorName = seedVector.getName().substring(1);
        int seedVectorPos = Integer.valueOf(seedVectorName);

        if (keyName.equals(seedVectorName)) {
            double diagValue = generateSMatDiagValue(smatDiagScale);
            outSVec.set(seedVectorPos, diagValue);
        } else {
            outSVec.set(seedVectorPos, -1 * distance);
        }
    }

    VectorWritable outSVecWritable = new VectorWritable(outSVec);

    context.write(outSKey, outSVecWritable);

    VectorWritable out0VecWritable = new VectorWritable(out0Vec);

    for (int i = 0; i < levels; i++) {
        outRKey = new Text(keyName + "\t" + i + "\tR");
        outAKey = new Text(keyName + "\t" + i + "\tA");
        context.write(outRKey, out0VecWritable);
        context.write(outAKey, out0VecWritable);
    }

    Text outCKey = new Text();
    Text outTKey = new Text();
    Text outPKey = new Text();

    out0Vec = new DenseVector(1);
    out0Vec.assign(0.0);
    DenseVector outTVec = new DenseVector(1);
    //      outTVec.assign(Double.MAX_VALUE / 2);
    outTVec.assign(TAU_INIT);

    out0VecWritable = new VectorWritable(out0Vec);
    VectorWritable outTVecWritable = new VectorWritable(outTVec);

    for (int i = 0; i < levels; i++) {
        outCKey = new Text(keyName + "\t" + i + "\tC");
        outTKey = new Text(keyName + "\t" + i + "\tT");
        outPKey = new Text(keyName + "\t" + i + "\tP");
        context.write(outCKey, out0VecWritable);
        context.write(outTKey, outTVecWritable);
        context.write(outPKey, out0VecWritable);
    }

}