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);
}
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());
}
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();
}
}
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();
}
}
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();
}
Aggregations