use of hex.glm.GLMModel.GLMParameters in project h2o-3 by h2oai.
the class GLMBasicTestMultinomial method testCovtypeBasic.
@Test
public void testCovtypeBasic() {
GLMParameters params = new GLMParameters(Family.multinomial);
GLMModel model = null;
Frame preds = null;
Vec weights = _covtype.anyVec().makeCon(1);
Key k = Key.<Frame>make("cov_with_weights");
Frame f = new Frame(k, _covtype.names(), _covtype.vecs());
f.add("weights", weights);
DKV.put(f);
try {
params._response_column = "C55";
params._train = k;
params._valid = _covtype._key;
params._lambda = new double[] { 4.881e-05 };
params._alpha = new double[] { 1 };
params._objective_epsilon = 1e-6;
params._beta_epsilon = 1e-4;
params._weights_column = "weights";
params._missing_values_handling = DeepLearningModel.DeepLearningParameters.MissingValuesHandling.Skip;
double[] alpha = new double[] { 1 };
double[] expected_deviance = new double[] { 25499.76 };
double[] lambda = new double[] { 2.544750e-05 };
for (Solver s : new Solver[] { Solver.IRLSM, Solver.COORDINATE_DESCENT, Solver.L_BFGS }) {
System.out.println("solver = " + s);
params._solver = s;
params._max_iterations = params._solver == Solver.L_BFGS ? 300 : 10;
for (int i = 0; i < alpha.length; ++i) {
params._alpha[0] = alpha[i];
params._lambda[0] = lambda[i];
model = new GLM(params).trainModel().get();
System.out.println(model._output._model_summary);
System.out.println(model._output._training_metrics);
System.out.println(model._output._validation_metrics);
assertTrue(model._output._training_metrics.equals(model._output._validation_metrics));
assertTrue(((ModelMetricsMultinomialGLM) model._output._training_metrics)._resDev <= expected_deviance[i] * 1.1);
preds = model.score(_covtype);
ModelMetricsMultinomialGLM mmTrain = (ModelMetricsMultinomialGLM) hex.ModelMetricsMultinomial.getFromDKV(model, _covtype);
assertTrue(model._output._training_metrics.equals(mmTrain));
model.delete();
model = null;
preds.delete();
preds = null;
}
}
} finally {
weights.remove();
DKV.remove(k);
if (model != null)
model.delete();
if (preds != null)
preds.delete();
}
}
use of hex.glm.GLMModel.GLMParameters in project h2o-3 by h2oai.
the class GLMBasicTestMultinomial method testCovtypeNoIntercept.
@Test
public void testCovtypeNoIntercept() {
GLMParameters params = new GLMParameters(Family.multinomial);
GLMModel model = null;
Frame preds = null;
Vec weights = _covtype.anyVec().makeCon(1);
Key k = Key.<Frame>make("cov_with_weights");
Frame f = new Frame(k, _covtype.names(), _covtype.vecs());
f.add("weights", weights);
DKV.put(f);
try {
params._response_column = "C55";
params._train = k;
params._valid = _covtype._key;
params._objective_epsilon = 1e-6;
params._beta_epsilon = 1e-4;
params._weights_column = "weights";
params._missing_values_handling = DeepLearningModel.DeepLearningParameters.MissingValuesHandling.Skip;
params._intercept = false;
double[] alpha = new double[] { 0, .5, .1 };
Solver s = Solver.L_BFGS;
System.out.println("solver = " + s);
params._solver = s;
params._max_iterations = 5000;
for (int i = 0; i < alpha.length; ++i) {
params._alpha = new double[] { alpha[i] };
// params._lambda[0] = lambda[i];
model = new GLM(params).trainModel().get();
System.out.println(model.coefficients());
// Assert.assertEquals(0,model.coefficients().get("Intercept"),0);
double[][] bs = model._output.getNormBetaMultinomial();
for (double[] b : bs) Assert.assertEquals(0, b[b.length - 1], 0);
System.out.println(model._output._model_summary);
System.out.println(model._output._training_metrics);
System.out.println(model._output._validation_metrics);
preds = model.score(_covtype);
ModelMetricsMultinomialGLM mmTrain = (ModelMetricsMultinomialGLM) hex.ModelMetricsMultinomial.getFromDKV(model, _covtype);
assertTrue(model._output._training_metrics.equals(mmTrain));
model.delete();
model = null;
preds.delete();
preds = null;
}
} finally {
weights.remove();
DKV.remove(k);
if (model != null)
model.delete();
if (preds != null)
preds.delete();
}
}
use of hex.glm.GLMModel.GLMParameters in project h2o-3 by h2oai.
the class GLMBasicTestRegression method testWeights.
@Test
public void testWeights() {
GLMModel model1 = null, model2 = null;
GLMParameters parms = new GLMParameters(Family.gaussian);
parms._train = _weighted._key;
parms._ignored_columns = new String[] { _weighted.name(0) };
parms._response_column = _weighted.name(1);
parms._standardize = true;
parms._objective_epsilon = 0;
parms._gradient_epsilon = 1e-10;
parms._max_iterations = 1000;
for (Solver s : GLMParameters.Solver.values()) {
// if(s != Solver.IRLSM)continue; //fixme: does not pass for other than IRLSM now
System.out.println("===============================================================");
System.out.println("Solver = " + s);
System.out.println("===============================================================");
try {
parms._lambda = new double[] { 1e-5 };
parms._alpha = null;
parms._train = _weighted._key;
parms._solver = s;
parms._weights_column = "weights";
model1 = new GLM(parms).trainModel().get();
HashMap<String, Double> coefs1 = model1.coefficients();
System.out.println("coefs1 = " + coefs1);
parms._train = _upsampled._key;
parms._weights_column = null;
parms._lambda = new double[] { 1e-5 };
parms._alpha = null;
model2 = new GLM(parms).trainModel().get();
HashMap<String, Double> coefs2 = model2.coefficients();
System.out.println("coefs2 = " + coefs2);
System.out.println("mse1 = " + model1._output._training_metrics.mse() + ", mse2 = " + model2._output._training_metrics.mse());
System.out.println(model1._output._training_metrics);
System.out.println(model2._output._training_metrics);
assertEquals(model2._output._training_metrics.mse(), model1._output._training_metrics.mse(), 1e-4);
} finally {
if (model1 != null)
model1.delete();
if (model2 != null)
model2.delete();
}
}
}
use of hex.glm.GLMModel.GLMParameters in project h2o-3 by h2oai.
the class GLMBasicTestRegression method testOffset.
@Test
public void testOffset() {
GLMModel model1 = null, model2 = null;
GLMParameters parms = new GLMParameters(Family.gaussian);
parms._train = _weighted._key;
parms._ignored_columns = new String[] { _weighted.name(0) };
parms._response_column = _weighted.name(1);
parms._standardize = true;
parms._objective_epsilon = 0;
parms._gradient_epsilon = 1e-10;
parms._max_iterations = 1000;
Solver s = Solver.IRLSM;
try {
parms._lambda = new double[] { 0 };
parms._alpha = new double[] { 0 };
parms._train = _weighted._key;
parms._solver = s;
parms._offset_column = "C20";
parms._compute_p_values = true;
parms._standardize = false;
model1 = new GLM(parms).trainModel().get();
HashMap<String, Double> coefs1 = model1.coefficients();
System.out.println("coefs1 = " + coefs1);
/**
* Call:
glm(formula = C2 ~ . - C1 - C20, data = data, offset = data$C20)
Deviance Residuals:
Min 1Q Median 3Q Max
-3.444 -0.821 -0.021 0.878 2.801
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.026928 0.479281 -0.056 0.9553
C3 -0.064657 0.144517 -0.447 0.6558
C4 -0.076132 0.163746 -0.465 0.6432
C5 0.397962 0.161458 2.465 0.0158 *
C6 0.119644 0.173165 0.691 0.4916
C7 -0.124615 0.151145 -0.824 0.4121
C8 0.142455 0.164912 0.864 0.3902
C9 0.087358 0.158266 0.552 0.5825
C10 -0.012873 0.155429 -0.083 0.9342
C11 0.277392 0.181299 1.530 0.1299
C12 0.004988 0.170290 0.029 0.9767
C13 -0.091400 0.172910 -0.529 0.5985
C14 -0.248876 0.177311 -1.404 0.1643
C15 0.053598 0.167305 0.320 0.7495
C16 0.156302 0.157823 0.990 0.3249
C17 0.296317 0.167453 1.770 0.0806 .
C18 0.013306 0.162185 0.082 0.9348
C19 0.115939 0.160250 0.723 0.4715
weights -0.005771 0.303477 -0.019 0.9849
*
*/
double[] expected_coefs = new double[] { -0.064656782, -0.076131880, 0.397962147, 0.119644094, -0.124614842, 0.142455018, 0.087357855, -0.012872522, 0.277392182, 0.004987961, -0.091400128, -0.248875970, 0.053597896, 0.156301780, 0.296317472, 0.013306398, 0.115938809, -0.005771429, -0.026928297 };
double[] expected_pvals = new double[] { 0.65578062, 0.64322317, 0.01582348, 0.49158786, 0.41209217, 0.39023637, 0.58248959, 0.93419972, 0.12990598, 0.97670462, 0.59852911, 0.16425679, 0.74951951, 0.32494727, 0.08056447, 0.93481349, 0.47146503, 0.98487376, 0.95533301 };
double[] actual_coefs = model1.beta();
double[] actual_pvals = model1._output.pValues();
for (int i = 0; i < expected_coefs.length; ++i) {
assertEquals(expected_coefs[i], actual_coefs[i], 1e-4);
assertEquals(expected_pvals[i], actual_pvals[i], 1e-4);
}
} finally {
if (model1 != null)
model1.delete();
if (model2 != null)
model2.delete();
}
}
use of hex.glm.GLMModel.GLMParameters in project h2o-3 by h2oai.
the class GLMBasicTestBinomial method testNoInterceptWithOffset.
@Test
public void testNoInterceptWithOffset() {
GLMModel model = null;
double[] offset_train = new double[] { -0.39771185, +1.20479170, -0.16374109, -0.97885903, -1.42996530, +0.83474893, +0.83474893, -0.74488827, +0.83474893, +0.86851236, +1.41589611, +1.41589611, -1.42996530, -0.39771185, -2.01111248, -0.39771185, -0.16374109, +0.62364452, -0.39771185, +0.60262749, -0.06143251, -1.42996530, -0.06143251, -0.06143251, +0.14967191, -0.06143251, -0.39771185, +0.14967191, +1.20479170, -0.39771185, -0.16374109, -0.06143251, -0.06143251, -1.42996530, -0.39771185, -0.39771185, -0.64257969, +1.65774729, -0.97885903, -0.39771185, -0.39771185, -0.39771185, -1.42996530, +1.41589611, -0.06143251, -0.06143251, -0.39771185, -0.06143251, -0.06143251, -0.39771185, -0.06143251, +0.14967191, -0.39771185, -1.42996530, -0.39771185, -0.64257969, -0.39771185, -0.06143251, -0.06143251, -0.06143251, -1.42996530, -2.01111248, -0.06143251, -0.39771185, -0.39771185, -1.42996530, -0.39771185, -1.42996530, -0.06143251, +1.41589611, +0.14967191, -1.42996530, -1.42996530, -0.06143251, -1.42996530, -1.42996530, -0.06143251, -1.42996530, -0.06143251, -0.39771185, -0.06143251, -1.42996530, -0.06143251, -0.39771185, -1.42996530, -0.06143251, -0.06143251, -0.06143251, -1.42996530, -0.39771185, -1.42996530, -0.43147527, -0.39771185, -0.39771185, -0.39771185, -1.42996530, -1.42996530, -0.43147527, -0.39771185, -0.39771185, -0.39771185, -0.39771185, -1.42996530, -1.42996530, -1.42996530, -0.39771185, +0.14967191, +1.41589611, -1.42996530, +1.41589611, -1.42996530, +1.41589611, -0.06143251, +0.14967191, -0.39771185, -0.97885903, -1.42996530, -0.39771185, -0.39771185, -0.39771185, -0.39771185, -1.42996530, -0.39771185, -0.97885903, -0.06143251, -0.06143251, +0.86851236, -0.39771185, -0.39771185, -0.06143251, -0.39771185, -0.39771185, -0.06143251, +0.14967191, -1.42996530, -1.42996530, -0.39771185, +1.20479170, -1.42996530, -0.39771185, -0.06143251, -1.42996530, -0.97885903, +0.14967191, +0.14967191, -1.42996530, -1.42996530, -0.39771185, -0.06143251, -0.43147527, -0.06143251, -0.39771185, -1.42996530, -0.06143251, -0.39771185, -0.39771185, -1.42996530, -0.39771185, -0.39771185, -0.06143251, -0.39771185, -0.39771185, +0.14967191, -0.06143251, +1.41589611, -0.06143251, -0.39771185, -0.39771185, -0.06143251, -1.42996530, -0.06143251, -1.42996530, -0.39771185, -0.64257969, -0.06143251, +1.20479170, -0.43147527, -0.97885903, -0.39771185, -0.39771185, -0.39771185, +0.14967191, -2.01111248, -1.42996530, -0.06143251, +0.83474893, -1.42996530, -1.42996530, -2.01111248, -1.42996530, -0.06143251, +0.86851236, +0.05524374, -0.39771185, -0.39771185, -0.39771185, +1.41589611, -1.42996530, -0.39771185, -1.42996530, -0.39771185, -0.39771185, -0.06143251, +0.14967191, -1.42996530, -0.39771185, -1.42996530, -1.42996530, -0.39771185, -0.39771185, -0.06143251, -1.42996530, -0.97885903, -1.42996530, -0.39771185, -0.06143251, -0.39771185, -0.06143251, -1.42996530, -1.42996530, -0.06143251, -1.42996530, -0.39771185, +0.14967191, -0.06143251, -1.42996530, -1.42996530, +0.14967191, -0.39771185, -0.39771185, -1.42996530, -0.06143251, -0.06143251, -1.42996530, -0.06143251, -1.42996530, +0.14967191, +1.20479170, -1.42996530, -0.06143251, -0.39771185, -0.39771185, -0.06143251, +0.14967191, -0.06143251, -1.42996530, -1.42996530, -1.42996530, -0.39771185, -0.39771185, -0.39771185, +0.86851236, -0.06143251, -0.97885903, -0.06143251, -0.64257969, +0.14967191, +0.86851236, -0.39771185, -0.39771185, -0.39771185, -0.64257969, -1.42996530, -0.06143251, -0.39771185, -0.39771185, -1.42996530, -1.42996530, -0.06143251, +0.14967191, -0.06143251, +0.86851236, -0.97885903, -1.42996530, -1.42996530, -1.42996530, -1.42996530, +0.86851236, +0.14967191, -1.42996530, -0.97885903, -1.42996530, -1.42996530, -0.06143251, +0.14967191, -1.42996530, -0.64257969, -2.01111248, -0.97885903, -0.39771185 };
double[] offset_test = new double[] { +1.65774729, -0.97700971, -0.97700971, -0.97700971, +0.05524374, +0.05524374, +0.05524374, +0.05524374, +0.39152308, +0.39152308, +0.39152308, +0.05524374, +0.05524374, +0.05524374, +0.39152308, -0.97700971, +0.05524374, +1.32146795, +0.39152308, +1.65774729, -0.97700971, +1.65774729, +0.39152308, +0.39152308, +1.65774729, +0.60262749, +0.05524374, +0.05524374, +0.05524374, +0.60262749, +0.05524374, -0.97700971, -0.97885903, +0.05524374, -2.01111248, -0.97700971, +0.05524374, +0.39152308, +0.05524374, +0.60262749, +0.60262749, +0.39152308, +0.60262749, -0.97700971, +0.39152308, +1.65774729, +0.39152308, +0.39152308, +0.05524374, +1.86885170, +0.05524374, -0.97700971, +0.60262749, -0.97700971, +0.60262749, -0.97700971, +0.39152308, -0.97700971, -0.43147527, +1.32146795, +0.05524374, +0.05524374, +0.39152308, +0.39152308, +0.05524374, +0.39152308, -0.97700971, +0.05524374, +0.39152308, +0.05524374, +0.60262749, +1.86885170, +0.05524374, +0.05524374, +1.86885170, +0.60262749, -0.64257969, -0.97700971, +0.60262749, +0.39152308, -0.97700971, -0.97700971, +0.05524374, -0.97700971, -0.97700971, +0.05524374, +0.05524374, +0.60262749, +0.05524374, +0.05524374 };
double[] pred_test = new double[] { +0.88475366, +0.23100271, +0.40966315, +0.08957188, +0.47333302, +0.44622513, +0.56450046, +0.74271010, +0.45129280, +0.72359111, +0.67918401, +0.19882802, +0.42330391, +0.62734862, +0.38055506, +0.47286476, +0.40180469, +0.97907526, +0.61428344, +0.97109299, +0.30489181, +0.81303545, +0.36130639, +0.65434899, +0.98863675, +0.58301866, +0.37950467, +0.53679205, +0.30636941, +0.70320372, +0.45303278, +0.35011042, +0.78165074, +0.44915160, +0.09008065, +0.16789833, +0.45748862, +0.59328118, +0.75002334, +0.35170410, +0.57550279, +0.42038237, +0.76349569, +0.28883753, +0.84824847, +0.72396381, +0.56782477, +0.54078190, +0.51169047, +0.80828547, +0.52001699, +0.26202346, +0.81014557, +0.29986016, +0.62011569, +0.33034872, +0.62284802, +0.28303618, +0.38470707, +0.96444405, +0.36155179, +0.46368503, +0.65192144, +0.43597041, +0.30906461, +0.69259415, +0.21819579, +0.49998652, +0.57162728, +0.44255738, +0.80820564, +0.90616782, +0.49377901, +0.34235025, +0.99621673, +0.65768252, +0.43909050, +0.23205826, +0.71124897, +0.42908417, +0.47880901, +0.29185818, +0.42648317, +0.01247279, +0.18372518, +0.27281535, +0.63807876, +0.44563524, +0.32821696, +0.43636099 };
Vec offsetVecTrain = _prostateTrain.anyVec().makeZero();
try (Vec.Writer vw = offsetVecTrain.open()) {
for (int i = 0; i < offset_train.length; ++i) vw.set(i, offset_train[i]);
}
Vec offsetVecTest = _prostateTest.anyVec().makeZero();
try (Vec.Writer vw = offsetVecTest.open()) {
for (int i = 0; i < offset_test.length; ++i) vw.set(i, offset_test[i]);
}
Key fKeyTrain = Key.make("prostate_with_offset_train");
Key fKeyTest = Key.make("prostate_with_offset_test");
Frame fTrain = new Frame(fKeyTrain, new String[] { "offset" }, new Vec[] { offsetVecTrain });
fTrain.add(_prostateTrain.names(), _prostateTrain.vecs());
DKV.put(fKeyTrain, fTrain);
Frame fTest = new Frame(fKeyTest, new String[] { "offset" }, new Vec[] { offsetVecTest });
fTest.add(_prostateTest.names(), _prostateTest.vecs());
DKV.put(fKeyTest, fTest);
// Call: glm(formula = CAPSULE ~ . - ID - RACE - DCAPS - DPROS - 1, family = binomial,
// data = train, offset = offset_train)
//
// Coefficients:
// AGE PSA VOL GLEASON
// -0.054102 0.027517 -0.008937 0.516363
//
// Degrees of Freedom: 290 Total (i.e. Null); 286 Residual
// Null Deviance: 355.7
// Residual Deviance: 313 AIC: 321
String[] cfs1 = new String[] { "Intercept", "AGE", "PSA", "VOL", "GLEASON" };
double[] vals = new double[] { 0, -0.054102, 0.027517, -0.008937, 0.516363 };
GLMParameters params = new GLMParameters(Family.binomial);
params._response_column = "CAPSULE";
params._ignored_columns = new String[] { "ID", "RACE", "DPROS", "DCAPS" };
params._train = fKeyTrain;
params._valid = fKeyTest;
params._offset_column = "offset";
params._lambda = new double[] { 0 };
params._alpha = new double[] { 0 };
params._standardize = false;
params._objective_epsilon = 0;
params._gradient_epsilon = 1e-6;
// not expected to reach max iterations here
params._max_iterations = 100;
params._intercept = false;
params._beta_epsilon = 1e-6;
params._missing_values_handling = MissingValuesHandling.Skip;
try {
for (Solver s : new Solver[] { Solver.AUTO, Solver.IRLSM, Solver.L_BFGS, Solver.COORDINATE_DESCENT }) {
Frame scoreTrain = null, scoreTest = null;
try {
params._solver = s;
System.out.println("SOLVER = " + s);
model = new GLM(params).trainModel().get();
HashMap<String, Double> coefs = model.coefficients();
System.out.println("coefs = " + coefs);
boolean CD = s == Solver.COORDINATE_DESCENT;
for (int i = 0; i < cfs1.length; ++i) assertEquals(vals[i], coefs.get(cfs1[i]), CD ? 1e-2 : 1e-4);
assertEquals(355.7, GLMTest.nullDeviance(model), 1e-1);
assertEquals(313.0, GLMTest.residualDeviance(model), 1e-1);
assertEquals(290, GLMTest.nullDOF(model), 0);
assertEquals(286, GLMTest.resDOF(model), 0);
assertEquals(321, GLMTest.aic(model), 1e-1);
assertEquals(88.72363, GLMTest.residualDevianceTest(model), CD ? 1e-2 : 1e-4);
// test scoring
try {
scoreTrain = model.score(_prostateTrain);
assertTrue("shoul've thrown IAE", false);
} catch (IllegalArgumentException iae) {
assertTrue(iae.getMessage().contains("Test/Validation dataset is missing offset column"));
}
hex.ModelMetricsBinomialGLM mmTrain = (ModelMetricsBinomialGLM) hex.ModelMetricsBinomial.getFromDKV(model, fTrain);
hex.AUC2 adata = mmTrain._auc;
assertEquals(model._output._training_metrics.auc_obj()._auc, adata._auc, 1e-8);
assertEquals(model._output._training_metrics._MSE, mmTrain._MSE, 1e-8);
assertEquals(((ModelMetricsBinomialGLM) model._output._training_metrics)._resDev, mmTrain._resDev, 1e-8);
scoreTrain = model.score(fTrain);
mmTrain = (ModelMetricsBinomialGLM) hex.ModelMetricsBinomial.getFromDKV(model, fTrain);
adata = mmTrain._auc;
assertEquals(model._output._training_metrics.auc_obj()._auc, adata._auc, 1e-8);
assertEquals(model._output._training_metrics._MSE, mmTrain._MSE, 1e-8);
assertEquals(((ModelMetricsBinomialGLM) model._output._training_metrics)._resDev, mmTrain._resDev, 1e-8);
scoreTest = model.score(fTest);
ModelMetricsBinomialGLM mmTest = (ModelMetricsBinomialGLM) hex.ModelMetricsBinomial.getFromDKV(model, fTest);
adata = mmTest._auc;
assertEquals(model._output._validation_metrics.auc_obj()._auc, adata._auc, 1e-8);
assertEquals(model._output._validation_metrics._MSE, mmTest._MSE, 1e-8);
assertEquals(((ModelMetricsBinomialGLM) model._output._validation_metrics)._resDev, mmTest._resDev, 1e-8);
GLMTest.testScoring(model, fTest);
// test the actual predictions
Vec.Reader preds = scoreTest.vec("p1").new Reader();
for (int i = 0; i < pred_test.length; ++i) // s == Solver.COORDINATE_DESCENT_NAIVE
assertEquals(pred_test[i], preds.at(i), CD ? 1e-3 : 1e-6);
} finally {
if (model != null)
model.delete();
if (scoreTrain != null)
scoreTrain.delete();
if (scoreTest != null)
scoreTest.delete();
}
}
} finally {
if (fTrain != null) {
fTrain.remove("offset").remove();
DKV.remove(fTrain._key);
}
if (fTest != null) {
fTest.remove("offset").remove();
DKV.remove(fTest._key);
}
}
}
Aggregations