def test_no_attributes_set_in_init(name, Estimator): # input validation etc for non-meta estimators with ignore_warnings(category=(DeprecationWarning, ConvergenceWarning, UserWarning, FutureWarning)): estimator = Estimator() # check this on class check_no_attributes_set_in_init(name, estimator)
def test_no_attributes_set_init_sublcassed(): """Tests that subclassed models can be made that set all parameters in a single __init__ """ estimator = SubclassedClassifier() check_no_attributes_set_in_init(estimator.__name__, estimator) basic_checks(estimator, load_iris)
def custom_check_estimator(Estimator): # Same as sklearn.check_estimator, skipping tests that can't succeed. if isinstance(Estimator, type): # got a class name = Estimator.__name__ estimator = Estimator() check_parameters_default_constructible(name, Estimator) check_no_attributes_set_in_init(name, estimator) else: # got an instance estimator = Estimator name = type(estimator).__name__ for check in _yield_all_checks(name, estimator): if (check is estimator_checks.check_estimators_dtypes or check is estimator_checks.check_fit_score_takes_y or check is estimator_checks.check_dtype_object or check is estimator_checks.check_sample_weights_list or check is estimator_checks.check_estimators_overwrite_params or check is estimator_checks.check_classifiers_classes or check is estimator_checks.check_supervised_y_2d or check is estimator_checks.check_fit2d_predict1d or check is estimator_checks.check_class_weight_classifiers or check is estimator_checks.check_methods_subset_invariance or check is estimator_checks.check_dont_overwrite_parameters or "check_estimators_fit_returns_self" in check.__repr__() or "check_classifiers_train" in check.__repr__()): continue try: check(name, estimator) except SkipTest as exception: # the only SkipTest thrown currently results from not # being able to import pandas. warnings.warn(str(exception), SkipTestWarning)
def test_check_no_attributes_set_in_init(): class NonConformantEstimatorPrivateSet(BaseEstimator): def __init__(self): self.you_should_not_set_this_ = None class NonConformantEstimatorNoParamSet(BaseEstimator): def __init__(self, you_should_set_this_=None): pass msg = ( "Estimator estimator_name should not set any" " attribute apart from parameters during init." r" Found attributes \['you_should_not_set_this_'\]." ) with raises(AssertionError, match=msg): check_no_attributes_set_in_init( "estimator_name", NonConformantEstimatorPrivateSet() ) msg = ( "Estimator estimator_name should store all parameters as an attribute" " during init" ) with raises(AttributeError, match=msg): check_no_attributes_set_in_init( "estimator_name", NonConformantEstimatorNoParamSet() )
def test_no_attributes_set_in_init(name, Estimator): # input validation etc for non-meta estimators with ignore_warnings(category=(DeprecationWarning, ConvergenceWarning, UserWarning, FutureWarning)): estimator = Estimator() # check this on class check_no_attributes_set_in_init(name, estimator)
def check_no_attributes_set_in_init(estimator): from sklearn.utils.estimator_checks import check_no_attributes_set_in_init check_no_attributes_set_in_init("test", estimator) return True
def test_no_attributes_set_in_init(name, estimator): # input validation etc for all estimators with ignore_warnings(category=(DeprecationWarning, ConvergenceWarning, UserWarning, FutureWarning)): tags = _safe_tags(estimator) if tags['_skip_test']: warnings.warn( "Explicit SKIP via _skip_test tag for " "{}.".format(name), SkipTestWarning) return # check this on class check_no_attributes_set_in_init(name, estimator)
def check_estimator_autofeat(Estimator): # usually, this would be # from sklearn.utils.estimator_checks import check_estimator # but first this issue needs to be resolved: # https://github.com/pandas-dev/pandas/issues/26247 # check class name = Estimator.__name__ estimator = Estimator() check_parameters_default_constructible(name, Estimator) check_no_attributes_set_in_init(name, estimator) # check with fewer feateng steps and featsel runs to speed things up check_estimator( Estimator(feateng_steps=1, featsel_runs=1, featsel_max_it=20))
def test_no_attributes_set_init_no_args(): """Tests that models with no build arguments set all parameters in a single __init__ """ def build_fn(): model = Sequential() model.add(layers.Dense(1, input_dim=1, activation="relu")) model.add(layers.Dense(1)) model.compile(loss="mse") return model estimator = KerasRegressor(model=build_fn) check_no_attributes_set_in_init(estimator.__name__, estimator) estimator.fit([[1]], [1])
def check_estimator(Estimator): """Check if estimator adheres to scikit-learn conventions. This estimator will run an extensive test-suite for input validation, shapes, etc. Additional tests for classifiers, regressors, clustering or transformers will be run if the Estimator class inherits from the corresponding mixin from sklearn.base. This test can be applied to classes or instances. Classes currently have some additional tests that related to construction, while passing instances allows the testing of multiple options. Parameters ---------- estimator : estimator object or class Estimator to check. Estimator is a class object or instance. """ if isinstance(Estimator, type): # got a class name = Estimator.__name__ estimator = Estimator() check_parameters_default_constructible(name, Estimator) check_no_attributes_set_in_init(name, estimator) else: # got an instance estimator = Estimator name = type(estimator).__name__ if hasattr(estimator, 'max_iter'): if (isinstance(estimator, ShapeletModel) or isinstance(estimator, SerializableShapeletModel)): estimator.set_params(max_iter=100) else: estimator.set_params(max_iter=10) if hasattr(estimator, 'total_lengths'): estimator.set_params(total_lengths=1) if hasattr(estimator, 'probability'): estimator.set_params(probability=True) for check in checks._yield_all_checks(name, estimator): try: check(name, estimator) except SkipTest as exception: # the only SkipTest thrown currently results from not # being able to import pandas. warnings.warn(str(exception), SkipTestWarning)
def rewritten_check_estimator(Estimator): # Same as sklearn.check_estimator, re-writing tests that can't succeed. if isinstance(Estimator, type): # got a class name = Estimator.__name__ estimator = Estimator() check_parameters_default_constructible(name, Estimator) check_no_attributes_set_in_init(name, estimator) else: # got an instance estimator = Estimator name = type(estimator).__name__ for check in _yield_rewritten_checks(name, estimator): try: check(name, estimator) except SkipTest as exception: # the only SkipTest thrown currently results from not # being able to import pandas. warnings.warn(str(exception), SkipTestWarning)
def check_estimator(Estimator): """Check if estimator adheres to scikit-learn conventions. This estimator will run an extensive test-suite for input validation, shapes, etc. Additional tests for classifiers, regressors, clustering or transformers will be run if the Estimator class inherits from the corresponding mixin from sklearn.base. This test can be applied to classes or instances. Classes currently have some additional tests that related to construction, while passing instances allows the testing of multiple options. Parameters ---------- estimator : estimator object or class Estimator to check. Estimator is a class object or instance. """ warnings.filterwarnings(action='ignore', category=DataConversionWarning) if isinstance(Estimator, type): # got a class name = Estimator.__name__ estimator = Estimator() check_parameters_default_constructible(name, Estimator) check_no_attributes_set_in_init(name, estimator) else: # got an instance estimator = Estimator name = type(estimator).__name__ for check in _yield_all_checks(name, estimator): if hasattr(check, "__name__"): if check.__name__ == "check_estimators_nan_inf": # we allow NaNs in transform... continue print("##", check.__name__) try: check(name, estimator) except SkipTest as exception: # the only SkipTest thrown currently results from not # being able to import pandas. warnings.warn(str(exception), SkipTestWarning)
def test_check_estimators_voting_estimator(estimator): # FIXME: to be removed when meta-estimators can be specified themselves # their testing parameters (for required parameters). check_estimator(estimator) check_no_attributes_set_in_init(estimator.__class__.__name__, estimator)
def test_no_attributes_set_in_init(name, Estimator): # input validation etc for non-meta estimators estimator = Estimator() # check this on class check_no_attributes_set_in_init(name, estimator)
def test_check_estimators_stacking_estimator(estimator): check_estimator(estimator) check_no_attributes_set_in_init(estimator.__class__.__name__, estimator)
def test_no_attributes_set_in_init(name, Estimator): # input validation etc for non-meta estimators estimator = Estimator() # check this on class check_no_attributes_set_in_init(name, estimator)