简体   繁体   English

继承具有“最大递归深度超出”错误的LabelBinarizer类

[英]Inherit Class of LabelBinarizer with “maximum recursion depth exceede” error

I created a customized encoder class from LabelBinarizer. 我从LabelBinarizer创建了一个自定义的编码器类。 Here's what it looks like 这是它的样子

class my_lb(LabelBinarizer):

  def fit(self, X, y=None):
    self.fit(X)

  def transform(self, X, y=None):
    return self.transform(X)

  def fit_transform(self, X, y=None):
    return self.fit(X).transform(X)

And I have the "maximum recursion depth exceeded" error, which occurred at the fit method. 而且我遇到了“超出最大递归深度”错误,该错误发生在拟合方法中。 I was able to make it right following some notes online: 我能够按照网上的一些说明进行操作:

class my_lb(LabelBinarizer):

  def __init__(self):
    super().__init__()

  def fit(self, X, y=None):
    super().fit(X)

  def transform(self, X, y=None):
    return super().transform(X)

  def fit_transform(self, X, y=None):
    return super().fit(X).transform(X)

But my question is, how does it solve my problem? 但是我的问题是,它如何解决我的问题? I can understand the cause in other posts here (which I can see are constructing an explicit infinite loop), but I read the codes of LabelBinarizer, and it looks pretty normal to me. 我可以在这里的其他帖子中了解原因(可以看到它们正在构造一个显式的无限循环),但是我阅读了LabelBinarizer的代码,对我来说这看起来很正常。 I can't find anything that may cause an infinite loop. 我找不到任何可能导致无限循环的东西。

def fit(self, y): 
        self.y_type_ = type_of_target(y)
        if 'multioutput' in self.y_type_:
             raise ValueError("Multioutput target data is not supported with "
                              "label binarization")
        if _num_samples(y) == 0:
            raise ValueError('y has 0 samples: %r' % y)
        self.sparse_input_ = sp.issparse(y)
        self.classes_ = unique_labels(y)
        return self

Can anyone tell me what am I missing here? 谁能告诉我我在这里想念什么吗? It will also help me know better when super is needed when I create an inheriting class. 创建继承类时,它还可以帮助我更好地了解何时需要使用super。

Before it was changed to call super() , calling any one of the methods shown would have caused infinite recursion. 在更改为调用super() ,调用显示的任何一种方法都将导致无限递归。 fit and transform are obvious, they just called themselves so if they were ever called, they would never return and you would exceed the maximum recursion depth. fittransform很明显,它们只是自称,因此,一旦被调用,它们将永远不会返回,并且您将超过最大递归深度。

fit_transform , on the other hand, was calling self.fit(X).transform(X) . fit_transform ,在另一方面,被调用self.fit(X).transform(X) So the first thing it did was call self.fit(X) , which just called itself over and over until the error occurred. 因此,它所做的第一件事是调用self.fit(X) ,它只是一遍又一遍地调用自己,直到发生错误为止。

The modified versions, on the other hand, pass the calls along to the parent class versions of fit and transform , via the call to super() , so they don't call themselves. 另一方面,修改后的版本会将调用通过对super()的调用传递给fittransform的父类版本,因此它们不会自行调用。 In fact, with those calls in place, you don't even need to call super() from fit_transform . 实际上,有了这些调用,您甚至不需要从fit_transform调用super()

But the best fix is to simply delete the definitions of fit and transform in the derived class, and remove the call to super() from fit_transform , which isn't needed. 但是最好的解决方法是简单地删除派生类中fittransform的定义,并从fit_transform删除对super()fit_transform ,这是不需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM