Search in sources :

Example 91 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstMean method colwiseMean.

/**
   * Compute column-wise means (i.e. means of each column), and return a frame having a single row.
   */
private ValFrame colwiseMean(Frame fr, final boolean na_rm) {
    Frame res = new Frame();
    Vec vec1 = Vec.makeCon(null, 0);
    assert vec1.length() == 1;
    for (int i = 0; i < fr.numCols(); i++) {
        Vec v = fr.vec(i);
        boolean valid = (v.isNumeric() || v.isTime() || v.isBinary()) && v.length() > 0 && (na_rm || v.naCnt() == 0);
        Vec newvec = vec1.makeCon(valid ? v.mean() : Double.NaN, v.isTime() ? Vec.T_TIME : Vec.T_NUM);
        res.add(fr.name(i), newvec);
    }
    vec1.remove();
    return new ValFrame(res);
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame)

Example 92 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstMean method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val val1 = asts[1].exec(env);
    if (val1 instanceof ValFrame) {
        Frame fr = stk.track(val1).getFrame();
        boolean na_rm = asts[2].exec(env).getNum() == 1;
        boolean axis = asts.length == 4 && (asts[3].exec(env).getNum() == 1);
        return axis ? rowwiseMean(fr, na_rm) : colwiseMean(fr, na_rm);
    } else if (val1 instanceof ValRow) {
        // This may be called from AstApply when doing per-row computations.
        double[] row = val1.getRow();
        boolean na_rm = asts[2].exec(env).getNum() == 1;
        double d = 0;
        int n = 0;
        for (double r : row) {
            if (Double.isNaN(r)) {
                if (!na_rm)
                    return new ValRow(new double[] { Double.NaN }, null);
            } else {
                d += r;
                n++;
            }
        }
        return new ValRow(new double[] { d / n }, null);
    } else
        throw new IllegalArgumentException("Incorrect argument to (mean): expected a frame or a row, received " + val1.getClass());
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) ValRow(water.rapids.vals.ValRow)

Example 93 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstTokenizeTest method testTokenize.

@Test
public void testTokenize() {
    Frame fr = makeTestFrame();
    Vec expected = svec("Foot", "on", "the", "pedal", "Never", "ever", "false", "metal", null, "Engine", "running", "hotter", "than", "a", "boiling", "kettle", "My", "job", "ain't", "a", "job", null, "It's", "a", "damn", "good", "time", "City", "to", "city", "I'm", "running", "my", "rhymes", null);
    Frame res = null;
    try {
        ValFrame val = (ValFrame) Rapids.exec("(tmp= py_1 (tokenize data \"\\\\s\"))");
        res = val.getFrame();
        Vec actual = res.anyVec();
        assertStringVecEquals(expected, actual);
    } finally {
        fr.remove();
        expected.remove();
        if (res != null)
            res.remove();
    }
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Vec(water.fvec.Vec) Test(org.junit.Test)

Example 94 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class RapidsTest method testProstate_assign_frame_scalar.

@Test
public void testProstate_assign_frame_scalar() {
    Frame fr = parse_test_file(Key.make("prostate.hex"), "smalldata/logreg/prostate.csv");
    try {
        Val val = Rapids.exec("(tmp= py_1 (:= prostate.hex -1 1 (== (cols_py prostate.hex 1) 0)))");
        if (val instanceof ValFrame) {
            Frame fr2 = val.getFrame();
            System.out.println(fr2.vec(0));
            fr2.remove();
        }
    } finally {
        fr.delete();
    }
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Test(org.junit.Test)

Example 95 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class RapidsTest method testCombo.

@Test
public void testCombo() {
    Frame fr = parse_test_file(Key.make("a.hex"), "smalldata/iris/iris_wheader.csv");
    String tree = "(tmp= py_2 (:= (tmp= py_1 (cbind a.hex (== (cols_py a.hex 4.0 ) \"Iris-setosa\" ) ) ) (as.factor (cols_py py_1 5.0 ) ) 5.0 [] ) )";
    //String tree = "(:= (tmp= py_1 a.hex) (h2o.runif a.hex -1) 4 [])";
    Val val = Rapids.exec(tree);
    if (val instanceof ValFrame) {
        Frame fr2 = val.getFrame();
        System.out.println(fr2.vec(0));
        fr2.remove();
    }
    fr.delete();
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Test(org.junit.Test)

Aggregations

ValFrame (water.rapids.vals.ValFrame)132 Frame (water.fvec.Frame)98 Val (water.rapids.Val)48 Vec (water.fvec.Vec)43 Test (org.junit.Test)38 MRTask (water.MRTask)32 Chunk (water.fvec.Chunk)24 NewChunk (water.fvec.NewChunk)23 BufferedString (water.parser.BufferedString)16 AstNumList (water.rapids.ast.params.AstNumList)11 AstNum (water.rapids.ast.params.AstNum)7 ValNum (water.rapids.vals.ValNum)7 AstRoot (water.rapids.ast.AstRoot)6 ValRow (water.rapids.vals.ValRow)6 ArrayList (java.util.ArrayList)5 Key (water.Key)5 AstStrList (water.rapids.ast.params.AstStrList)5 Futures (water.Futures)4 AstParameter (water.rapids.ast.AstParameter)4 Random (java.util.Random)3