Example #1
0
def test_basic_successive_halving(c, s, a, b):
    # Most of the basics are tested through Hyperband (which relies on
    # successive halving)
    model = SGDClassifier(tol=1e-3)
    params = {"alpha": np.logspace(-3, 0, num=1000)}
    n, r = 10, 5
    search = SuccessiveHalvingSearchCV(model, params, n, r)

    X, y = make_classification()
    yield search.fit(X, y, classes=np.unique(y))
    assert search.best_score_ > 0
    assert isinstance(search.best_estimator_, SGDClassifier)
Example #2
0
def test_successive_halving_params(c, s, a, b):
    # Makes sure when SHAs are fit with values from the "SuccessiveHalvingSearchCV
    # params" key, the number of models/calls stay the same as Hyperband.

    # This sanity check again makes sure parameters passed correctly
    # (similar to `test_params_passed`)
    X, y = make_classification(n_samples=10, n_features=4, chunks=10)
    model = ConstantFunction()
    params = {"value": scipy.stats.uniform(0, 1)}
    alg = HyperbandSearchCV(model, params, max_iter=27, random_state=42)

    kwargs = [
        v["SuccessiveHalvingSearchCV params"] for v in alg.metadata["brackets"]
    ]
    SHAs = [SuccessiveHalvingSearchCV(model, params, **v) for v in kwargs]

    metadata = alg.metadata["brackets"]
    for k, (true_meta, SHA) in enumerate(zip(metadata, SHAs)):
        yield SHA.fit(X, y)
        n_models = len(SHA.model_history_)
        pf_calls = [
            v[-1]["partial_fit_calls"] for v in SHA.model_history_.values()
        ]
        assert true_meta["n_models"] == n_models
        assert true_meta["partial_fit_calls"] == sum(pf_calls)
Example #3
0
def test_search_patience_infeasible_tol(c, s, a, b):
    X, y = make_classification(n_samples=100, n_features=5)

    params = {"value": np.random.RandomState(42).rand(1000)}
    model = ConstantFunction()

    search = SuccessiveHalvingSearchCV(
        model,
        params,
        patience=2,
        tol=np.nan,
        n_initial_parameters=20,
        n_initial_iter=4,
        max_iter=1000,
    )
    yield search.fit(X, y, classes=[0, 1])

    assert search.metadata_["partial_fit_calls"] == search.metadata[
        "partial_fit_calls"]
    assert search.metadata_ == search.metadata
Example #4
0
    def _test_sha_max_iter(c, s, a, b):
        model = SGDClassifier(tol=1e-3)
        params = {"alpha": np.logspace(-3, 0, num=1000)}
        search = SuccessiveHalvingSearchCV(model,
                                           params,
                                           n_initial_parameters=n,
                                           n_initial_iter=r)

        X, y = make_classification()
        yield search.fit(X, y, classes=np.unique(y))

        calls = set(search.cv_results_["partial_fit_calls"]) - {1}
        assert min(calls) == r

        # One model trained to completion
        assert (search.cv_results_["partial_fit_calls"]
                == max(calls)).sum() < search.aggressiveness

        assert search.metadata == search.metadata_
        assert set(search.metadata.keys()) == {
            "partial_fit_calls",
            "n_models",
            "max_iter",
        }