Python sklearn.utils.estimator_checks.check_estimator() Examples

The following are 30 code examples of sklearn.utils.estimator_checks.check_estimator(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.utils.estimator_checks , or try the search function .
Example #1
Source File: test_mtl.py    From celer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_MultiTaskLasso(fit_intercept):
    """Test that our MultiTaskLasso behaves as sklearn's."""
    X, Y = build_dataset(n_samples=20, n_features=30, n_targets=10)
    alpha_max = np.max(norm(X.T.dot(Y), axis=1)) / X.shape[0]

    alpha = alpha_max / 2.
    params = dict(alpha=alpha, fit_intercept=fit_intercept, tol=1e-10,
                  normalize=True)
    clf = MultiTaskLasso(**params)
    clf.verbose = 2
    clf.fit(X, Y)

    clf2 = sklearn_MultiTaskLasso(**params)
    clf2.fit(X, Y)
    np.testing.assert_allclose(clf.coef_, clf2.coef_, rtol=1e-5)
    if fit_intercept:
        np.testing.assert_allclose(clf.intercept_, clf2.intercept_)

    clf.tol = 1e-7
    check_estimator(clf) 
Example #2
Source File: test_mtl.py    From celer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_MultiTaskLassoCV():
    """Test that our MultitaskLassoCV behaves like sklearn's."""
    X, y = build_dataset(n_samples=30, n_features=50, n_targets=3)

    params = dict(eps=1e-2, n_alphas=10, tol=1e-10, cv=2, n_jobs=1,
                  fit_intercept=False, verbose=2)

    clf = MultiTaskLassoCV(**params)
    clf.fit(X, y)

    clf2 = sklearn_MultiTaskLassoCV(**params)
    clf2.max_iter = 10000  # increase max_iter bc of low tol
    clf2.fit(X, y)

    np.testing.assert_allclose(clf.mse_path_, clf2.mse_path_,
                               atol=1e-4, rtol=1e-04)
    np.testing.assert_allclose(clf.alpha_, clf2.alpha_,
                               atol=1e-4, rtol=1e-04)
    np.testing.assert_allclose(clf.coef_, clf2.coef_,
                               atol=1e-4, rtol=1e-04)

    # check_estimator tests float32 so using tol < 1e-7 causes precision
    # issues
    clf.tol = 1e-5
    check_estimator(clf) 
Example #3
Source File: test_lasso.py    From celer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_LassoCV(sparse_X, fit_intercept, positive):
    """Test that our LassoCV behaves like sklearn's LassoCV."""

    X, y = build_dataset(n_samples=20, n_features=30, sparse_X=sparse_X)
    params = dict(eps=0.05, n_alphas=10, tol=1e-10, cv=2,
                  fit_intercept=fit_intercept, positive=positive, verbose=2,
                  n_jobs=-1)

    clf = LassoCV(**params)
    clf.fit(X, y)

    clf2 = sklearn_LassoCV(**params, max_iter=10000)
    clf2.fit(X, y)

    np.testing.assert_allclose(clf.mse_path_, clf2.mse_path_, atol=1e-4)
    np.testing.assert_allclose(clf.alpha_, clf2.alpha_)
    np.testing.assert_allclose(clf.coef_, clf2.coef_, atol=1e-5)

    # TODO this one is slow (3s * 8 tests). Pass an instance and icnrease tol
    # check_estimator(LassoCV) 
Example #4
Source File: test_autoencoder.py    From muffnn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, hidden_units=(256,), batch_size=128, n_epochs=300,
                 keep_prob=1.0, hidden_activation=tf.nn.relu,
                 encoding_activation=None,
                 output_activation=None, random_state=None,
                 learning_rate=1e-3, loss='mse', sigmoid_indices=None,
                 softmax_indices=None):
        super(AutoencoderManyEpochs, self).__init__(
            hidden_units=hidden_units, batch_size=batch_size,
            n_epochs=n_epochs, keep_prob=keep_prob,
            hidden_activation=hidden_activation,
            encoding_activation=encoding_activation,
            output_activation=output_activation, random_state=random_state,
            learning_rate=learning_rate, loss=loss,
            sigmoid_indices=sigmoid_indices, softmax_indices=softmax_indices,
        )


# The subset invariance part of check_estimator seems to fail due to
# small numerical differences (e.g., 1e-6). The test failure is difficult to
# replicate outside of travis, but I was able to get the test to fail locally
# by changing atol in sklearn.utils.check_methods_subset_invariance from 1e-7
# to 1e-10. This simply skips that part of check_estimator. 
Example #5
Source File: cblof.py    From pyod with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _validate_estimator(self, default=None):
        """Check the value of alpha and beta and clustering algorithm.
        """
        check_parameter(self.alpha, low=0, high=1, param_name='alpha',
                        include_left=False, include_right=False)

        check_parameter(self.beta, low=1, param_name='beta',
                        include_left=False)

        if self.clustering_estimator is not None:
            self.clustering_estimator_ = self.clustering_estimator
        else:
            self.clustering_estimator_ = default

        # make sure the base clustering algorithm is valid
        if self.clustering_estimator_ is None:
            raise ValueError("clustering algorithm cannot be None")

        if self.check_estimator:
            check_estimator(self.clustering_estimator_) 
