1

对于我想检索给定样本的预测分数/概率的所有类。我正在使用sklearn的RandomForestClassifier。我的代码运行良好,如果我使用.predict()。但是,要显示我使用的概率为.predict_proba(X),并且它始终返回相同的值,即使在X更改时也是如此。为什么是这样以及如何解决它?RandomForestClassifier(sklearn)的predict_proba(X)似乎是静态的?

我我的代码打破的有关部分:

# ... code ... feature generation/gets the feature data 
if rf is None: 
    rf = RandomForestClassifier(n_estimators=80) 
    rf.fit(featureData, classes) 
else: 
    prediction = rf.predict(featureData) # gets the right class/always different 
    proba = rf.predict_proba(featureData) 
    print proba # this prints always the same values for all my 40 classes 

有趣的是max(proba)检索类.predict()回报在第一个运行。由于.predict()正在按预期工作,我相信这个错误在sklearn的一边,也就是说我想有一个标志需要设置。

有没有人有想法?

回答

1

我想问题是你总是将相同的参数传递给predict_proba。这里是我的代码来构建的树木虹膜数据集森林:

from sklearn import datasets 
from sklearn.ensemble import RandomForestClassifier 
iris = datasets.load_iris() 
X = iris.data 
y = iris.target 
rf = RandomForestClassifier(n_estimators=80) 
rf.fit(X, y) 

当我调用的方法predictpredict_proba,阶级和阶级数概率预测为不同的参数也不同,作为一个可以合理期望。

采样运行:

In [82]: a, b = X[:3], X[-3:] 

In [83]: a 
Out[83]: 
array([[ 5.1, 3.5, 1.4, 0.2], 
     [ 4.9, 3. , 1.4, 0.2], 
     [ 4.7, 3.2, 1.3, 0.2]]) 

In [84]: b 
Out[84]: 
array([[ 6.5, 3. , 5.2, 2. ], 
     [ 6.2, 3.4, 5.4, 2.3], 
     [ 5.9, 3. , 5.1, 1.8]]) 

In [85]: rf.predict(a) 
Out[85]: array([0, 0, 0]) 

In [86]: rf.predict(b) 
Out[86]: array([2, 2, 2]) 

In [87]: rf.predict_proba(a) 
Out[87]: 
array([[ 1., 0., 0.], 
     [ 1., 0., 0.], 
     [ 1., 0., 0.]]) 

In [88]: rf.predict_proba(b) 
Out[88]: 
array([[ 0. , 0. , 1. ], 
     [ 0. , 0.0125, 0.9875], 
     [ 0. , 0.0375, 0.9625]]) 
+0

感谢您的时间有某种加载错误为'X'。不过,我仍然想知道为什么'预测()'成功了。谢谢你帮了我很多 – user3085931