use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstFilterNaCols method apply.
@Override
public ValNums apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
double frac = asts[2].exec(env).getNum();
double nrow = fr.numRows() * frac;
Vec[] vecs = fr.vecs();
ArrayList<Double> idxs = new ArrayList<>();
for (double i = 0; i < fr.numCols(); i++) if (vecs[(int) i].naCnt() < nrow)
idxs.add(i);
double[] include_cols = new double[idxs.size()];
int i = 0;
for (double d : idxs) include_cols[i++] = d;
return new ValNums(include_cols);
}
use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstFlatten method apply.
@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
// did not flatten
if (fr.numCols() != 1 || fr.numRows() != 1)
return new ValFrame(fr);
Vec vec = fr.anyVec();
switch(vec.get_type()) {
case Vec.T_BAD:
case Vec.T_NUM:
return new ValNum(vec.at(0));
case Vec.T_TIME:
// check for missing values
return vec.isNA(0) ? new ValNum(Double.NaN) : new ValNum(vec.at8(0));
case Vec.T_STR:
return new ValStr(vec.atStr(new BufferedString(), 0).toString());
case // check for missing values
Vec.T_CAT:
return vec.isNA(0) ? new ValStr("NA") : new ValStr(vec.factor(vec.at8(0)));
default:
throw H2O.unimpl("The type of vector: " + vec.get_type_str() + " is not supported by " + str());
}
}
use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstGroup method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
int ncols = fr.numCols();
AstNumList groupby = check(ncols, asts[2]);
final int[] gbCols = groupby.expand4();
// Count of aggregates; knock off the first 4 ASTs (GB data [group-by] [order-by]...),
// then count by triples.
int naggs = (asts.length - 3) / 3;
final AGG[] aggs = new AGG[naggs];
for (int idx = 3; idx < asts.length; idx += 3) {
Val v = asts[idx].exec(env);
String fn = v instanceof ValFun ? v.getFun().str() : v.getStr();
FCN fcn = FCN.valueOf(fn);
AstNumList col = check(ncols, asts[idx + 1]);
if (col.cnt() != 1)
throw new IllegalArgumentException("Group-By functions take only a single column");
// Aggregate column
int agg_col = (int) col.min();
if (fcn == FCN.mode && !fr.vec(agg_col).isCategorical())
throw new IllegalArgumentException("Mode only allowed on categorical columns");
NAHandling na = NAHandling.valueOf(asts[idx + 2].exec(env).getStr().toUpperCase());
aggs[(idx - 3) / 3] = new AGG(fcn, agg_col, na, (int) fr.vec(agg_col).max() + 1);
}
// do the group by work now
IcedHashMap<G, String> gss = doGroups(fr, gbCols, aggs);
final G[] grps = gss.keySet().toArray(new G[gss.size()]);
// apply an ORDER by here...
if (gbCols.length > 0)
Arrays.sort(grps, new java.util.Comparator<G>() {
// Compare 2 groups. Iterate down _gs, stop when _gs[i] > that._gs[i],
// or _gs[i] < that._gs[i]. Order by various columns specified by
// gbCols. NaN is treated as least
@Override
public int compare(G g1, G g2) {
for (int i = 0; i < gbCols.length; i++) {
if (Double.isNaN(g1._gs[i]) && !Double.isNaN(g2._gs[i]))
return -1;
if (!Double.isNaN(g1._gs[i]) && Double.isNaN(g2._gs[i]))
return 1;
if (g1._gs[i] != g2._gs[i])
return g1._gs[i] < g2._gs[i] ? -1 : 1;
}
return 0;
}
// I do not believe sort() calls equals() at this time, so no need to implement
@Override
public boolean equals(Object o) {
throw H2O.unimpl();
}
});
// Build the output!
String[] fcnames = new String[aggs.length];
for (int i = 0; i < aggs.length; i++) fcnames[i] = aggs[i]._fcn.toString() + "_" + fr.name(aggs[i]._col);
MRTask mrfill = new MRTask() {
@Override
public void map(Chunk[] c, NewChunk[] ncs) {
int start = (int) c[0].start();
for (int i = 0; i < c[0]._len; ++i) {
// One Group per row
G g = grps[i + start];
int j;
for (// The Group Key, as a row
j = 0; // The Group Key, as a row
j < g._gs.length; // The Group Key, as a row
j++) ncs[j].addNum(g._gs[j]);
for (int a = 0; a < aggs.length; a++) ncs[j++].addNum(aggs[a]._fcn.postPass(g._dss[a], g._ns[a]));
}
}
};
Frame f = buildOutput(gbCols, naggs, fr, fcnames, grps.length, mrfill);
return new ValFrame(f);
}
use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstGroupedPermute method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
final int permCol = (int) asts[2].exec(env).getNum();
AstNumList groupby = AstGroup.check(fr.numCols(), asts[3]);
final int[] gbCols = groupby.expand4();
final int permuteBy = (int) asts[4].exec(env).getNum();
final int keepCol = (int) asts[5].exec(env).getNum();
String[] names = new String[gbCols.length + 4];
int i = 0;
for (; i < gbCols.length; ++i) names[i] = fr.name(gbCols[i]);
names[i++] = "In";
names[i++] = "Out";
names[i++] = "InAmnt";
names[i] = "OutAmnt";
String[][] domains = new String[names.length][];
int d = 0;
for (; d < gbCols.length; d++) domains[d] = fr.domains()[gbCols[d]];
domains[d++] = fr.domains()[permCol];
domains[d++] = fr.domains()[permCol];
domains[d++] = fr.domains()[keepCol];
domains[d] = fr.domains()[keepCol];
long s = System.currentTimeMillis();
BuildGroups t = new BuildGroups(gbCols, permuteBy, permCol, keepCol).doAll(fr);
Log.info("Elapsed time: " + (System.currentTimeMillis() - s) / 1000. + "s");
s = System.currentTimeMillis();
SmashGroups sg;
H2O.submitTask(sg = new SmashGroups(t._grps)).join();
Log.info("Elapsed time: " + (System.currentTimeMillis() - s) / 1000. + "s");
return new ValFrame(buildOutput(sg._res.values().toArray(new double[0][][]), names, domains));
}
use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstRename method apply.
@Override
public ValNum apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Key oldKey = Key.make(env.expand(asts[1].exec(env).getStr()));
Key newKey = Key.make(env.expand(asts[2].exec(env).getStr()));
Iced o = DKV.remove(oldKey).get();
if (o instanceof Frame)
DKV.put(newKey, new Frame(newKey, ((Frame) o)._names, ((Frame) o).vecs()));
else if (o instanceof Model) {
((Model) o)._key = newKey;
DKV.put(newKey, o);
} else
throw new IllegalArgumentException("Trying to rename Value of type " + o.getClass());
return new ValNum(Double.NaN);
}
Aggregations