Example #6
Source File: test_preprocessing.py    From xam with MIT License 5 votes vote down vote up
def test_check_estimator(self):
        assert check_estimator(xam.preprocessing.MDLPBinner) is None 
Example #7
Source File: test_estimator_checks.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_check_estimator_clones():
    # check that check_estimator doesn't modify the estimator it receives
    from sklearn.datasets import load_iris
    iris = load_iris()

    for Estimator in [GaussianMixture, LinearRegression,
                      RandomForestClassifier, NMF, SGDClassifier,
                      MiniBatchKMeans]:
        with ignore_warnings(category=FutureWarning):
            # when 'est = SGDClassifier()'
            est = Estimator()
        set_checking_parameters(est)
        set_random_state(est)
        # without fitting
        old_hash = joblib.hash(est)
        check_estimator(est)
        assert_equal(old_hash, joblib.hash(est))

        with ignore_warnings(category=FutureWarning):
            # when 'est = SGDClassifier()'
            est = Estimator()
        set_checking_parameters(est)
        set_random_state(est)
        # with fitting
        est.fit(iris.data + 10, iris.target)
        old_hash = joblib.hash(est)
        check_estimator(est)
        assert_equal(old_hash, joblib.hash(est)) 
Example #8
Source File: test_feature_extraction.py    From xam with MIT License 5 votes vote down vote up
def test_check_estimator(self):
        assert check_estimator(xam.feature_extraction.CycleTransformer) is None 
Example #9
Source File: test_mlp_classifier.py    From muffnn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def predict_proba(self, *args, **kwargs):
        res = super(MLPClassifierManyEpochs, self).predict_proba(
            *args, **kwargs)
        res = res.astype(np.float64)
        res /= np.sum(res, axis=1).reshape(-1, 1)
        return res


# The subset invariance part of check_estimator seems to fail due to
# small numerical differences (e.g., 1e-6). The test failure is difficult to
# replicate outside of travis, but I was able to get the test to fail locally
# by changing atol in sklearn.utils.check_methods_subset_invariance from 1e-7
# to 1e-10. This simply skips that part of check_estimator. 
Example #10
Source File: test_mlp_classifier.py    From muffnn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_check_estimator(mock_check_methods_subset_invariance):
    """Check adherence to Estimator API."""
    if sys.version_info.major == 3 and sys.version_info.minor == 7:
        # Starting in Tensorflow 1.14 and Python 3.7, there's one module
        # with a `0` in the __warningregistry__. Scikit-learn tries to clear
        # this dictionary in its tests.
        name = 'tensorboard.compat.tensorflow_stub.pywrap_tensorflow'
        with mock.patch.object(sys.modules[name], '__warningregistry__', {}):
            check_estimator(MLPClassifierManyEpochs)
    else:
        check_estimator(MLPClassifierManyEpochs) 
Example #11
Source File: test_mlp_regressor.py    From muffnn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_sample_weight():
    """Ensure we handle sample weights for regression problems."""
    assert_sample_weights_work(
        make_regression,
        {'n_samples': 3000},
        lambda: MLPRegressor(n_epochs=30, random_state=42,
                             keep_prob=0.8, hidden_units=(128,))
    )


# Make a subclass that has no `solver` parameter. The scikit-learn
# `check_estimator` has a check which fails with a class as a default. 
Example #12
Source File: test_mlp_regressor.py    From muffnn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_check_estimator():
    """Check adherence to Estimator API."""
    if sys.version_info.major == 3 and sys.version_info.minor == 7:
        # Starting in Tensorflow 1.14 and Python 3.7, there's one module
        # with a `0` in the __warningregistry__. Scikit-learn tries to clear
        # this dictionary in its tests.
        name = 'tensorboard.compat.tensorflow_stub.pywrap_tensorflow'
        with mock.patch.object(sys.modules[name], '__warningregistry__', {}):
            check_estimator(MLPRegressorFewerParams)
    else:
        check_estimator(MLPRegressorFewerParams) 
Example #13
Source File: common_tests.py    From kenchi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_check_estimator(self):
        self.assertIsNone(check_estimator(self.sut)) 
Example #14
Source File: test_common.py    From sagemaker-scikit-learn-extension with Apache License 2.0 5 votes vote down vote up
def test_all_estimators(Estimator):
    return check_estimator(Estimator) 
Example #15
Source File: test_common.py    From decision-tree-id3 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_estimator():
    return check_estimator(Id3Estimator) 
