use of water.fvec.Vec 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.Vec 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.Vec in project h2o-3 by h2oai.
the class AstSetDomain method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame f = stk.track(asts[1].exec(env)).getFrame();
String[] _domains = ((AstStrList) asts[2])._strs;
if (f.numCols() != 1)
throw new IllegalArgumentException("Must be a single column. Got: " + f.numCols() + " columns.");
Vec v = f.anyVec();
if (!v.isCategorical())
throw new IllegalArgumentException("Vector must be a factor column. Got: " + v.get_type_str());
if (_domains != null && _domains.length != v.domain().length) {
// in this case we want to recollect the domain and check that number of levels matches _domains
VecUtils.CollectDomainFast t = new VecUtils.CollectDomainFast((int) v.max());
t.doAll(v);
final long[] dom = t.domain();
if (dom.length != _domains.length)
throw new IllegalArgumentException("Number of replacement factors must equal current number of levels. Current number of levels: " + dom.length + " != " + _domains.length);
new MRTask() {
@Override
public void map(Chunk c) {
for (int i = 0; i < c._len; ++i) {
if (!c.isNA(i)) {
long num = Arrays.binarySearch(dom, c.at8(i));
if (num < 0)
throw new IllegalArgumentException("Could not find the categorical value!");
c.set(i, num);
}
}
}
}.doAll(v);
}
v.setDomain(_domains);
DKV.put(v);
return new ValFrame(f);
}
use of water.fvec.Vec in project h2o-3 by h2oai.
the class AstBinOp method scalar_op_frame.
/**
* Auto-widen the scalar to every element of the frame
*/
private ValFrame scalar_op_frame(final String str, Frame fr) {
Frame res = new MRTask() {
@Override
public void map(Chunk[] chks, NewChunk[] cress) {
BufferedString vstr = new BufferedString();
for (int c = 0; c < chks.length; c++) {
Chunk chk = chks[c];
NewChunk cres = cress[c];
Vec vec = chk.vec();
// String Vectors: apply str_op as BufferedStrings to all elements
if (vec.isString()) {
final BufferedString conStr = new BufferedString(str);
for (int i = 0; i < chk._len; i++) cres.addNum(str_op(conStr, chk.atStr(vstr, i)));
} else if (vec.isCategorical()) {
// categorical Vectors: convert string to domain value; apply op (not
// str_op). Not sure what the "right" behavior here is, can
// easily argue that should instead apply str_op to the categorical
// string domain value - except that this whole operation only
// makes sense for EQ/NE, and is much faster when just comparing
// doubles vs comparing strings.
final double d = (double) ArrayUtils.find(vec.domain(), str);
for (int i = 0; i < chk._len; i++) cres.addNum(op(d, chk.atd(i)));
} else {
// mixing string and numeric
// false or true only
final double d = op(1, 2);
for (int i = 0; i < chk._len; i++) cres.addNum(d);
}
}
}
}.doAll(fr.numCols(), Vec.T_NUM, fr).outputFrame(fr._names, null);
return new ValFrame(res);
}
use of water.fvec.Vec in project h2o-3 by h2oai.
the class AstRepLen method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Val v = asts[1].exec(env);
long length = (long) asts[2].exec(env).getNum();
Frame ff;
if (v instanceof ValFrame)
ff = stk.track(v).getFrame();
else
return new ValFrame(new Frame(Vec.makeCon(v.getNum(), length)));
final Frame fr = ff;
if (fr.numCols() == 1) {
Vec vec = Vec.makeRepSeq(length, fr.numRows());
new MRTask() {
@Override
public void map(Chunk c) {
for (int i = 0; i < c._len; ++i) c.set(i, fr.anyVec().at((long) c.atd(i)));
}
}.doAll(vec);
vec.setDomain(fr.anyVec().domain());
return new ValFrame(new Frame(vec));
} else {
Frame f = new Frame();
for (int i = 0; i < length; ++i) f.add(Frame.defaultColName(f.numCols()), fr.vec(i % fr.numCols()));
return new ValFrame(f);
}
}
Aggregations