use of water.rapids.vals.ValNum in project h2o-3 by h2oai.
the class AstVariance method array.
// Matrix covariance. Compute covariance between all columns from each Frame
// against each other. Return a matrix of covariances which is frx.numCols
// wide and fry.numCols tall.
private Val array(Frame frx, Frame fry, Mode mode, boolean symmetric) {
Vec[] vecxs = frx.vecs();
int ncolx = vecxs.length;
Vec[] vecys = fry.vecs();
int ncoly = vecys.length;
if (mode.equals(Mode.Everything) || mode.equals(Mode.AllObs)) {
if (mode.equals(Mode.AllObs)) {
for (Vec v : vecxs) if (v.naCnt() != 0)
throw new IllegalArgumentException("Mode is 'all.obs' but NAs are present");
if (!symmetric)
for (Vec v : vecys) if (v.naCnt() != 0)
throw new IllegalArgumentException("Mode is 'all.obs' but NAs are present");
}
CoVarTaskEverything[] cvs = new CoVarTaskEverything[ncoly];
double[] xmeans = new double[ncolx];
for (int x = 0; x < ncoly; x++) xmeans[x] = vecxs[x].mean();
if (symmetric) {
//1-col returns scalar
if (ncoly == 1)
return new ValNum(vecys[0].naCnt() == 0 ? vecys[0].sigma() * vecys[0].sigma() : Double.NaN);
int[] idx = new int[ncoly];
for (int y = 1; y < ncoly; y++) idx[y] = y;
int[] first_index = new int[] { 0 };
//compute covariances between column_i and column_i+1, column_i+2, ...
Frame reduced_fr;
for (int y = 0; y < ncoly - 1; y++) {
idx = ArrayUtils.removeIds(idx, first_index);
reduced_fr = new Frame(frx.vecs(idx));
cvs[y] = new CoVarTaskEverything(vecys[y].mean(), xmeans).dfork(new Frame(vecys[y]).add(reduced_fr));
}
double[][] res_array = new double[ncoly][ncoly];
//fill in the diagonals (variances) using sigma from rollupstats
for (int y = 0; y < ncoly; y++) res_array[y][y] = vecys[y].naCnt() == 0 ? vecys[y].sigma() * vecys[y].sigma() : Double.NaN;
//arrange the results into the bottom left of res_array. each successive cvs is 1 smaller in length
for (int y = 0; y < ncoly - 1; y++) System.arraycopy(ArrayUtils.div(cvs[y].getResult()._covs, (fry.numRows() - 1)), 0, res_array[y], y + 1, ncoly - y - 1);
//copy over the bottom left of res_array to its top right
for (int y = 0; y < ncoly - 1; y++) {
for (int x = y + 1; x < ncoly; x++) {
res_array[x][y] = res_array[y][x];
}
}
//set Frame
Vec[] res = new Vec[ncoly];
Key<Vec>[] keys = Vec.VectorGroup.VG_LEN1.addVecs(ncoly);
for (int y = 0; y < ncoly; y++) {
res[y] = Vec.makeVec(res_array[y], keys[y]);
}
return new ValFrame(new Frame(fry._names, res));
}
// Launch tasks; each does all Xs vs one Y
for (int y = 0; y < ncoly; y++) cvs[y] = new CoVarTaskEverything(vecys[y].mean(), xmeans).dfork(new Frame(vecys[y]).add(frx));
// 1-col returns scalar
if (ncolx == 1 && ncoly == 1) {
return new ValNum(cvs[0].getResult()._covs[0] / (fry.numRows() - 1));
}
// Gather all the Xs-vs-Y covariance arrays; divide by rows
Vec[] res = new Vec[ncoly];
Key<Vec>[] keys = Vec.VectorGroup.VG_LEN1.addVecs(ncoly);
for (int y = 0; y < ncoly; y++) res[y] = Vec.makeVec(ArrayUtils.div(cvs[y].getResult()._covs, (fry.numRows() - 1)), keys[y]);
return new ValFrame(new Frame(fry._names, res));
} else {
if (symmetric) {
if (ncoly == 1)
return new ValNum(vecys[0].sigma() * vecys[0].sigma());
CoVarTaskCompleteObsMeanSym taskCompleteObsMeanSym = new CoVarTaskCompleteObsMeanSym().doAll(fry);
long NACount = taskCompleteObsMeanSym._NACount;
double[] ymeans = ArrayUtils.div(taskCompleteObsMeanSym._ysum, fry.numRows() - NACount);
// 1 task with all Ys
CoVarTaskCompleteObsSym cvs = new CoVarTaskCompleteObsSym(ymeans).doAll(new Frame(fry));
double[][] res_array = new double[ncoly][ncoly];
for (int y = 0; y < ncoly; y++) {
System.arraycopy(ArrayUtils.div(cvs._covs[y], (fry.numRows() - 1 - NACount)), y, res_array[y], y, ncoly - y);
}
//copy over the bottom left of res_array to its top right
for (int y = 0; y < ncoly - 1; y++) {
for (int x = y + 1; x < ncoly; x++) {
res_array[x][y] = res_array[y][x];
}
}
//set Frame
Vec[] res = new Vec[ncoly];
Key<Vec>[] keys = Vec.VectorGroup.VG_LEN1.addVecs(ncoly);
for (int y = 0; y < ncoly; y++) {
res[y] = Vec.makeVec(res_array[y], keys[y]);
}
return new ValFrame(new Frame(fry._names, res));
}
CoVarTaskCompleteObsMean taskCompleteObsMean = new CoVarTaskCompleteObsMean(ncoly, ncolx).doAll(new Frame(fry).add(frx));
long NACount = taskCompleteObsMean._NACount;
double[] ymeans = ArrayUtils.div(taskCompleteObsMean._ysum, fry.numRows() - NACount);
double[] xmeans = ArrayUtils.div(taskCompleteObsMean._xsum, fry.numRows() - NACount);
// 1 task with all Xs and Ys
CoVarTaskCompleteObs cvs = new CoVarTaskCompleteObs(ymeans, xmeans).doAll(new Frame(fry).add(frx));
// 1-col returns scalar
if (ncolx == 1 && ncoly == 1) {
return new ValNum(cvs._covs[0][0] / (fry.numRows() - 1 - NACount));
}
// Gather all the Xs-vs-Y covariance arrays; divide by rows
Vec[] res = new Vec[ncoly];
Key<Vec>[] keys = Vec.VectorGroup.VG_LEN1.addVecs(ncoly);
for (int y = 0; y < ncoly; y++) res[y] = Vec.makeVec(ArrayUtils.div(cvs._covs[y], (fry.numRows() - 1 - NACount)), keys[y]);
return new ValFrame(new Frame(fry._names, res));
}
}
use of water.rapids.vals.ValNum in project h2o-3 by h2oai.
the class AstRm method apply.
@Override
public ValNum apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Key id = Key.make(env.expand(asts[1].str()));
Value val = DKV.get(id);
if (val == null)
return new ValNum(0);
if (val.isFrame())
// Remove unshared Vecs
env._ses.remove(val.<Frame>get());
else
// Normal (e.g. Model) remove
Keyed.remove(id);
return new ValNum(1);
}
use of water.rapids.vals.ValNum in project h2o-3 by h2oai.
the class AstIsNa method exec.
@Override
public Val exec(Val... args) {
Val val = args[1];
switch(val.type()) {
case Val.NUM:
return new ValNum(op(val.getNum()));
case Val.FRM:
Frame fr = val.getFrame();
String[] newNames = new String[fr.numCols()];
for (int i = 0; i < newNames.length; i++) {
newNames[i] = "isNA(" + fr.name(i) + ")";
}
return new ValFrame(new MRTask() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
for (int col = 0; col < cs.length; col++) {
Chunk c = cs[col];
NewChunk nc = ncs[col];
for (int i = 0; i < c._len; i++) nc.addNum(c.isNA(i) ? 1 : 0);
}
}
}.doAll(fr.numCols(), Vec.T_NUM, fr).outputFrame(newNames, null));
case Val.STR:
return new ValNum(val.getStr() == null ? 1 : 0);
default:
throw H2O.unimpl("is.na unimpl: " + val.getClass());
}
}
use of water.rapids.vals.ValNum in project h2o-3 by h2oai.
the class AstNLevels method apply.
@Override
public ValNum apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
int nlevels;
Frame fr = stk.track(asts[1].exec(env)).getFrame();
if (fr.numCols() == 1) {
Vec v = fr.anyVec();
nlevels = v != null && v.isCategorical() ? v.domain().length : 0;
return new ValNum(nlevels);
} else
throw new IllegalArgumentException("nlevels applies to a single column. Got: " + fr.numCols());
}
use of water.rapids.vals.ValNum in project h2o-3 by h2oai.
the class AstBinOp method prim_apply.
public Val prim_apply(Val left, Val rite) {
switch(left.type()) {
case Val.NUM:
final double dlf = left.getNum();
switch(rite.type()) {
case Val.NUM:
return new ValNum(op(dlf, rite.getNum()));
case Val.NUMS:
return new ValNum(op(dlf, rite.getNums()[0]));
case Val.FRM:
return scalar_op_frame(dlf, rite.getFrame());
case Val.ROW:
double[] lft = new double[rite.getRow().length];
Arrays.fill(lft, dlf);
return row_op_row(lft, rite.getRow(), ((ValRow) rite).getNames());
case Val.STR:
throw H2O.unimpl();
case Val.STRS:
throw H2O.unimpl();
default:
throw H2O.unimpl();
}
case Val.NUMS:
final double ddlf = left.getNums()[0];
switch(rite.type()) {
case Val.NUM:
return new ValNum(op(ddlf, rite.getNum()));
case Val.NUMS:
return new ValNum(op(ddlf, rite.getNums()[0]));
case Val.FRM:
return scalar_op_frame(ddlf, rite.getFrame());
case Val.ROW:
double[] lft = new double[rite.getRow().length];
Arrays.fill(lft, ddlf);
return row_op_row(lft, rite.getRow(), ((ValRow) rite).getNames());
case Val.STR:
throw H2O.unimpl();
case Val.STRS:
throw H2O.unimpl();
default:
throw H2O.unimpl();
}
case Val.FRM:
Frame flf = left.getFrame();
switch(rite.type()) {
case Val.NUM:
return frame_op_scalar(flf, rite.getNum());
case Val.NUMS:
return frame_op_scalar(flf, rite.getNums()[0]);
case Val.STR:
return frame_op_scalar(flf, rite.getStr());
case Val.STRS:
return frame_op_scalar(flf, rite.getStrs()[0]);
case Val.FRM:
return frame_op_frame(flf, rite.getFrame());
default:
throw H2O.unimpl();
}
case Val.STR:
String slf = left.getStr();
switch(rite.type()) {
case Val.NUM:
throw H2O.unimpl();
case Val.NUMS:
throw H2O.unimpl();
case Val.STR:
throw H2O.unimpl();
case Val.STRS:
throw H2O.unimpl();
case Val.FRM:
return scalar_op_frame(slf, rite.getFrame());
default:
throw H2O.unimpl();
}
case Val.STRS:
String sslf = left.getStrs()[0];
switch(rite.type()) {
case Val.NUM:
throw H2O.unimpl();
case Val.NUMS:
throw H2O.unimpl();
case Val.STR:
throw H2O.unimpl();
case Val.STRS:
throw H2O.unimpl();
case Val.FRM:
return scalar_op_frame(sslf, rite.getFrame());
default:
throw H2O.unimpl();
}
case Val.ROW:
double[] dslf = left.getRow();
switch(rite.type()) {
case Val.NUM:
double[] right = new double[dslf.length];
Arrays.fill(right, rite.getNum());
return row_op_row(dslf, right, ((ValRow) left).getNames());
case Val.ROW:
return row_op_row(dslf, rite.getRow(), ((ValRow) rite).getNames());
case Val.FRM:
return row_op_row(dslf, rite.getRow(), rite.getFrame().names());
default:
throw H2O.unimpl();
}
default:
throw H2O.unimpl();
}
}
Aggregations