use of hex.glm.GLMModel.GLMParameters.Solver in project h2o-3 by h2oai.
the class GLMBasicTestBinomial method testNoInterceptWithOffsetAndWeights.
@Test
public void testNoInterceptWithOffsetAndWeights() {
Scope.enter();
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 };
// random observation weights, integers in 0 - 9 range
double[] weights_train = new double[] { 0, 6, 5, 4, 4, 8, 2, 4, 9, 5, 2, 0, 0, 4, 0, 0, 6, 3, 6, 5, 5, 5, 6, 0, 9, 9, 8, 6, 6, 5, 6, 1, 0, 6, 8, 6, 9, 2, 8, 0, 3, 0, 2, 3, 0, 2, 5, 0, 0, 3, 7, 4, 8, 4, 1, 9, 3, 7, 1, 3, 8, 6, 9, 5, 5, 1, 9, 5, 2, 1, 0, 6, 4, 0, 5, 3, 1, 2, 4, 0, 7, 9, 6, 8, 0, 2, 3, 7, 5, 8, 3, 4, 7, 8, 1, 2, 5, 7, 3, 7, 1, 1, 5, 7, 4, 9, 2, 6, 3, 5, 4, 9, 8, 1, 8, 5, 3, 0, 4, 5, 1, 2, 2, 7, 8, 3, 4, 9, 0, 1, 3, 9, 8, 7, 0, 8, 2, 7, 1, 9, 0, 7, 7, 5, 2, 9, 7, 6, 4, 3, 4, 6, 9, 1, 5, 0, 7, 9, 4, 1, 6, 8, 8, 5, 4, 2, 5, 9, 8, 1, 9, 2, 9, 2, 3, 0, 6, 7, 3, 2, 3, 0, 9, 5, 1, 8, 0, 2, 8, 6, 9, 5, 1, 2, 3, 1, 3, 5, 0, 7, 4, 0, 5, 5, 7, 9, 3, 0, 0, 0, 1, 5, 3, 2, 8, 9, 9, 1, 6, 2, 2, 0, 5, 5, 6, 2, 8, 8, 9, 8, 5, 0, 1, 5, 3, 0, 2, 5, 4, 0, 6, 5, 4, 5, 9, 7, 5, 6, 2, 2, 6, 2, 5, 1, 5, 9, 0, 3, 0, 2, 7, 0, 4, 7, 7, 9, 3, 7, 9, 7, 9, 6, 2, 6, 2, 2, 9, 0, 9, 8, 1, 2, 6, 3, 4, 1, 2, 2, 3, 0 };
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 weightsVecTrain = _prostateTrain.anyVec().makeZero();
try (Vec.Writer vw = weightsVecTrain.open()) {
for (int i = 0; i < weights_train.length; ++i) vw.set(i, weights_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]);
}
Frame fTrain = new Frame(Key.<Frame>make("prostate_with_offset_train"), new String[] { "offset", "weights" }, new Vec[] { offsetVecTrain, weightsVecTrain });
fTrain.add(_prostateTrain.names(), _prostateTrain.vecs());
DKV.put(fTrain);
Frame fTest = new Frame(Key.<Frame>make("prostate_with_offset_test"), new String[] { "offset" }, new Vec[] { offsetVecTest });
fTest.add(_prostateTest.names(), _prostateTest.vecs());
DKV.put(fTest);
// Call: glm(formula = CAPSULE ~ . - ID - RACE - DCAPS - DPROS - 1, family = binomial,
// data = train, weights = w, offset = offset_train)
//
// Coefficients:
// AGE PSA VOL GLEASON
// -0.070637 0.034939 -0.006326 0.645700
//
// Degrees of Freedom: 252 Total (i.e. Null); 248 Residual
// Null Deviance: 1494
// Residual Deviance: 1235 AIC: 1243
String[] cfs1 = new String[] { "Intercept", "AGE", "PSA", "VOL", "GLEASON" };
double[] vals = new double[] { 0, -0.070637, 0.034939, -0.006326, 0.645700 };
GLMParameters params = new GLMParameters(Family.binomial);
params._response_column = "CAPSULE";
params._ignored_columns = new String[] { "ID", "RACE", "DPROS", "DCAPS" };
params._train = fTrain._key;
params._offset_column = "offset";
params._weights_column = "weights";
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;
try {
for (Solver s : new Solver[] { Solver.IRLSM, Solver.L_BFGS, Solver.COORDINATE_DESCENT }) {
Frame scoreTrain = null, scoreTest = null;
try {
params._solver = s;
params._valid = fTest._key;
System.out.println("SOLVER = " + s);
try {
model = new GLM(params, Key.<GLMModel>make("prostate_model")).trainModel().get();
} catch (Exception iae) {
assertTrue(iae.getMessage().contains("Test/Validation dataset is missing weights column"));
}
params._valid = null;
model = new GLM(params, Key.<GLMModel>make("prostate_model")).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(1494, GLMTest.nullDeviance(model), 1);
assertEquals(1235, GLMTest.residualDeviance(model), 1);
assertEquals(252, GLMTest.nullDOF(model), 0);
assertEquals(248, GLMTest.resDOF(model), 0);
assertEquals(1243, GLMTest.aic(model), 1);
// 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"));
}
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()._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);
// // test the actual predictions
// Vec preds = scoreTest.vec("p1");
// for(int i = 0; i < pred_test.length; ++i)
// assertEquals(pred_test[i],preds.at(i),1e-6);
} finally {
if (model != null)
model.delete();
if (scoreTrain != null)
scoreTrain.delete();
if (scoreTest != null)
scoreTest.delete();
}
}
} finally {
DKV.remove(fTrain._key);
DKV.remove(fTest._key);
Scope.exit();
}
}
use of hex.glm.GLMModel.GLMParameters.Solver 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.Solver 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.Solver 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.Solver 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();
}
}
Aggregations