【问题标题】:unable to pass check_estimator()无法通过 check_estimator()
【发布时间】:2020-08-21 08:59:18
【问题描述】:

我正在尝试自己编写 AdaBoostClassifier,并希望使其与 scikit-learn 兼容。但是,我的估算器无法通过check_estimator()。我检查了我的代码,分类器在我的数据集上运行良好。 p.s.我不是计算机科学专业的学生,​​所以我基本上使用简单的代码。

import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.utils.estimator_checks import check_estimator
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import confusion_matrix,classification_report
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils.multiclass import check_classification_targets
class AdaboostClassifier(BaseEstimator, ClassifierMixin):


当我运行check_estimator(AdaboostClassifier, generate_only = False)时,错误如下。

ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-13-67c9b1a2bb34> in <module>
----> 1 check_estimator(AdaboostClassifier)

~\Anaconda3\lib\site-packages\sklearn\utils\estimator_checks.py in check_estimator(Estimator, generate_only)
    425     for estimator, check in checks_generator:
    426         try:
--> 427             check(estimator)
    428         except SkipTest as exception:
    429             # the only SkipTest thrown currently results from not

~\Anaconda3\lib\site-packages\sklearn\utils\_testing.py in wrapper(*args, **kwargs)
    325             with warnings.catch_warnings():
    326                 warnings.simplefilter("ignore", self.category)
--> 327                 return fn(*args, **kwargs)
    328 
    329         return wrapper

~\Anaconda3\lib\site-packages\sklearn\utils\estimator_checks.py in check_estimators_empty_data_messages(name, estimator_orig)
   1355                        "to train. Perhaps use "
   1356                        "check_array in train.".format(name)):
-> 1357         e.fit(X_zero_samples, [])
   1358 
   1359     X_zero_features = np.empty(0).reshape(3, 0)

<ipython-input-12-eb321088a172> in fit(***failed resolving arguments***)
     26         self.mis_rate = []
     27         self.index_tree = []
---> 28         data_weights = [1/len(x)]*len(x)
     29 
     30         x, y = check_X_y(x, y)

ZeroDivisionError: division by zero

x应该被输入,所以当然,它的长度目前为零。 我不知道check_estimator() 有效。谁能告诉我?

欣赏它。

【问题讨论】:

    标签: python machine-learning scikit-learn adaboost


    【解决方案1】:

    check_estimator 运行一系列特定于估计器(具有 fit 方法的类)、转换器(具有 transform 方法的类)和分类器的测试,因为它们从父类 ClassifierMixin 继承而被识别。

    这里很难解释这些测试的作用,因为它们有很多。这些测试的文档并不详尽,所以我认为唯一的出路是浏览源代码,可以找到here

    简而言之,要通过 check_estimator 运行的许多测试,您正在开发的类不应在拟合期间引发错误。在测试中,会生成几个包含值 0 的 numpy 数组。因此,如果您的类在值为零时引发错误,例如除以 0,许多测试将失败,仅仅是因为它们可以不能下结论,它们在中途被打破了。

    目前我不确定是否有解决方法。如果需要,您可以使用名为 _more_tags() 的属性和指示哪些测试失败的字典跳过这些测试。例如这样的:

       def _more_tags(self):
            return {
                "_xfail_checks": {
                    # check_estimator checks that fail:
                    "check_estimators_nan_inf": "transformer allows NA",
                    "check_parameters_default_constructible":
                        "transformer has 1 mandatory parameter",
                    "check_transformer_preserve_dtypes":
                        "Test not relevant, transformers can change the types",
                    "check_methods_sample_order_invariance":
                        "Test does not work on dataframes",
                    "check_fit_idempotent": "Test does not work on dataframes",
                    "check_fit1d": "Test not relevant, transformers only "
                    "work with dataframes",
                    "check_fit2d_predict1d":
                        "Test not relevant, transformers only "
                    "work with dataframes",
                }
            }
    

    【讨论】:

      【解决方案2】:

      这只是一个建议,请验证它是否会破坏您的算法:

      lenX = len(x)
      if lenX == 0:
        lenX = 1
      data_weights = [1/lenX]*len(x)
      

      如果 len(x) == 0,则将 data_weights 设置为 0,因为计算结果为 (1/1)*0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        • 2021-09-24
        • 2016-10-08
        相关资源
        最近更新 更多