无法通过SKlearn的检查估算器

时间:2020-05-14 01:59:23

标签: scikit-learn

我不明白为什么我会不断收到此错误?有人知道吗?

class AdaBoost(BaseEstimator, ClassifierMixin):

    def __init__(self, M=1, tree_depth=1, random_state=None):
        self.M = M
        self.tree_depth = tree_depth 
        self.random_state = random_state

    def get_params(self, deep=True):
        return {"tree_depth": self.tree_depth, "M": self.M, "random_state": self.random_state}

    def set_params(self, **parameters):
        for parameter, value in parameters.items():
            setattr(self, parameter, value)
        return self

    def fit(self, X, y):
        self.classes_, y = np.unique(y, return_inverse=True)
        self.X_ = X
        self.y_ = y
        X, y = check_X_y(X, y)

        self.models = []
        self.alphas = []
        n_samples, _ = X.shape
        w = np.ones(n_samples) / n_samples

        for m in range(self.M):
            clf = DecisionTreeClassifier(max_depth = self.tree_depth)
            clf.fit(X,y, sample_weight = w)
            pred = clf.predict(X)

            error = w.dot(pred != y)
            alpha = 0.5*(np.log(1-error)-np.log(error))

            w = w*np.exp(-alpha*y*pred)
            w = w/w.sum() # normalise to sum to 1

            self.models.append(clf)
            self.alphas.append(alpha)

    def predict(self, X):
        check_is_fitted(self, ['X_', 'y_', 'classes_'])
        n_samples, _ = X.shape
        ada = np.zeros(n_samples)
        for alpha, clf in zip(self.alphas, self.models):
            ada += alpha*clf.predict(X)
        return np.sign(ada)

    def score(self, X, y):
        pred = self.predict(X)
        accuracy = 100*sum(pred==y)/len(y)
        return accuracy

错误:

Traceback (most recent call last):
  File "C:\Users\usethis.py", line 81, in <module>
    check_estimator(AdaBoost)
  File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\estimator_checks.py", line 302, in check_estimator
    check(name, estimator)
  File "C:\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\testing.py", line 355, in wrapper
    return fn(*args, **kwargs)
  File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\estimator_checks.py", line 1646, in check_estimators_fit_returns_self
    assert estimator.fit(X, y) is estimator
AssertionError
[Finished in 1.7s with exit code 1]

1 个答案:

答案 0 :(得分:1)

开发scikit学习的方式要求fit函数最后返回自身。您可以通过在return self函数的最后一行添加fit来完成此操作。

相关问题