Scikit随机森林分类器未评估为True

时间:2017-10-27 21:49:31

标签: python machine-learning scikit-learn

好奇的边缘行为。在此示例中,KNN exists被打印,但Random Forest exists没有。

在检查是否存在模型时发现它,当模型是随机森林时,未触发if model: ...

from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier

if KNeighborsClassifier(4):
    print('KNN exists')

if RandomForestClassifier(n_estimators=10, max_depth=4):
    print('Random Forest exists')

为什么会这样?

1 个答案:

答案 0 :(得分:5)

啊哈!这是因为Random实现了__len__

In [1]: from sklearn.ensemble import RandomForestClassifier
   ...: from sklearn.neighbors import KNeighborsClassifier
   ...:

In [2]: knn =  KNeighborsClassifier(4)

In [3]: forest = RandomForestClassifier(n_estimators=10, max_depth=4)

In [4]: knn.__bool__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-ef1cfe16be77> in <module>()
----> 1 knn.__bool__

AttributeError: 'KNeighborsClassifier' object has no attribute '__bool__'

In [5]: knn.__len__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-dc98bf8c50e0> in <module>()
----> 1 knn.__len__

AttributeError: 'KNeighborsClassifier' object has no attribute '__len__'

In [6]: forest.__bool__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-fbdd7f01e843> in <module>()
----> 1 forest.__bool__

AttributeError: 'RandomForestClassifier' object has no attribute '__bool__'

In [7]: forest.__len__
Out[7]:
<bound method BaseEnsemble.__len__ of RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=4, max_features='auto', max_leaf_nodes=None,
            min_impurity_split=1e-07, min_samples_leaf=1,
            min_samples_split=2, min_weight_fraction_leaf=0.0,
            n_estimators=10, n_jobs=1, oob_score=False, random_state=None,
            verbose=0, warm_start=False)>

In [8]: len(forest)
Out[8]: 0

而且,根据Python Data Model

  

object.__bool__(self)

     

被要求实施真值测试和内置操作   bool();应该返回False或True。如果未定义此方法,   __len__()被调用,如果已定义,则,如果对象的结果非零,则该对象被视为true。如果类未定义__len__()   并且__bool__(),其所有实例都被认为是真实的。

正如人们所预料的那样,len的{​​{1}}是估算器的数量,但只有之后才是RandomForestClassifier

.fit