Example usage for org.apache.mahout.math Matrix get

List of usage examples for org.apache.mahout.math Matrix get

Introduction

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

Prototype

double get(String rowLabel, String columnLabel);

Source Link

Document

Return the value at the given labels

Usage

From source file:com.cloudera.knittingboar.messages.iterativereduce.ParameterVector.java

License:Apache License

/**
 * TODO: fix loop//w  w  w .ja  v a 2 s  . co  m
 * 
 * @param other_gamma
 */
public void AccumulateParameterVector(Matrix other_gamma) {

    //    this.gamma.plus(arg0)

    for (int row = 0; row < this.parameter_vector.rowSize(); row++) {

        for (int col = 0; col < this.parameter_vector.columnSize(); col++) {

            double old_this_val = this.parameter_vector.get(row, col);
            double other_val = other_gamma.get(row, col);

            // System.out.println( "Accumulate: " + old_this_val + ", " + other_val
            // );

            this.parameter_vector.set(row, col, old_this_val + other_val);

            // System.out.println( "new value: " + this.gamma.get(row, col) );

        }

    }

    //    this.AccumulatedGradientsCount++;

}

From source file:com.cloudera.knittingboar.sgd.GradientBuffer.java

License:Apache License

public void AccumulateGradient(Matrix other_gamma) {

    for (int row = 0; row < this.gamma.rowSize(); row++) {

        for (int col = 0; col < this.gamma.columnSize(); col++) {

            double old_this_val = this.gamma.get(row, col);
            double other_val = other_gamma.get(row, col);

            // System.out.println( "Accumulate: " + old_this_val + ", " + other_val
            // );

            this.gamma.set(row, col, old_this_val + other_val);

            // System.out.println( "new value: " + this.gamma.get(row, col) );

        }// ww  w  . j a  v  a2  s.c o  m

    }

    this.AccumulatedGradientsCount++;

}

From source file:com.cloudera.knittingboar.sgd.MultinomialLogisticRegressionParameterVectors_deprecated.java

License:Apache License

/**
 * TODO: fix loop/*from   w w w .  j a v a2 s . co  m*/
 * 
 * @param other_gamma
 */
public void AccumulateParameterVector(Matrix other_gamma) {

    //    this.gamma.plus(arg0)

    for (int row = 0; row < this.gamma.rowSize(); row++) {

        for (int col = 0; col < this.gamma.columnSize(); col++) {

            double old_this_val = this.gamma.get(row, col);
            double other_val = other_gamma.get(row, col);

            // System.out.println( "Accumulate: " + old_this_val + ", " + other_val
            // );

            this.gamma.set(row, col, old_this_val + other_val);

            // System.out.println( "new value: " + this.gamma.get(row, col) );

        }

    }

    this.AccumulatedGradientsCount++;

}

From source file:com.cloudera.knittingboar.sgd.TestGradientBuffer.java

License:Apache License

public void testAccumulateGradientMatrix() {

    GradientBuffer g0 = new GradientBuffer(2, 2);

    g0.setCell(0, 0, 0.4);/*from w  w  w  .  j a  v  a 2s . com*/
    g0.setCell(0, 1, 0.3);

    assertEquals(g0.numFeatures(), 2);

    /*    GradientBuffer g1 = new GradientBuffer( 2, 2 );
                
        g1.setCell(0, 0, 0.1);
        g1.setCell(0, 1, 0.3);
                
        assertEquals( g1.numFeatures(), 2 );
    */
    Matrix m = new DenseMatrix(2, 2);
    m.set(0, 0, 0.1);
    m.set(0, 1, 0.3);

    g0.AccumulateGradient(m);
    //m.get(arg0, arg1)
    // check source
    assertEquals(m.get(0, 0), 0.1);
    // check accumlation in g0
    assertEquals(g0.getCell(0, 0), 0.5);

    // check source
    assertEquals(m.get(0, 1), 0.3);
    // check accumlation in g0
    assertEquals(g0.getCell(0, 1), 0.6);

    System.out.println("matrix accumulation test done!");

    assertNotNull(0);

}

From source file:com.cloudera.knittingboar.sgd.TestGradientBuffer.java

License:Apache License

public void testAverageGradientBuffer() {

    System.out.println("testAverageGradientBuffer --------");

    GradientBuffer g0 = new GradientBuffer(2, 2);

    g0.setCell(0, 0, 0.1d);//  w w w . j a  v  a2  s.  com
    g0.setCell(0, 1, 0.5d);

    assertEquals(g0.numFeatures(), 2);

    Matrix m = new DenseMatrix(2, 2);
    m.set(0, 0, 0.5d);
    m.set(0, 1, 0.1d);

    g0.AccumulateGradient(m);
    //m.get(arg0, arg1)
    // check source
    assertEquals(m.get(0, 0), 0.5d);
    // check accumlation in g0
    //assertEquals( g0.getCell(0, 0), 0.6 );

    junit.framework.Assert.assertEquals(0.6d, g0.getCell(0, 0), 0.0001);

    // check source
    assertEquals(m.get(0, 1), 0.1d);
    // check accumlation in g0
    //    assertEquals( g0.getCell(0, 1), 0.6 );

    Utils.PrintVectorNonZero(g0.gamma.viewRow(0));
    //Utils.PrintVectorNonZero(g0.gamma.viewRow(1));

    g0.AverageAccumulations(2);

    Utils.PrintVectorNonZero(g0.gamma.viewRow(0));

    System.out.println("matrix accumulation AVG test done!");

    //System.out.println( "add test: " + ( 10.0d + 6.0d ) );

    assertNotNull(0);

}

From source file:com.ml.ira.algos.RunLogistic.java

License:Apache License

static void mainToOutput(String[] args, PrintWriter output) throws Exception {
    if (parseArgs(args)) {
        if (!showAuc && !showConfusion && !showScores) {
            showAuc = true;/* w w w . j a v a 2s  .  c o  m*/
            showConfusion = true;
        }

        Auc collector = new Auc();
        LogisticModelParameters lmp;
        if (modelFile.startsWith("hdfs://")) {
            lmp = LogisticModelParameters.loadFrom(new Path(modelFile));
        } else {
            lmp = LogisticModelParameters.loadFrom(new File(modelFile));
        }
        CsvRecordFactory csv = lmp.getCsvRecordFactory();
        OnlineLogisticRegression lr = lmp.createRegression();
        BufferedReader in = TrainLogistic.open(inputFile);
        //String line = in.readLine();
        //csv.firstLine(line);
        String line;
        if (fieldNames != null && fieldNames.equalsIgnoreCase("internal")) {
            csv.firstLine(lmp.getFieldNames());
        } else {
            csv.firstLine(in.readLine());
        }
        line = in.readLine();
        if (showScores) {
            output.println("\"target\",\"model-output\",\"log-likelihood\"");
        }
        while (line != null) {
            Vector v = new SequentialAccessSparseVector(lmp.getNumFeatures());
            int target = csv.processLine(line, v);

            double score = lr.classifyScalar(v);
            if (showScores) {
                output.printf(Locale.ENGLISH, "%d,%.3f,%.6f%n", target, score, lr.logLikelihood(target, v));
            }
            collector.add(target, score);
            line = in.readLine();
        }

        if (showAuc) {
            output.printf(Locale.ENGLISH, "AUC = %.2f%n", collector.auc());
        }
        if (showConfusion) {
            Matrix m = collector.confusion();
            output.printf(Locale.ENGLISH, "confusion: [[%.1f, %.1f], [%.1f, %.1f]]%n", m.get(0, 0), m.get(1, 0),
                    m.get(0, 1), m.get(1, 1));
            m = collector.entropy();
            output.printf(Locale.ENGLISH, "entropy: [[%.1f, %.1f], [%.1f, %.1f]]%n", m.get(0, 0), m.get(1, 0),
                    m.get(0, 1), m.get(1, 1));
        }
    }
}

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

License:Apache License

