如何实现 sklearn 的 Estimator 接口以用于 GridSearchCV 管道?
How to implement sklearn's Estimator interface for use in GridSearchCV pipeline?
所以我有自己的感知器分类器实现,并且想使用 sklearn 的 GridSearchCV 调整它的超参数。我一直在尝试围绕实现 Estimator 的模型编写一个包装器(通读 https://scikit-learn.org/stable/developers/develop.html)但是当我 运行
GridSearchCV(wrapper, params).fit(X,y)
,我收到以下错误:
FitFailedWarning: Estimator fit failed. The score on this train-test partition for these parameters will be set to nan. Details:
AttributeError: 'NoneType' object has no attribute 'fit'
FitFailedWarning)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 738, in fit
**self.best_params_))
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py", line 67, in clone
% (repr(estimator), type(estimator)))
TypeError: Cannot clone object 'None' (type <class 'NoneType'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.
此错误与 How to write a custom estimator in sklearn and use cross-validation on it? 相同,但我已经执行了最高评价评论中建议的所有操作。
我确信模型是正确的。这是模型包装器的代码:
from models import Perceptron, Softmax, SVM
from sklearn.model_selection import GridSearchCV
class Estimator():
def __init__(self, alpha=0.5, epochs=100):
self.alpha = alpha
self.epochs = epochs
self.model = Perceptron()
def fit(self, X, y, **kwargs):
self.alpha = kwargs['alpha']
self.epochs = kwargs['epochs']
self.model.alpha = kwargs['alpha']
self.model.epochs = kwargs['epochs']
self.model.train(X, y)
def predict(self, X):
return self.model.predict(X)
def score(self, data, targets):
return self.model.get_acc(self.predict(data), targets)
def set_params(self, alpha, epochs):
self.alpha = alpha
self.epochs = epochs
self.model.alpha = alpha
self.model.epochs = epochs
def get_params(self, deep=False):
return {'alpha':self.alpha, 'epochs':self.epochs}
如 in this section of the documentation 所述,您应该从 BaseEstimator class Estimator(BaseEstimator):
派生 class 以避免样板代码并遵循拟合预测结构。正如@Shihab 在评论中所说,您的 fit 函数缺少 return self
代码行。
另外,在get_params()中不知道你是不是故意的,但是文档里面的参数deep也建议默认设置为deep=True
.请检查一下。
set_params
和 fit
方法应该 return self
.
所以我有自己的感知器分类器实现,并且想使用 sklearn 的 GridSearchCV 调整它的超参数。我一直在尝试围绕实现 Estimator 的模型编写一个包装器(通读 https://scikit-learn.org/stable/developers/develop.html)但是当我 运行
GridSearchCV(wrapper, params).fit(X,y)
,我收到以下错误:
FitFailedWarning: Estimator fit failed. The score on this train-test partition for these parameters will be set to nan. Details:
AttributeError: 'NoneType' object has no attribute 'fit'
FitFailedWarning)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 738, in fit
**self.best_params_))
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py", line 67, in clone
% (repr(estimator), type(estimator)))
TypeError: Cannot clone object 'None' (type <class 'NoneType'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.
此错误与 How to write a custom estimator in sklearn and use cross-validation on it? 相同,但我已经执行了最高评价评论中建议的所有操作。
我确信模型是正确的。这是模型包装器的代码:
from models import Perceptron, Softmax, SVM
from sklearn.model_selection import GridSearchCV
class Estimator():
def __init__(self, alpha=0.5, epochs=100):
self.alpha = alpha
self.epochs = epochs
self.model = Perceptron()
def fit(self, X, y, **kwargs):
self.alpha = kwargs['alpha']
self.epochs = kwargs['epochs']
self.model.alpha = kwargs['alpha']
self.model.epochs = kwargs['epochs']
self.model.train(X, y)
def predict(self, X):
return self.model.predict(X)
def score(self, data, targets):
return self.model.get_acc(self.predict(data), targets)
def set_params(self, alpha, epochs):
self.alpha = alpha
self.epochs = epochs
self.model.alpha = alpha
self.model.epochs = epochs
def get_params(self, deep=False):
return {'alpha':self.alpha, 'epochs':self.epochs}
如 in this section of the documentation 所述,您应该从 BaseEstimator class Estimator(BaseEstimator):
派生 class 以避免样板代码并遵循拟合预测结构。正如@Shihab 在评论中所说,您的 fit 函数缺少 return self
代码行。
另外,在get_params()中不知道你是不是故意的,但是文档里面的参数deep也建议默认设置为deep=True
.请检查一下。
set_params
和 fit
方法应该 return self
.