use of hex.drf.DRF.DRFModel in project h2o-2 by h2oai.
the class DRFTest2 method testBalanceWithCrossValidation.
@Override
protected void testBalanceWithCrossValidation(String dataset, int response, int[] ignored_cols, int ntrees, int nfolds) {
Frame f = parseFrame(dataset);
DRFModel model = null;
DRF drf = new DRF();
try {
Vec respVec = f.vec(response);
// Build a model
drf.source = f;
drf.response = respVec;
drf.ignored_cols = ignored_cols;
drf.classification = true;
drf.ntrees = ntrees;
drf.seed = 42;
drf.balance_classes = true;
drf.n_folds = nfolds;
drf.keep_cross_validation_splits = false;
drf.invoke();
Assert.assertEquals("Number of cross validation model is wrond!", nfolds, drf.xval_models.length);
model = UKV.get(drf.dest());
//HEX-1817
Assert.assertTrue(model.get_params().state == Job.JobState.DONE);
} finally {
if (f != null)
f.delete();
if (model != null) {
if (drf.xval_models != null) {
for (Key k : drf.xval_models) {
Model m = UKV.get(k);
m.delete();
}
}
model.delete();
}
}
}
use of hex.drf.DRF.DRFModel in project h2o-2 by h2oai.
the class DRFModelAdaptTest method testModelAdaptation.
void testModelAdaptation(String train, String test, PrepData dprep, boolean exactAdaptation) {
DRFModel model = null;
Frame frTest = null;
Frame frTrain = null;
Key trainKey = Key.make("train.hex");
Key testKey = Key.make("test.hex");
Frame[] frAdapted = null;
try {
// Prepare a simple model
frTrain = parseFrame(trainKey, train);
model = runDRF(frTrain, dprep);
// Load test dataset - test data contains input columns matching train data,
// BUT each input requires adaptation. Moreover, test data contains additional columns
// containing correct value mapping.
frTest = parseFrame(testKey, test);
Assert.assertEquals("TEST CONF ERROR: The test dataset should contain 2*<number of input columns>+1!", 2 * (frTrain.numCols() - 1) + 1, frTest.numCols());
// Adapt test dataset
// do/do not perform translation to enums
frAdapted = model.adapt(frTest, exactAdaptation);
Assert.assertEquals("Adapt method should return two frames", 2, frAdapted.length);
Assert.assertEquals("Test expects that all columns in test dataset has to be adapted", dprep.needAdaptation(frTrain), frAdapted[1].numCols());
// Compare vectors
Frame adaptedFrame = frAdapted[0];
for (int av = 0; av < frTrain.numCols() - 1; av++) {
int ev = av + frTrain.numCols();
Vec actV = adaptedFrame.vecs()[av];
Vec expV = frTest.vecs()[ev];
Assert.assertEquals("Different number of rows in test vectors", expV.length(), actV.length());
for (long r = 0; r < expV.length(); r++) {
if (expV.isNA(r))
Assert.assertTrue("Badly adapted vector - expected NA! Col: " + av + ", row: " + r, actV.isNA(r));
else {
Assert.assertTrue("Badly adapted vector - expected value but get NA! Col: " + av + ", row: " + r, !actV.isNA(r));
Assert.assertEquals("Badly adapted vector - wrong values! Col: " + av + ", row: " + r, expV.at8(r), actV.at8(r));
}
}
}
} finally {
// Test cleanup
if (model != null)
model.delete();
if (frTrain != null)
frTrain.delete();
if (frTest != null)
frTest.delete();
// Remove adapted vectors which were saved into KV-store, rest of vectors are remove by frTest.remove()
if (frAdapted != null)
frAdapted[1].delete();
}
}
use of hex.drf.DRF.DRFModel in project h2o-2 by h2oai.
the class DRFTest method testReproducibility.
@Test
public void testReproducibility() {
Frame tfr = null;
final int N = 5;
double[] mses = new double[N];
Scope.enter();
try {
// Load data, hack frames
tfr = parseFrame(Key.make("air.hex"), "./smalldata/covtype/covtype.20k.data");
// rebalance to 256 chunks
Key dest = Key.make("df.rebalanced.hex");
RebalanceDataSet rb = new RebalanceDataSet(tfr, dest, 256);
H2O.submitTask(rb);
rb.join();
tfr.delete();
tfr = DKV.get(dest).get();
for (int i = 0; i < N; ++i) {
DRF parms = new DRF();
parms.source = tfr;
parms.response = tfr.lastVec();
parms.nbins = 1000;
parms.ntrees = 1;
parms.max_depth = 8;
parms.mtries = -1;
parms.min_rows = 10;
parms.classification = false;
parms.seed = 1234;
// Build a first model; all remaining models should be equal
DRFModel drf = parms.fork().get();
mses[i] = drf.mse();
drf.delete();
}
} finally {
if (tfr != null)
tfr.delete();
}
Scope.exit();
for (int i = 0; i < mses.length; ++i) {
Log.info("trial: " + i + " -> mse: " + mses[i]);
}
for (int i = 0; i < mses.length; ++i) {
assertEquals(mses[i], mses[0], 1e-15);
}
}
use of hex.drf.DRF.DRFModel in project h2o-2 by h2oai.
the class ModelSerializationTest method testDRFModelMultinomial.
@Test
public void testDRFModelMultinomial() throws IOException {
DRFModel model = null, loadedModel = null;
try {
model = prepareDRFModel("smalldata/iris/iris.csv", EIA, 4, true, 5);
loadedModel = saveAndLoad(model);
// And compare
assertTreeModelEquals(model, loadedModel);
assertModelBinaryEquals(model, loadedModel);
} finally {
if (model != null)
model.delete();
if (loadedModel != null)
loadedModel.delete();
}
}