public static void assertMatrixEquals(Matrix expected, Matrix actual) {
    Assert.assertEquals(expected.numRows(), actual.numRows());
    Assert.assertEquals(actual.numCols(), actual.numCols());
    for (int row = 0; row < expected.numRows(); row++) {
        for (int col = 0; col < expected.numCols(); col++) {
            Assert.assertEquals("Non-matching values in [" + row + ',' + col + ']', expected.get(row, col),
                    actual.get(row, col), MahoutTestCase.EPSILON);
        }//from w ww.  j  a  va 2  s  .c om
    }
}

From source file:guipart.view.GUIOverviewController.java

@FXML
void handleClassifyModel(ActionEvent event) throws IOException {

    if (pathModel != null && pathCSV != null) {

        Auc collector = new Auc();
        LogisticModelParameters lmp = LogisticModelParameters.loadFrom(new File(pathModel));

        CsvRecordFactory csv = lmp.getCsvRecordFactory();
        OnlineLogisticRegression lr = lmp.createRegression();

        BufferedReader in = Utils.open(pathCSV);

        String line = in.readLine();
        csv.firstLine(line);/*w  ww  .  j a  va  2s  . c  o  m*/
        line = in.readLine();

        int correct = 0;
        int wrong = 0;
        Boolean booltemp;
        String gender;

        while (line != null) {

            Vector v = new SequentialAccessSparseVector(lmp.getNumFeatures());
            int target = csv.processLine(line, v);
            String[] split = line.split(",");

            double score = lr.classifyFull(v).maxValueIndex();
            if (score == target)
                correct++;
            else
                wrong++;

            System.out.println("Target is: " + target + " Score: " + score);

            booltemp = score != 0;

            if (split[1].contentEquals("1"))
                gender = "male";
            else
                gender = "female";

            Person temp = new Person(Integer.parseInt(split[0]), Integer.parseInt(split[4]),
                    Integer.parseInt(split[7]), booltemp, gender, Integer.parseInt(split[5]),
                    Integer.parseInt(split[6]), Integer.parseInt(split[3]));

            guiPart.addPerson(temp);

            line = in.readLine();
            collector.add(target, score);

        }
        double posto = ((double) wrong / (double) (correct + wrong)) * 100;
        System.out.println("Total: " + (correct + wrong) + " Correct: " + correct + " Wrong: " + wrong
                + " Wrong pct: " + posto + "%");
        //PrintWriter output = null;
        Matrix m = collector.confusion();
        //output.printf(Locale.ENGLISH, "confusion: [[%.1f, %.1f], [%.1f, %.1f]]%n",m.get(0, 0), m.get(1, 0), m.get(0, 1), m.get(1, 1));
        System.out.println("Confusion:" + m.get(0, 0) + " " + m.get(1, 0) + "\n \t   " + m.get(0, 1) + " "
                + m.get(1, 1) + " ");
        //        m = collector.entropy();
        //output.printf(Locale.ENGLISH, "entropy: [[%.1f, %.1f], [%.1f, %.1f]]%n",m.get(0, 0), m.get(1, 0), m.get(0, 1), m.get(1, 1));
        textAnalyze2.setText("Confusion:" + m.get(0, 0) + " " + m.get(1, 0) + "\n \t \t   " + m.get(0, 1) + " "
                + m.get(1, 1) + "\n" + "Total: " + (correct + wrong) + " Correct: " + correct + " Wrong: "
                + wrong + " Wrong pct: " + posto + "%");
    } else {

        Dialogs.create().owner(guiPart.getPrimaryStage()).title("Error Dialog")
                .masthead("Look, an Error Dialog").message("One or more files aren't selected").showError();

    }
}

From source file:haflow.component.mahout.logistic.RunLogistic.java

License:Apache License

