Example usage for org.apache.mahout.math RandomAccessSparseVector iterator

List of usage examples for org.apache.mahout.math RandomAccessSparseVector iterator

Introduction

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

Prototype

@Override
    public Iterator<Element> iterator() 

Source Link

Usage

From source file:org.swjtu.helloworldcn.APCDriver.java

License:Apache License

/**
 * @param conf/*w  ww . jav a2  s  .  co  m*/
 *       the Configuration to be used
 * @param inputPath
 *       the Path to the input tuples directory
 * @param outputCalc
 *       the Path to the output directory
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws InterruptedException
 */
private void parallelUpdateRA(Configuration conf, Path inputPath, Path outputCalc)
        throws IOException, ClassNotFoundException, InterruptedException {

    Boolean dn = false;
    FileSystem fs = FileSystem.get(conf);

    int i = -1;
    Vector diagAplusR = null;
    while (!dn) {
        i = i + 1;
        Path outputPath = new Path(outputCalc, "parallelUpdateRA-" + i);
        if (i > 1) {
            HadoopUtil.delete(conf, new Path(outputCalc, "parallelUpdateRA-" + (i - 2)));
        }
        inputPath = APCParallelUpdateRAJob.runJob(inputPath, outputPath, numDims, lamda);

        diagAplusR = APCGetDiagAplusRJob.runJob(inputPath, numDims);
        //System.out.println("diag"+diagAplusR);
        RandomAccessSparseVector E = new RandomAccessSparseVector(numDims, 100);
        //RandomAccessSparseVector E = new RandomAccessSparseVector(diagAplusR);
        Iterator<Element> iter = diagAplusR.iterateNonZero();
        int K = 0;
        while (iter.hasNext()) {
            Element element = iter.next();
            if (element.get() > 0) {
                E.set(element.index(), 1.0);
                K++;
            } else {
                E.set(element.index(), 0.0);
            }
        }

        /*for (int c = 0; c < E.size(); c++) {
           if (E.get(c) > 0) {
              E.set(c, 1.0);
           } else {
              E.set(c, 0.0);
           }
        }*/

        //System.out.println("con is"+convits);
        int index = i % convits;
        if (i >= convits) {
            e.remove(index);
        }

        e.add(index, E);
        //System.out.println(e);

        //double K = E.zSum();
        if (i >= convits || i >= maxIterations) {
            RandomAccessSparseVector se = new RandomAccessSparseVector(numDims, 100);
            Iterator<RandomAccessSparseVector> iterator = e.iterator();
            while (iterator.hasNext()) {
                RandomAccessSparseVector v = iterator.next();
                se = (RandomAccessSparseVector) se.plus(v);
            }
            iter = se.iterateNonZero();
            while (iter.hasNext()) {
                Element element = iter.next();
                if (element.get() != convits) {
                    unconverged = true;
                    break;
                } else {
                    unconverged = false;
                }
            }
            if ((!unconverged && K > 0) || i == maxIterations) {
                dn = true;
            }

        }
    }
    //Get exemplars     
    exemplars = new ArrayList<Integer>();
    Path outputExemplasPath = new Path(new Path(outputCalc, "exemplars"), "result");
    IntWritable exValue = new IntWritable();
    SequenceFile.Writer writerExemplas = SequenceFile.createWriter(fs, conf, outputExemplasPath,
            NullWritable.get().getClass(), exValue.getClass());
    Iterator<Element> iter = diagAplusR.iterateNonZero();
    while (iter.hasNext()) {
        Element element = iter.next();
        if (element.get() > 0) {
            exemplars.add(element.index());
            exValue.set(element.index());
            writerExemplas.append(NullWritable.get(), exValue);
        }
    }
    /*for (int k = 0; k < numDims; k++) {
    if (diagAplusR.getQuick(k) > 0) {
        exemplars.add(k);
        exValue.set(k);
        writerExemplas.append(NullWritable.get(),exValue );
    }
    }*/
    writerExemplas.close();

    clusteringResult = APCGetClusteringResultJob.runJob(inputPath, outputExemplasPath, numDims);
    /*for (int j = 0; j< exemplars.size(); j++) {
       clusteringResult.set(exemplars.get(j), exemplars.get(j));          
    }*/
    //System.out.println("julei jieguo:"+clusteringResult);
}