use of water.fvec.NewChunk in project h2o-2 by h2oai.
the class SpeeDRFModel method scoreOnTest.
private void scoreOnTest(Frame fr, Vec modelResp) {
Frame scored = score(fr);
water.api.ConfusionMatrix cm = new water.api.ConfusionMatrix();
cm.vactual = fr.lastVec();
cm.vpredict = scored.anyVec();
cm.invoke();
// Regression scoring
if (regression) {
float mse = (float) cm.mse;
errs[errs.length - 1] = mse;
cms[cms.length - 1] = null;
// Classification scoring
} else {
Vec lv = scored.lastVec();
double mse = CMTask.MSETask.doTask(scored.add("actual", fr.lastVec()));
this.cm = cm.cm;
errs[errs.length - 1] = (float) mse;
ConfusionMatrix new_cm = new ConfusionMatrix(this.cm);
cms[cms.length - 1] = new_cm;
// Create the ROC Plot
if (classes() == 2) {
Vec v = null;
Frame fa = null;
if (lv.isInt()) {
fa = new MRTask2() {
@Override
public void map(Chunk[] cs, NewChunk nchk) {
int rows = cs[0]._len;
int cols = cs.length - 1;
for (int r = 0; r < rows; ++r) {
nchk.addNum(cs[cols].at0(r) == 0 ? 1e-10 : 1.0 - 1e-10);
}
}
}.doAll(1, scored).outputFrame(null, null);
v = fa.anyVec();
}
AUC auc_calc = new AUC();
auc_calc.vactual = cm.vactual;
// lastVec is class1
auc_calc.vpredict = v == null ? lv : v;
auc_calc.invoke();
validAUC = auc_calc.data();
if (v != null)
UKV.remove(v._key);
if (fa != null)
fa.delete();
UKV.remove(lv._key);
}
}
scored.remove("actual");
scored.delete();
}
use of water.fvec.NewChunk in project h2o-3 by h2oai.
the class ArrayUtils method frame.
/** Create a new frame based on given row data.
* @param key Key for the frame
* @param names names of frame columns
* @param rows data given in the form of rows
* @return new frame which contains columns named according given names and including given data */
public static Frame frame(Key<Frame> key, String[] names, double[]... rows) {
assert names == null || names.length == rows[0].length;
Futures fs = new Futures();
Vec[] vecs = new Vec[rows[0].length];
Key<Vec>[] keys = Vec.VectorGroup.VG_LEN1.addVecs(vecs.length);
int rowLayout = -1;
for (int c = 0; c < vecs.length; c++) {
AppendableVec vec = new AppendableVec(keys[c], Vec.T_NUM);
NewChunk chunk = new NewChunk(vec, 0);
for (double[] row : rows) chunk.addNum(row[c]);
chunk.close(0, fs);
if (rowLayout == -1)
rowLayout = vec.compute_rowLayout();
vecs[c] = vec.close(rowLayout, fs);
}
fs.blockForPending();
Frame fr = new Frame(key, names, vecs);
if (key != null)
DKV.put(key, fr);
return fr;
}
use of water.fvec.NewChunk in project h2o-3 by h2oai.
the class VecUtils method numericToStringVec.
/**
* Create a new {@link Vec} of string values from a numeric {@link Vec}.
*
* Currently only uses a default pretty printer. Would be better if
* it accepted a format string PUBDEV-2211
*
* @param src a numeric {@link Vec}
* @return a string {@link Vec}
*/
public static Vec numericToStringVec(Vec src) {
if (src.isCategorical() || src.isUUID())
throw new H2OIllegalValueException("Cannot convert a non-numeric column" + " using numericToStringVec() ", src);
Vec res = new MRTask() {
@Override
public void map(Chunk chk, NewChunk newChk) {
if (chk instanceof C0DChunk) {
// all NAs
for (int i = 0; i < chk._len; i++) newChk.addNA();
} else {
for (int i = 0; i < chk._len; i++) {
if (!chk.isNA(i))
newChk.addStr(PrettyPrint.number(chk, chk.atd(i), 4));
else
newChk.addNA();
}
}
}
}.doAll(Vec.T_STR, src).outputFrame().anyVec();
assert res != null;
return res;
}
use of water.fvec.NewChunk in project h2o-3 by h2oai.
the class VecUtils method stringToCategorical.
/**
* Create a new {@link Vec} of categorical values from string {@link Vec}.
*
* FIXME: implement in more efficient way with Brandon's primitives for BufferedString manipulation
*
* @param vec a string {@link Vec}
* @return a categorical {@link Vec}
*/
public static Vec stringToCategorical(Vec vec) {
final String[] vecDomain = new CollectStringVecDomain().domain(vec);
MRTask task = new MRTask() {
private transient java.util.HashMap<String, Integer> lookupTable;
@Override
protected void setupLocal() {
lookupTable = new java.util.HashMap<>(vecDomain.length);
for (int i = 0; i < vecDomain.length; i++) {
// FIXME: boxing
lookupTable.put(vecDomain[i], i);
}
}
@Override
public void map(Chunk c, NewChunk nc) {
BufferedString bs = new BufferedString();
for (int row = 0; row < c.len(); row++) {
if (c.isNA(row)) {
nc.addNA();
} else {
c.atStr(bs, row);
nc.addNum(lookupTable.get(bs.bytesToString()), 0);
}
}
}
};
// Invoke tasks - one input vector, one ouput vector
task.doAll(new byte[] { Vec.T_CAT }, vec);
// Return result
return task.outputFrame(null, null, new String[][] { vecDomain }).vec(0);
}
use of water.fvec.NewChunk in project h2o-2 by h2oai.
the class ASTFunc method map.
@Override
double[] map(Env env, double[] in, double[] out) {
final int sp = env._sp;
Key key = Vec.VectorGroup.VG_LEN1.addVecs(1)[0];
AppendableVec av = new AppendableVec(key);
NewChunk nc = new NewChunk(av, 0);
for (double v : in) nc.addNum(v);
nc.close(0, null);
Frame fr = new Frame(new String[] { "row" }, new Vec[] { av.close(null) });
env.push(this);
env.push(fr);
this.apply(env, 2, null);
if (env.isDbl()) {
if (out == null || out.length < 1)
out = new double[1];
out[0] = env.popDbl();
} else if (env.isAry()) {
fr = env.peekAry();
if (fr.vecs().length > 1)
H2O.unimpl();
Vec vec = fr.anyVec();
if (vec.length() > 1 << 8)
H2O.unimpl();
if (out == null || out.length < vec.length())
out = new double[(int) vec.length()];
for (long i = 0; i < vec.length(); i++) out[(int) i] = vec.at(i);
env.pop();
} else {
H2O.unimpl();
}
assert sp == env._sp;
return out;
}
Aggregations