scikit学习随机森林分类器概率阈值

问题描述 投票:0回答:1

我用的是 sklearn RandomForestClassifier(随机森林分类器) 的预测任务。

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(n_estimators=300, n_jobs=-1)
model.fit(x_train,y_train)
model.predict_proba(x_test)

有171个班级需要预测,我只想预测那些班级,其中的 predict_proba(class) 是至少90%。下面的一切都应该设置为 0.

例如,给定以下内容。

     1   2   3   4   5   6   7
0  0.0 0.0 0.1 0.9 0.0 0.0 0.0
1  0.2 0.1 0.1 0.3 0.1 0.0 0.2
2  0.1 0.1 0.1 0.1 0.1 0.4 0.1
3  1.0 0.0 0.0 0.0 0.0 0.0 0.0

我的预期输出是:

0   4
1   0
2   0   
3   1
machine-learning scikit-learn random-forest
1个回答
1
投票

你可以使用 numpy.argwhere 如下。

from sklearn.ensemble import RandomForestClassifier
import numpy as np

model = RandomForestClassifier(n_estimators=300, n_jobs=-1)
model.fit(x_train,y_train)
preds = model.predict_proba(x_test)

#preds = np.array([[0.0, 0.0, 0.1, 0.9, 0.0, 0.0, 0.0],
#                  [ 0.2, 0.1, 0.1, 0.3, 0.1, 0.0, 0.2],
#                  [ 0.1 ,0.1, 0.1, 0.1, 0.1, 0.4, 0.1],
#                  [ 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]])

r = np.zeros(preds.shape[0], dtype=int)
t = np.argwhere(preds>=0.9)

r[t[:,0]] = t[:,1]+1
r
array([4, 0, 0, 1])

1
投票

你可以使用列表理解。

import numpy as np

# dummy predictions - 3 samples, 3 classes
pred = np.array([[0.1, 0.2, 0.7],
                 [0.95, 0.02, 0.03],
                 [0.08, 0.02, 0.9]])

# first, keep only entries >= 0.9:
out_temp = np.array([[x[i] if x[i] >= 0.9 else 0 for i in range(len(x))] for x in pred])
out_temp
# result:
array([[0.  , 0.  , 0.  ],
       [0.95, 0.  , 0.  ],
       [0.  , 0.  , 0.9 ]])

out = [0 if not x.any() else x.argmax()+1 for x in out_temp]
out
# result:
[0, 1, 3]
© www.soinside.com 2019 - 2024. All rights reserved.