def test_parameters_default_constructible(estimator): name = estimator.__class__.__name__ if sk_version >= parse_version("0.24"): Estimator = estimator else: Estimator = estimator.__class__ sklearn_checks.check_parameters_default_constructible(name, Estimator)
def check_estimator(Estimator, run_sampler_tests=True): """Check if estimator adheres to scikit-learn conventions and imbalanced-learn This estimator will run an extensive test-suite for input validation, shapes, etc. Additional tests samplers if the Estimator inherits from the corresponding mixin from imblearn.base Parameters ---------- Estimator : class Class to check. Estimator is a class object (not an instance) run_sampler_tests=True : bool, default=True Will run or not the samplers tests. """ name = Estimator.__name__ # monkey patch check_dtype_object for the sampler allowing strings import sklearn.utils.estimator_checks sklearn.utils.estimator_checks.check_dtype_object = \ monkey_patch_check_dtype_object # scikit-learn common tests sklearn_check_estimator(Estimator) check_parameters_default_constructible(name, Estimator) if run_sampler_tests: for check in _yield_all_checks(name, Estimator): check(name, Estimator)
def test_sklearn_api(): name = DataFrameETL.__name__ check_parameters_default_constructible(name, DataFrameETL) check_no_fit_attributes_set_in_init(name, DataFrameETL) estimator = DataFrameETL() for check in [ check_transformers_unfitted, check_transformer_n_iter, check_get_params_invariance]: check(name, estimator) # these are known failures for check in [ check_transformer_data_not_an_array, check_transformer_general, check_fit2d_predict1d, check_fit2d_1sample, check_fit2d_1feature, check_fit1d_1feature, check_fit1d_1sample, check_dict_unchanged, check_dont_overwrite_parameters]: with pytest.raises(TypeError) as e: check(name, estimator) assert ( "ETL transformer must be fit " "to a dataframe.") in str(e.value)
def check_estimator(Estimator, run_sampler_tests=True): """Check if estimator adheres to scikit-learn conventions and imbalanced-learn This estimator will run an extensive test-suite for input validation, shapes, etc. Additional tests samplers if the Estimator inherits from the corresponding mixin from imblearn.base Parameters ---------- Estimator : class Class to check. Estimator is a class object (not an instance) run_sampler_tests=True : bool, default=True Will run or not the samplers tests. """ name = Estimator.__name__ # monkey patch check_dtype_object for the sampler allowing strings import sklearn.utils.estimator_checks sklearn.utils.estimator_checks.check_dtype_object = \ monkey_patch_check_dtype_object # scikit-learn common tests sklearn_check_estimator(Estimator) check_parameters_default_constructible(name, Estimator) if run_sampler_tests: for check in _yield_all_checks(name, Estimator): check(name, Estimator)
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_class_support_deprecated(): # Make sure passing classes to check_estimator or parametrize_with_checks # is deprecated msg = "Passing a class is deprecated" with pytest.warns(FutureWarning, match=msg): check_estimator(LogisticRegression) with pytest.warns(FutureWarning, match=msg): parametrize_with_checks([LogisticRegression]) # Make sure check_parameters_default_constructible accepts instances now check_parameters_default_constructible('name', LogisticRegression())
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_sklearn_integration(self): # we cannot use `check_estimator` directly since there is no skip test mechanism for name, estimator in ((lgb.sklearn.LGBMClassifier.__name__, lgb.sklearn.LGBMClassifier), (lgb.sklearn.LGBMRegressor.__name__, lgb.sklearn.LGBMRegressor)): check_parameters_default_constructible(name, estimator) # we cannot leave default params (see https://github.com/microsoft/LightGBM/issues/833) estimator = estimator(min_child_samples=1, min_data_in_bin=1) for check in _yield_all_checks(name, estimator): check_name = check.func.__name__ if hasattr(check, 'func') else check.__name__ if check_name == 'check_estimators_nan_inf': continue # skip test because LightGBM deals with nan try: check(name, estimator) except SkipTest as message: warnings.warn(message, SkipTestWarning)
def test_sklearn_integration(self): # we cannot use `check_estimator` directly since there is no skip test mechanism for name, estimator in ((lgb.sklearn.LGBMClassifier.__name__, lgb.sklearn.LGBMClassifier), (lgb.sklearn.LGBMRegressor.__name__, lgb.sklearn.LGBMRegressor)): check_parameters_default_constructible(name, estimator) check_no_fit_attributes_set_in_init(name, estimator) # we cannot leave default params (see https://github.com/Microsoft/LightGBM/issues/833) estimator = estimator(min_child_samples=1, min_data_in_bin=1) for check in _yield_all_checks(name, estimator): if check.__name__ == 'check_estimators_nan_inf': continue # skip test because LightGBM deals with nan try: check(name, estimator) except SkipTest as message: warnings.warn(message, SkipTestWarning)
def test_sklearn_integration(self): # sklearn <0.19 cannot accept instance, but many tests could be passed only with min_data=1 and min_data_in_bin=1 if sklearn_at_least_019: # we cannot use `check_estimator` directly since there is no skip test mechanism for name, estimator in ((lgb.sklearn.LGBMClassifier.__name__, lgb.sklearn.LGBMClassifier), (lgb.sklearn.LGBMRegressor.__name__, lgb.sklearn.LGBMRegressor)): check_parameters_default_constructible(name, estimator) check_no_fit_attributes_set_in_init(name, estimator) # we cannot leave default params (see https://github.com/Microsoft/LightGBM/issues/833) estimator = estimator(min_child_samples=1, min_data_in_bin=1) for check in _yield_all_checks(name, estimator): if check.__name__ == 'check_estimators_nan_inf': continue # skip test because LightGBM deals with nan try: check(name, estimator) except SkipTest as message: warnings.warn(message, 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. """ 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 check_estimator(Estimator): """Check if estimator adheres to scikit-learn conventions and imbalanced-learn This estimator will run an extensive test-suite for input validation, shapes, etc. Additional tests samplers if the Estimator inherits from the corresponding mixin from imblearn.base Parameters ---------- Estimator : class Class to check. Estimator is a class object (not an instance). """ name = Estimator.__name__ # test scikit-learn compatibility sklearn_check_estimator(Estimator) check_parameters_default_constructible(name, Estimator) for check in _yield_all_checks(name, Estimator): check(name, Estimator)
def check_estimator(Estimator): """Check if estimator adheres to scikit-learn conventions and imbalanced-learn This estimator will run an extensive test-suite for input validation, shapes, etc. Additional tests samplers if the Estimator inherits from the corresponding mixin from imblearn.base Parameters ---------- Estimator : class Class to check. Estimator is a class object (not an instance). """ name = Estimator.__name__ # test scikit-learn compatibility sklearn_check_estimator(Estimator) check_parameters_default_constructible(name, Estimator) for check in _yield_all_checks(name, Estimator): check(name, Estimator)
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_parameters_default_constructible(name, Estimator): # Test that estimators are default-constructible check_parameters_default_constructible(name, Estimator)
def test_parameters_default_constructible(name, Estimator): # Test that estimators are default-constructible check_parameters_default_constructible(name, Estimator)
def test_parameters_default_constructible(): checks.check_parameters_default_constructible(NAME, CLF)
def test_parameters_default_constructible(estimator): name, Estimator = estimator.__class__.__name__, estimator.__class__ sklearn_checks.check_parameters_default_constructible(name, Estimator)