我想对sklearn
GridSearchCV
使用自定义的回归器。其精神类似于创建自定义分类器-如本博客所示:
http://danielhnyk.cz/creating-your-own-estimator-scikit-learn/
这是博客中代码的压缩/编辑部分-显示了一些覆盖的必需方法。
from sklearn.base import BaseEstimator, RegressorMixin
class MyCustomClassifier(BaseEstimator, RegressorMixin):
"""An example of classifier"""
def __init__(self, intValue=0, stringParam="defaultValue", otherParam=None):
"""
Called when initializing the classifier
"""
self.intValue = intValue
self.stringParam = stringParam
def fit(self, X, y=None):
self.treshold_ = (sum(X)/len(X)) + self.intValue # mean + intValue
return self
def _meaning(self, x):
# returns True/False according to fitted classifier
# notice underscore on the beginning
return( True if x >= self.treshold_ else False )
def predict(self, X, y=None):
..
在回归器和分类器之间可能是相同的事情可能是__init__, fit, predict
方法:我可能会拼凑很多。但是等效于__meaning
的情况如何?还是对于Regressor
可能不明显/不清楚的其他细微注意事项?
这是我要开始的骨架:
from sklearn.base import BaseEstimator, RegressorMixin
class MyCustomRegressor(BaseEstimator, RegressorMixin):
def __init__(self, regressorMethod):
self.regressorMethod = regressorMethod
def fit(self, X, y=None):
self.regressorMethod(X,y)
return self
据我了解,score
方法将由GridSearchCv使用..?