Search in sources :

Example 21 with NewChunk

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();
}
Also used : Frame(water.fvec.Frame) ConfusionMatrix(hex.ConfusionMatrix) Chunk(water.fvec.Chunk) NewChunk(water.fvec.NewChunk) water(water) NewChunk(water.fvec.NewChunk) Vec(water.fvec.Vec) water.api(water.api)

Example 22 with NewChunk

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;
}
Also used : Frame(water.fvec.Frame) Vec(water.fvec.Vec) AppendableVec(water.fvec.AppendableVec) AppendableVec(water.fvec.AppendableVec) NewChunk(water.fvec.NewChunk)

Example 23 with NewChunk

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;
}
Also used : C0DChunk(water.fvec.C0DChunk) Vec(water.fvec.Vec) Chunk(water.fvec.Chunk) NewChunk(water.fvec.NewChunk) C0DChunk(water.fvec.C0DChunk) H2OIllegalValueException(water.exceptions.H2OIllegalValueException) NewChunk(water.fvec.NewChunk)

Example 24 with NewChunk

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);
}
Also used : java.util(java.util) NonBlockingHashMap(water.nbhm.NonBlockingHashMap) BufferedString(water.parser.BufferedString) BufferedString(water.parser.BufferedString) Chunk(water.fvec.Chunk) NewChunk(water.fvec.NewChunk) C0DChunk(water.fvec.C0DChunk) NewChunk(water.fvec.NewChunk)

Example 25 with NewChunk

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;
}
Also used : Frame(water.fvec.Frame) Vec(water.fvec.Vec) AppendableVec(water.fvec.AppendableVec) AppendableVec(water.fvec.AppendableVec) Key(water.Key) NewChunk(water.fvec.NewChunk)

Aggregations

NewChunk (water.fvec.NewChunk)42 Frame (water.fvec.Frame)34 Chunk (water.fvec.Chunk)32 ValFrame (water.rapids.vals.ValFrame)23 MRTask (water.MRTask)22 Vec (water.fvec.Vec)19 AppendableVec (water.fvec.AppendableVec)8 BufferedString (water.parser.BufferedString)6 Key (water.Key)5 Futures (water.Futures)4 MRTask2 (water.MRTask2)4 ValNum (water.rapids.vals.ValNum)4 C0DChunk (water.fvec.C0DChunk)3 Val (water.rapids.Val)2 ValRow (water.rapids.vals.ValRow)2 ConfusionMatrix (hex.ConfusionMatrix)1 MersenneTwisterRNG (hex.rng.MersenneTwisterRNG)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1