use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstQtile method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
QuantileModel.QuantileParameters parms = new QuantileModel.QuantileParameters();
Frame fr = stk.track(asts[1].exec(env)).getFrame();
// Force a bogus Key for Quantiles ModelBuilder
Frame fr_wkey = new Frame(fr);
DKV.put(fr_wkey);
parms._train = fr_wkey._key;
parms._probs = ((AstNumList) asts[2]).expand();
for (double d : parms._probs) if (d < 0 || d > 1)
throw new IllegalArgumentException("Probability must be between 0 and 1: " + d);
String inter = asts[3].exec(env).getStr();
parms._combine_method = QuantileModel.CombineMethod.valueOf(inter.toUpperCase());
parms._weights_column = asts[4].str().equals("_") ? null : asts[4].str();
// Compute Quantiles
Job j = new Quantile(parms).trainModel();
QuantileModel q = (QuantileModel) j.get();
DKV.remove(j._key);
// Remove bogus Key
DKV.remove(fr_wkey._key);
// Reshape all outputs as a Frame, with probs in col 0 and the
// quantiles in cols 1 thru fr.numCols() - except the optional weights vec
int ncols = fr.numCols();
if (parms._weights_column != null)
ncols--;
Vec[] vecs = new Vec[1 + /*1 more for the probs themselves*/
ncols];
String[] names = new String[vecs.length];
vecs[0] = Vec.makeCon(null, parms._probs);
names[0] = "Probs";
int w = 0;
for (int i = 0; i < vecs.length - 1; ++i) {
if (fr._names[i].equals(parms._weights_column))
w = 1;
vecs[i + 1] = Vec.makeCon(null, q._output._quantiles[i]);
names[i + 1] = fr._names[w + i] + "Quantiles";
}
q.delete();
return new ValFrame(new Frame(names, vecs));
}
use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstRunif method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
long seed = (long) asts[2].exec(env).getNum();
if (seed == -1)
seed = new Random().nextLong();
return new ValFrame(new Frame(new String[] { "rnd" }, new Vec[] { fr.anyVec().makeRand(seed) }));
}
use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstTable method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr1 = stk.track(asts[1].exec(env)).getFrame();
final boolean dense = asts[asts.length - 1].exec(env).getNum() == 1;
Frame fr2 = asts.length == 4 ? stk.track(asts[2].exec(env)).getFrame() : null;
int ncols = fr1.numCols() + (fr2 == null ? 0 : fr2.numCols());
Vec vec1 = fr1.vec(0);
ValFrame res = fast_table(vec1, ncols, fr1._names[0]);
if (res != null)
return res;
if (!(asts.length == 3 || asts.length == 4) || ncols > 2)
throw new IllegalArgumentException("table expects one or two columns");
Vec vec2 = fr1.numCols() == 2 ? fr1.vec(1) : fr2 != null ? fr2.vec(0) : null;
int sz = fr1._names.length + (fr2 != null ? fr2._names.length : 0);
String[] colnames = new String[sz];
int i = 0;
for (String name : fr1._names) colnames[i++] = name;
if (fr2 != null)
for (String name : fr2._names) colnames[i++] = name;
return slow_table(vec1, vec2, colnames, dense);
}
use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstUnique method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
Vec vec0 = fr.vec(0);
Vec v;
if (fr.numCols() != 1)
throw new IllegalArgumentException("Unique applies to a single column only.");
if (vec0.isCategorical()) {
v = Vec.makeSeq(0, (long) vec0.domain().length, true);
v.setDomain(vec0.domain());
DKV.put(v);
} else {
UniqTask t = new UniqTask().doAll(fr);
int nUniq = t._uniq.size();
final AstGroup.G[] uniq = t._uniq.keySet().toArray(new AstGroup.G[nUniq]);
v = Vec.makeZero(nUniq, vec0.get_type());
new MRTask() {
@Override
public void map(Chunk c) {
int start = (int) c.start();
for (int i = 0; i < c._len; ++i) c.set(i, uniq[i + start]._gs[0]);
}
}.doAll(v);
}
return new ValFrame(new Frame(v));
}
use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstLs method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
ArrayList<String> domain = new ArrayList<>();
Futures fs = new Futures();
AppendableVec av = new AppendableVec(Vec.VectorGroup.VG_LEN1.addVec(), Vec.T_CAT);
NewChunk keys = new NewChunk(av, 0);
int r = 0;
for (Key key : KeySnapshot.globalSnapshot().keys()) {
keys.addCategorical(r++);
domain.add(key.toString());
}
String[] key_domain = domain.toArray(new String[domain.size()]);
av.setDomain(key_domain);
keys.close(fs);
// c0 is the row index vec
Vec c0 = av.layout_and_close(fs);
fs.blockForPending();
return new ValFrame(new Frame(Key.<Frame>make("h2o_ls"), new String[] { "key" }, new Vec[] { c0 }));
}
Aggregations