static void mainToOutput(String[] args) throws Exception {
    if (parseArgs(args)) {
        if (!showAuc && !showConfusion && !showScores) {
            showAuc = true;/* w w  w  . j a v a 2s .com*/
            showConfusion = true;
        }

        //PrintWriter output=new PrintWriter(new FileOutputStream(outputFile),true);

        PrintWriter output = new PrintWriter(HdfsUtil.writeHdfs(outputFile), true);
        PrintWriter acc_output = new PrintWriter(HdfsUtil.writeHdfs(accurateFile), true);
        Auc collector = new Auc();
        LogisticModelParameters lmp = LogisticModelParameters.loadFrom(HdfsUtil.open(modelFile));

        CsvRecordFactory csv = lmp.getCsvRecordFactory();
        OnlineLogisticRegression lr = lmp.createRegression();
        BufferedReader in = new BufferedReader(new InputStreamReader(HdfsUtil.open(inputFile)));
        String line = in.readLine();
        csv.firstLine(line);
        line = in.readLine();
        if (showScores) {
            output.println("\"target\",\"model-output\",\"log-likelihood\"");
        }
        while (line != null) {
            Vector v = new SequentialAccessSparseVector(lmp.getNumFeatures());
            int target = csv.processLine(line, v);

            double score = lr.classifyScalar(v);
            if (showScores) {
                output.printf(Locale.ENGLISH, "%d,%.3f,%.6f%n", target, score, lr.logLikelihood(target, v));
            }
            collector.add(target, score);
            line = in.readLine();
        }

        if (showAuc) {
            acc_output.printf(Locale.ENGLISH, "AUC , %.2f%n", collector.auc());
        }
        if (showConfusion) {
            Matrix m = collector.confusion();
            acc_output.printf(Locale.ENGLISH, "confusion, [[%.1f  %.1f], [%.1f  %.1f]]%n", m.get(0, 0),
                    m.get(1, 0), m.get(0, 1), m.get(1, 1));
            m = collector.entropy();
            acc_output.printf(Locale.ENGLISH, "entropy, [[%.1f  %.1f], [%.1f  %.1f]]%n", m.get(0, 0),
                    m.get(1, 0), m.get(0, 1), m.get(1, 1));
        }
        output.close();
        acc_output.close();
    }
}

From source file:javaapplication3.RunLogistic.java

public static void main(String[] args) throws IOException {
    // TODO code application logic here
    Auc collector = new Auc();

    LogisticModelParameters lmp = LogisticModelParameters.loadFrom(new File(modelFile));

    CsvRecordFactory csv = lmp.getCsvRecordFactory();
    OnlineLogisticRegression lr = lmp.createRegression();

    BufferedReader in = open(inputFile);

    String line = in.readLine();//w w  w . ja va  2s  .c o m
    csv.firstLine(line);
    line = in.readLine();
    int correct = 0;
    int wrong = 0;
    while (line != null) {
        Vector v = new SequentialAccessSparseVector(lmp.getNumFeatures());
        int target = csv.processLine(line, v);

        System.out.println(line);
        String[] split = line.split(",");

        double score = lr.classifyFull(v).maxValueIndex();
        if (score == target)
            correct++;
        else
            wrong++;

        System.out.println("Target is: " + target + " Score: " + score);
        line = in.readLine();
        collector.add(target, score);

    }
    double posto = ((double) wrong / (double) (correct + wrong)) * 100;
    System.out.println("Total: " + (correct + wrong) + " Correct: " + correct + " Wrong: " + wrong
            + " Wrong pct: " + posto + "%");
    //PrintWriter output = null;
    Matrix m = collector.confusion();
    //output.printf(Locale.ENGLISH, "confusion: [[%.1f, %.1f], [%.1f, %.1f]]%n",m.get(0, 0), m.get(1, 0), m.get(0, 1), m.get(1, 1));
    System.out.println("Confusion:" + m.get(0, 0) + " " + m.get(1, 0) + "\n \t   " + m.get(0, 1) + " "
            + m.get(1, 1) + " ");
    //        m = collector.entropy();
    //output.printf(Locale.ENGLISH, "entropy: [[%.1f, %.1f], [%.1f, %.1f]]%n",m.get(0, 0), m.get(1, 0), m.get(0, 1), m.get(1, 1));

}