简体   繁体   English

断言两个 sklearn 对象相等

[英]Assert two sklearn object are equal

My question is simple and divided in two sub-questions :我的问题很简单,分为两个子问题:

  • I have two LabelEncoder s with the exact same parameter .classes_ .我有两个LabelEncoder具有完全相同的参数.classes_ But when I compare then with == , they seems different.但是当我与==进行比较时,它们似乎不同。 How can I check they are equal ?我如何检查它们是否相等?

  • I also want to check two sklearn model ( RandomForest already fitted) are equals ?我还想检查两个 sklearn 模型(已经安装了RandomForest )是否相等? The == check does not seems to work. == 检查似乎不起作用。

If you compare two objects with ==, it will return False because they have different id.如果用 == 比较两个对象,它将返回 False,因为它们具有不同的 id。

You'd better override __eq__ function of class.你最好覆盖类的__eq__函数。

As mentioned in the comments, we need to create our own condition for __eq__ .正如评论中提到的,我们需要为__eq__创建我们自己的条件。 Here is my version:这是我的版本:

from sklearn.preprocessing import LabelEncoder
import numpy as np
target = pd.Series(np.random.choice(['yes', 'no'], (20,)))

class MyLabelEncoder(LabelEncoder):
    def __eq__(self, other):
        if np.array_equal(other.classes_,
                                   self.classes_):
            return True
        return False

le1 = MyLabelEncoder().fit(target)
le2 = MyLabelEncoder().fit(target)
le1 == le2
# True

For DecisionTree, there is already a solution provided here .对于DecisionTree,已经有提供了一个解决方案在这里 You could extend it for RandomForestClassifier .您可以为RandomForestClassifier扩展它。

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

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