Scikit learn 无法通过SKlearn的检查估计器
我不明白为什么我总是犯这样的错误?有人知道吗Scikit learn 无法通过SKlearn的检查估计器,scikit-learn,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_param
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]
回溯(最近一次呼叫最后一次):
文件“C:\Users\usethis.py”,第81行,在
校验估计器(ADABOST)
文件“C:\Users\AppData\Local\Programs\Python37-32\lib\site packages\sklearn\utils\estimator\u checks.py”,第302行,在check\u estimator中
支票(姓名、估计人)
文件“C:\AppData\Local\Programs\Python\Python37-32\lib\site packages\sklearn\utils\testing.py”,第355行,在包装器中
返回fn(*args,**kwargs)
文件“C:\Users\AppData\Local\Programs\Python37-32\lib\site packages\sklearn\utils\estimator\u checks.py”,check\u estimators\u fit\u returns\u self第1646行
拟合(X,y)是估计量
断言错误
[在1.7秒内完成,退出代码为1]
scikit学习的开发方式要求fit函数在最后自动返回。您可以通过添加return self
作为fit
函数的最后一行来完成此操作