use of water.Key in project h2o-2 by h2oai.
the class FrameUtils method frame.
/**
* Create a new frame based on given row data.
* @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(String[] names, double[]... rows) {
assert names == null || names.length == rows[0].length;
Futures fs = new Futures();
Vec[] vecs = new Vec[rows[0].length];
Key[] keys = Vec.VectorGroup.VG_LEN1.addVecs(vecs.length);
for (int c = 0; c < vecs.length; c++) {
AppendableVec vec = new AppendableVec(keys[c]);
NewChunk chunk = new NewChunk(vec, 0);
for (int r = 0; r < rows.length; r++) chunk.addNum(rows[r][c]);
chunk.close(0, fs);
vecs[c] = vec.close(fs);
}
fs.blockForPending();
return new Frame(names, vecs);
}
use of water.Key in project h2o-2 by h2oai.
the class Env method remove_and_unlock.
// Remove everything
public void remove_and_unlock() {
// Remove all shallow scopes
while (_tod > 0) popScope();
// Push changes at the outer scope into the K/V store
while (_sp > 0) {
if (isAry() && _key[_sp - 1] != null) {
// Has a K/V mapping?
// Pop w/o lowering refcnt
Frame fr = popAry();
String skey = key();
Frame fr2 = new Frame(Key.make(skey), fr._names.clone(), fr.vecs().clone());
for (int i = 0; i < fr.numCols(); i++) {
Vec v = fr.vecs()[i];
int refcnt = _refcnt.get(v)._val;
assert refcnt > 0;
if (refcnt > 1) {
// Need a deep-copy now
Vec v2 = new Frame(v).deepSlice(null, null).vecs()[0];
// Replace with private deep-copy
fr2.replace(i, v2);
// Now lower refcnt for good assertions
subRef(v);
addRef(v2);
}
// But not down to zero (do not delete items in global scope)
}
if (// Upgrade to write-lock
_locked.contains(fr2._key))
// Upgrade to write-lock
fr2.write_lock(null);
else // Clear prior & set new data
{
fr2.delete_and_lock(null);
_locked.add(fr2._key);
}
fr2.unlock(null);
// Unlocked already
_locked.remove(fr2._key);
} else {
popUncheck();
}
}
// Unlock all things that do not survive, plus also delete them
for (Key k : _locked) {
Frame fr = UKV.get(k);
// Should be atomic really
fr.unlock(null);
// Should be atomic really
fr.delete();
}
}
use of water.Key in project h2o-3 by h2oai.
the class GLRMGridTest method testMultipleGridInvocation.
@Test
public void testMultipleGridInvocation() {
Grid<GLRMModel.GLRMParameters> grid = null;
Frame fr = null;
try {
fr = parse_test_file("smalldata/iris/iris_wheader.csv");
// Hyper-space
HashMap<String, Object[]> hyperParms = new HashMap<String, Object[]>() {
{
put("_k", new Integer[] { 2, 4 });
// Search over this range of the init enum
put("_transform", new DataInfo.TransformType[] { DataInfo.TransformType.NONE, DataInfo.TransformType.DEMEAN });
}
};
// Name of used hyper parameters
String[] hyperParamNames = hyperParms.keySet().toArray(new String[hyperParms.size()]);
Arrays.sort(hyperParamNames);
int hyperSpaceSize = ArrayUtils.crossProductSize(hyperParms);
// Create default parameters
GLRMModel.GLRMParameters params = new GLRMModel.GLRMParameters();
params._train = fr._key;
params._seed = 4224L;
params._loss = GlrmLoss.Absolute;
params._init = GlrmInitialization.SVD;
//
// Fire off a grid search multiple times with same key and make sure
// that results are same
//
final int ITER_CNT = 2;
Key<Model>[][] modelKeys = new Key[ITER_CNT][];
Key<Grid> gridKey = Key.make("GLRM_grid_iris" + Key.rand());
for (int i = 0; i < ITER_CNT; i++) {
Job<Grid> gs = GridSearch.startGridSearch(gridKey, params, hyperParms);
grid = (Grid<GLRMModel.GLRMParameters>) gs.get();
modelKeys[i] = grid.getModelKeys();
// Make sure number of produced models match size of specified hyper space
Assert.assertEquals("Size of grid should match to size of hyper space", hyperSpaceSize, grid.getModelCount() + grid.getFailureCount());
//
// Make sure that names of used parameters match
//
String[] gridHyperNames = grid.getHyperNames();
Arrays.sort(gridHyperNames);
Assert.assertArrayEquals("Hyper parameters names should match!", hyperParamNames, gridHyperNames);
}
Assert.assertArrayEquals("The model keys should be same between two iterations!", modelKeys[0], modelKeys[1]);
} finally {
if (fr != null) {
fr.remove();
}
if (grid != null) {
grid.remove();
}
}
}
use of water.Key in project h2o-3 by h2oai.
the class NaiveBayesTest method testIrisValidation.
@Test
public void testIrisValidation() throws InterruptedException, ExecutionException {
NaiveBayesModel model = null;
Frame fr = null, fr2 = null;
Frame tr = null, te = null;
try {
fr = parse_test_file("smalldata/iris/iris_wheader.csv");
SplitFrame sf = new SplitFrame(fr, new double[] { 0.5, 0.5 }, new Key[] { Key.make("train.hex"), Key.make("test.hex") });
// Invoke the job
sf.exec().get();
Key[] ksplits = sf._destination_frames;
tr = DKV.get(ksplits[0]).get();
te = DKV.get(ksplits[1]).get();
NaiveBayesParameters parms = new NaiveBayesParameters();
parms._train = ksplits[0];
parms._valid = ksplits[1];
// Need Laplace smoothing
parms._laplace = 0.01;
parms._response_column = fr._names[4];
parms._compute_metrics = true;
model = new NaiveBayes(parms).trainModel().get();
// Done building model; produce a score column with class assignments
fr2 = model.score(te);
Assert.assertTrue(model.testJavaScoring(te, fr2, 1e-6));
} finally {
if (fr != null)
fr.delete();
if (fr2 != null)
fr2.delete();
if (tr != null)
tr.delete();
if (te != null)
te.delete();
if (model != null)
model.delete();
}
}
use of water.Key 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