在sklearn中创建自定义回归器

2024-09-25 00:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用带有sklearnGridSearchCV的自定义回归函数。它的精神类似于创建一个自定义分类器-如本博客所示:

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的等价物呢?或者其他不明显/不清晰的微妙考虑?在

下面是我要开始的一个骨架:

^{pr2}$

据我所知,GridSearchCv将使用score方法。。?在


Tags: the方法selfnonereturn分类器initdef