def test_generic_estimator_with_distribution_param():
    # Note that in normal situations (release build), init_connection_args can be omitted
    # otherwise, it should be set to the first H2O element in the pipeline
    pipeline = Pipeline([('svd', TruncatedSVD(n_components=3,
                                              random_state=seed)),
                         ('estimator',
                          H2OGradientBoostingEstimator(
                              distribution='bernoulli',
                              seed=seed,
                              init_connection_args=init_connection_args))])
    data = _get_data(format='numpy')
    assert isinstance(data.X_train, np.ndarray)
    pipeline.fit(data.X_train, data.y_train)
    preds = pipeline.predict(data.X_test)
    assert isinstance(preds, np.ndarray)
    assert preds.shape == (len(data.X_test), )
    probs = pipeline.predict_proba(data.X_test)
    assert probs.shape == (len(data.X_test), 2)
    assert np.allclose(np.sum(probs, axis=1),
                       1.), "`predict_proba` didn't return probabilities"

    score = pipeline.score(data.X_test, data.y_test)
    assert isinstance(score, float)
    skl_score = accuracy_score(data.y_test, preds)
    assert abs(score - skl_score) < 1e-6
    scores['generic_estimator_with_distribution_param'] = score
Esempio n. 2
0
def test_params_are_correctly_passed_to_underlying_estimator():
    estimator = H2OGradientBoostingEstimator(seed=seed)
    estimator.set_params(max_depth=10, learn_rate=0.5)
    estimator.model_id = "dummy"
    assert estimator.estimator is None
    estimator._make_estimator()  # normally done when calling `fit`
    real_estimator = estimator.estimator
    assert real_estimator
    parms = real_estimator._parms
    assert real_estimator.seed == parms['seed'] == seed
    assert real_estimator.max_depth == parms['max_depth'] == 10
    assert real_estimator.learn_rate == parms['learn_rate'] == 0.5
    assert real_estimator._id == parms['model_id'] == "dummy"
    assert real_estimator.training_frame == parms['training_frame'] is None
Esempio n. 3
0
def test_all_params_can_be_set_as_properties():
    pipeline = Pipeline([('standardize', H2OScaler()), ('pca', H2OPCA()),
                         ('estimator', H2OGradientBoostingEstimator())])
    pipeline.named_steps.standardize.center = True
    pipeline.named_steps.standardize.scale = False
    pipeline.named_steps.pca.k = 2
    pipeline.named_steps.pca.seed = seed
    pipeline.named_steps.estimator.ntrees = 20
    pipeline.named_steps.estimator.max_depth = 5
    pipeline.named_steps.estimator.seed = seed
    params = pipeline.get_params()
    assert isinstance(params['standardize'], H2OScaler)
    assert params['standardize__center'] is True
    assert params['standardize__scale'] is False
    assert isinstance(params['pca'], H2OPCA)
    assert params['pca__k'] == 2
    assert params['pca__seed'] == seed
    assert isinstance(params['estimator'], H2OGradientBoostingEstimator)
    assert params['estimator__ntrees'] == 20
    assert params['estimator__max_depth'] == 5
    assert params['estimator__seed'] == seed
Esempio n. 4
0
def test_all_params_can_be_set_using_set_params():
    pipeline = Pipeline([('standardize', H2OScaler()), ('pca', H2OPCA()),
                         ('estimator', H2OGradientBoostingEstimator())])
    pipeline.set_params(standardize__center=True,
                        standardize__scale=False,
                        pca__k=2,
                        pca__seed=seed,
                        estimator__ntrees=20,
                        estimator__max_depth=5,
                        estimator__seed=seed)
    assert isinstance(pipeline.named_steps.standardize, H2OScaler)
    assert pipeline.named_steps.standardize.center is True
    assert pipeline.named_steps.standardize.scale is False
    assert isinstance(pipeline.named_steps.pca, H2OPCA)
    assert pipeline.named_steps.pca.k == 2
    assert pipeline.named_steps.pca.seed == seed
    assert isinstance(pipeline.named_steps.estimator,
                      H2OGradientBoostingEstimator)
    assert pipeline.named_steps.estimator.ntrees == 20
    assert pipeline.named_steps.estimator.max_depth == 5
    assert pipeline.named_steps.estimator.seed == seed
Esempio n. 5
0
def test_all_params_are_accessible_as_properties():
    pipeline = Pipeline([('standardize', H2OScaler(center=True, scale=False)),
                         ('pca', H2OPCA(k=2, seed=seed)),
                         ('estimator',
                          H2OGradientBoostingEstimator(ntrees=20,
                                                       max_depth=5,
                                                       seed=seed))])
    assert isinstance(pipeline.named_steps.standardize, H2OScaler)
    assert pipeline.named_steps.standardize.center is True
    assert pipeline.named_steps.standardize.scale is False
    assert isinstance(pipeline.named_steps.pca, H2OPCA)
    assert pipeline.named_steps.pca.k == 2
    assert pipeline.named_steps.pca.seed == seed
    assert isinstance(pipeline.named_steps.estimator,
                      H2OGradientBoostingEstimator)
    assert pipeline.named_steps.estimator.ntrees == 20
    assert pipeline.named_steps.estimator.max_depth == 5
    assert pipeline.named_steps.estimator.seed == seed
    # also the ones that were not set explicitly
    assert pipeline.named_steps.pca.max_iterations is None
    assert pipeline.named_steps.estimator.learn_rate is None
Esempio n. 6
0
def test_all_params_are_visible_in_get_params():
    pipeline = Pipeline([('standardize', H2OScaler(center=True, scale=False)),
                         ('pca', H2OPCA(k=2, seed=seed)),
                         ('estimator',
                          H2OGradientBoostingEstimator(ntrees=20,
                                                       max_depth=5,
                                                       seed=seed))])
    params = pipeline.get_params()
    assert isinstance(params['standardize'], H2OScaler)
    assert params['standardize__center'] is True
    assert params['standardize__scale'] is False
    assert isinstance(params['pca'], H2OPCA)
    assert params['pca__k'] == 2
    assert params['pca__seed'] == seed
    assert isinstance(params['estimator'], H2OGradientBoostingEstimator)
    assert params['estimator__ntrees'] == 20
    assert params['estimator__max_depth'] == 5
    assert params['estimator__seed'] == seed
    # also the ones that were not set explicitly
    assert params['pca__max_iterations'] is None
    assert params['estimator__learn_rate'] is None
def test_generic_estimator_with_distribution_param():
    # Note that in normal situations (release build), init_connection_args can be omitted
    # otherwise, it should be set to the first H2O element in the pipeline
    pipeline = Pipeline([('standardize', StandardScaler()),
                         ('pca', PCA(n_components=2, random_state=seed)),
                         ('estimator',
                          H2OGradientBoostingEstimator(
                              distribution='gaussian',
                              seed=seed,
                              init_connection_args=init_connection_args))])
    data = _get_data(format='numpy')
    assert isinstance(data.X_train, np.ndarray)
    pipeline.fit(data.X_train, data.y_train)
    preds = pipeline.predict(data.X_test)
    assert isinstance(preds, np.ndarray)
    assert preds.shape == (len(data.X_test), )

    score = pipeline.score(data.X_test, data.y_test)
    assert isinstance(score, float)
    skl_score = r2_score(data.y_test, preds)
    assert abs(score - skl_score) < 1e-6
    scores['generic_estimator_with_distribution_param'] = score