Ryantstrong
Ryantstrong

Reputation: 1

Cant pass the check estimator for SKlearn

I cant understand why i keep getting this errors? Does anybody know?

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

Error:

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]

Upvotes: 0

Views: 440

Answers (1)

robertlayton
robertlayton

Reputation: 722

The way scikit-learn is developed requires that fit functions return the object itself after fitting. You can do this by adding return self as the last line in the fit function.


class AdaBoost(BaseEstimator, ClassifierMixin):

    ...

    def fit(self, X, y):
        ...
        return self

Upvotes: 1

Related Questions