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

有趣的边缘行为。在这个例子中,KNN exists会被打印出来,但Random Forest exists却不会。

在检查模型是否存在时发现了这个问题,当模型是随机森林时,if model: ...不会被触发。

from sklearn.ensemble import RandomForestClassifierfrom sklearn.neighbors import KNeighborsClassifierif KNeighborsClassifier(4):    print('KNN exists')if RandomForestClassifier(n_estimators=10, max_depth=4):    print('Random Forest exists')

为什么会这样呢?


回答:

啊哈!这是因为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数据模型的描述:

object.__bool__(self)

用于实现真值测试和内置操作bool();应该返回False或True。当此方法未定义时,如果定义了__len__(),则会调用它,如果其结果非零,则认为对象为真。如果一个类既没有定义__len__()也没有定义__bool__(),那么它的所有实例都被认为是真的。

正如预期的那样,RandomForestClassifierlen是估计器的数量,但只有在.fit之后才会这样:

In [9]: from sklearn.datasets import make_classification   ...: X, y = make_classification(n_samples=1000, n_features=4,   ...:             n_informative=2, n_redundant=0,   ...:             random_state=0, shuffle=False)   ...:In [10]: X.shapeOut[10]: (1000, 4)In [11]: y.shapeOut[11]: (1000,)In [12]: forest.fit(X,y)Out[12]: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 [13]: len(forest)Out[13]: 10

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注