scikit-learn - Sklearn 推出自己的估算器,检查估算器错误
问题描述
有人可以告诉我为什么我不断收到这些错误
AdaBoostClassifier 类(ClassifierMixin,BaseEstimator):
def __init__(self, base_estimator = None, n_estimators = 50, random_state = None):
self.base_estimator = base_estimator
self.n_estimators = n_estimators
self.random_state = random_state
def fit(self, X, y):
"""
----------
X : array-like, shape (n_samples, n_features)
The training input samples.
y : array-like, shape (n_samples,)
The target values. An array of int.
Returns
-------
self : object
Returns self.
"""
# Check that X and y have correct shape
X, y = check_X_y(X, y)
# Store the classes seen during fit
self.classes_ = unique_labels(y)
self.X_ = X
self.y_ = y
self.models = []
self.alphas = []
n_samples, _ = X.shape
w = np.ones(n_samples) / n_samples
for m in range(self.n_estimators):
clf = DecisionTreeClassifier(max_depth = 1)
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)
# Return the classifier
return self.models
def predict(self, X):
""" A reference implementation of a prediction for a classifier.
Parameters
----------
X : array-like, shape (n_samples, n_features)
The input samples.
Returns
-------
y : ndarray, shape (n_samples,)
The label for each sample is the label of the closest sample
seen during fit.
"""
# Check is fit had been called
check_is_fitted(self, ['X_', 'y_'])
# Input validation
X = check_array(X)
n_samples, _ = X.shape
self.ada = np.zeros(n_samples)
for alpha, clf in zip(self.alphas, self.models):
self.ada += alpha*clf.predict(X)
self.ada = np.sign(self.ada)
return self.ada
def score(self, X, y):
self.pred = self.predict(X)
self.accuracy = 100*sum(self.pred==y)/len(y)
return self.accuracy
check_estimator(AdaBoostClassifier)
回溯(最后一次调用):文件“C:\Users\Desktop\ada.py”,第 98 行,在 check_estimator(AdaBoostClassifier) 文件“C:\Users\AppData\Local\Programs\Python\Python37-32\lib \site-packages\sklearn\utils\estimator_checks.py”,第 302 行,在 check_estimator check(name, estimator) 文件“C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ sklearn\utils\testing.py”,第 355 行,在包装器中返回 fn(*args, **kwargs) 文件“C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn \utils\estimator_checks.py",第 1646 行,在 check_estimators_fit_returns_self 中断言 estimator.fit(X, y) 是估计器 AssertionError
解决方案
我相信你的fit
方法应该返回自我,而不是self.models
推荐阅读
- google-apps-script - 为什么函数 submitData 不将字段添加到指定的工作表?
- node.js - 在 Express 中 bodyparser 和 multer 如何协同工作?
- android - Jetpack WorkManager - 组织 WorkRequest
- android - 模拟器运行时总是崩溃
- javascript - Laravel 从数据库中获取经度和纬度并在谷歌地图上显示标记
- vue.js - 如何以 eslint 不会抛出“tinymce 未定义错误”的方式在 vue 中实现 tinymce?
- python - 如何获取此验证码以限制字符长度并仅允许数字
- c++ - 重用 auto 类型的变量
- javascript - Req.user 在 POST 和 GET 之间丢失
- postgresql - 容器之间的 Docker 连接被拒绝