【问题标题】:Sklearn RandomForest: predict_proba: wrong shape and how to get rid of compliment probabilitySklearn RandomForest:predict_proba:错误的形状以及如何摆脱恭维概率
【发布时间】:2018-01-24 16:43:59
【问题描述】:

在标记为 9 个类的 6000 个分量向量上训练 RForest 后,我​​尝试获取以前未见过的一组向量的类概率,其形状如下:

X.shape
Out[6]: (15091, 6000)

与:

clf = RandomForestClassifier( n_estimators = 100 )    
probs = clf.predict_proba(X)

结果我得到了一个列表:

[array([[ 0.61,  0.39],
        [ 0.62,  0.38],
        [ 0.24,  0.76],
        ..., 
        [ 0.96,  0.04],
        [ 0.49,  0.51],
        [ 0.91,  0.09]]), array([[ 0.91,  0.09],
        [ 0.94,  0.06],
        [ 0.93,  0.07],
        ..., 
        [ 1.  ,  0.  ],
        [ 0.96,  0.04],
        [ 0.99,  0.01]]), array([[ 0.95,  0.05],
        [ 0.9 ,  0.1 ],
        [ 0.95,  0.05],
        ..., 

带有恭维概率。有什么办法可以消除predict_proba 中的恭维概率,所以这个方法的输出而不是[ 0.96, 0.04] 只包含0.96 而无需自己编码?

* 主要更新 *

将 RForest 返回的概率列表转换为 numpy 数组后:

predictions = np.array(probs)

看它的形状:

predictions.shape
(9, 15091, 2)

揭示了主要问题:我有 9 个类和 15091 个样本,所以predict_proba 应该返回 15091 个列表,其中每个列表依次包含 9 个两个概率元素的列表(赞美)。相反,我得到 9 个列表,每个列表长 15091 个元素,其中每个元素都是一个恭维概率列表。

简而言之,为什么而不是:

(15091, 9, 2)

我明白了:

(9, 15091, 2)

怎么了?

【问题讨论】:

    标签: python scikit-learn random-forest


    【解决方案1】:

    据我所知,你不能开箱即用,但为什么不扩展类呢?

    from sklearn.ensemble import RandomForestClassifier
    import numpy as np
    
    class RandomForestClassifier2(RandomForestClassifier):
        def predict_proba(self,X,drop_compliment=False):
            result = super().predict_proba(X)
            if drop_compliment:
                result = np.array([p for p,_ in result])
            return result
    

    示例用法:

    # Generate some dummy data
    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)
    clf = RandomForestClassifier2(max_depth=2, random_state=0)
    clf.fit(X, y)
    

    您可以通过以下方式获得“默认”RandomForestClassifier 结果:

    clf.predict_proba(X,drop_compliment=False)
    
    Out[13]:
    array([[ 0.88724838,  0.11275162],
           [ 0.73563529,  0.26436471],
           [ 0.88724838,  0.11275162],
           ..., 
           [ 0.16937022,  0.83062978],
           [ 0.14297294,  0.85702706],
           [ 0.14297294,  0.85702706]])
    

    或者你想要的输出:

    clf.predict_proba(X,drop_compliment=True)
    
    Out[14]:
    array([ 0.88724838,  0.73563529,  0.88724838, ...
            0.16937022,  0.14297294,  0.14297294])
    

    【讨论】:

      【解决方案2】:

      一个简单的切片就可以了

      probs = clf.predict_proba(X)[:, 0] #0 in your case since you need the first one

      【讨论】:

      • 我有 15091 个样本。为什么我只需要第一个?
      • 在更新之前,您需要第一列预测。所以 slice 会给你第一列预测。
      • 15k 是你的行。我是说只取第一列。
      猜你喜欢
      • 2022-08-18
      • 2021-08-26
      • 1970-01-01
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2011-08-07
      • 1970-01-01
      相关资源
      最近更新 更多