Example #16
Source File: test_common.py    From polylearn with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_check_estimator():
    # TODO: classifiers that provide predict_proba but are not multiclass fail
    # No trivial way to use OneVsRestClassifier even if it actually works.

    try:
        from sklearn.utils.estimator_checks import check_estimator
    except ImportError:
        raise SkipTest('Common scikit-learn tests not available. '
                       'You must be running an older version of scikit-learn.')
    yield check_estimator, PolynomialNetworkRegressor
    # FM Regressor fails because 5 iter is not enough :(
    # yield check_estimator, FactorizationMachineRegressor 
Example #17
Source File: test_pca.py    From prince with MIT License 5 votes vote down vote up
def test_check_estimator(self):
        estimator_checks.check_estimator(prince.PCA(as_array=True)) 
Example #18
Source File: test_common.py    From project-template with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_all_estimators(Estimator):
    return check_estimator(Estimator) 
Example #19
Source File: test_preprocessing.py    From xam with MIT License 5 votes vote down vote up
def test_check_estimator(self):
        assert check_estimator(xam.preprocessing.EqualWidthBinner) is None 
Example #20
Source File: test_preprocessing.py    From xam with MIT License 5 votes vote down vote up
def test_check_estimator(self):
        assert check_estimator(xam.preprocessing.EqualFrequencyBinner) is None 
Example #21
Source File: test_preprocessing.py    From xam with MIT License 5 votes vote down vote up
def test_check_estimator(self):
        assert check_estimator(xam.preprocessing.BayesianBlocksBinner) is None 
Example #22
Source File: test_estimator_checks.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_check_estimator_pairwise():
    # check that check_estimator() works on estimator with _pairwise
    # kernel or  metric

    # test precomputed kernel
    est = SVC(kernel='precomputed')
    check_estimator(est)

    # test precomputed metric
    est = KNeighborsRegressor(metric='precomputed')
    check_estimator(est) 
Example #23
Source File: test_estimator_checks.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_check_estimator_clones():
    # check that check_estimator doesn't modify the estimator it receives
    from sklearn.datasets import load_iris
    iris = load_iris()

    for Estimator in [GaussianMixture, LinearRegression,
                      RandomForestClassifier, NMF, SGDClassifier,
                      MiniBatchKMeans]:
        with ignore_warnings(category=(FutureWarning, DeprecationWarning)):
            # when 'est = SGDClassifier()'
            est = Estimator()
            set_checking_parameters(est)
            set_random_state(est)
            # without fitting
            old_hash = _joblib.hash(est)
            check_estimator(est)
        assert_equal(old_hash, _joblib.hash(est))

        with ignore_warnings(category=(FutureWarning, DeprecationWarning)):
            # when 'est = SGDClassifier()'
            est = Estimator()
            set_checking_parameters(est)
            set_random_state(est)
            # with fitting
            est.fit(iris.data + 10, iris.target)
            old_hash = _joblib.hash(est)
            check_estimator(est)
        assert_equal(old_hash, _joblib.hash(est)) 
Example #24
Source File: test_estimator_checks.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_check_estimator_transformer_no_mixin():
    # check that TransformerMixin is not required for transformer tests to run
    assert_raises_regex(AttributeError, '.*fit_transform.*',
                        check_estimator, BadTransformerWithoutMixin()) 
Example #25
Source File: test_lmdd.py    From pyod with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_sklearn_estimator(self):
        # check_estimator(self.clf)
        pass 
Example #26
Source File: cblof.py    From pyod with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, n_clusters=8, contamination=0.1,
                 clustering_estimator=None, alpha=0.9, beta=5,
                 use_weights=False, check_estimator=False, random_state=None,
                 n_jobs=1):
        super(CBLOF, self).__init__(contamination=contamination)
        self.n_clusters = n_clusters
        self.clustering_estimator = clustering_estimator
        self.alpha = alpha
        self.beta = beta
        self.use_weights = use_weights
        self.check_estimator = check_estimator
        self.random_state = random_state
        self.n_jobs = n_jobs

    # noinspection PyIncorrectDocstring 
Example #27
Source File: test_dummy.py    From mlens with MIT License 5 votes vote down vote up
def test_init_mixin():
    """[Utils] InitMixin: test mixin."""
    if check_estimator is not None:
        check_estimator(TestMixin) 
Example #28
Source File: test_dummy.py    From mlens with MIT License 5 votes vote down vote up
def test_scale_estimators():
    """[Utils] Scale: check estimators"""
    if check_estimator is not None:
        check_estimator(Scale) 
Example #29
Source File: test_dummy.py    From mlens with MIT License 5 votes vote down vote up
def test_logistic_regression_estimators():
    """[Utils] LogisticRegression: check estimators."""
    if check_estimator is not None:
        check_estimator(LogisticRegression) 
Example #30
Source File: test_dummy.py    From mlens with MIT License 5 votes vote down vote up
def test_ols_estimators():
    """[Utils] OLS: check estimators."""
    if check_estimator is not None:
        check_estimator(OLS)