首页 > 解决方案 > 如何使用 Python 在随机森林中投票?

问题描述

我尝试使用 Python 实现随机森林,我知道有库 sklearn 但它使用的是概率。我尝试做的是使用投票来决定班级。准确度差很多。输入类是 0 和 1。

这是我的代码

from sklearn.preprocessing import LabelEncoder

estimators = []
preditions = []

labelenc_ = LabelEncoder()
labelenc_.fit(Y_train)
classes_ = labelenc_.classes_

for i in range(5):
    t = DecisionTreeClassifier(max_depth=1, criterion='entropy', max_features='log2')
    idx = np.random.choice(X_train.index ,size=len(X_train), replace=True)
    fitted_clf= clone(t).fit(X_train.loc[idx,:],labelenc_.transform( Y_train[idx]))
    estimators.append(fitted_clf)

pool = np.asarray([clf.predict(X_test) for clf in estimators]).T
v = tmp= np.apply_along_axis(lambda x: np.argmax(np.bincount(x))  ,arr=pool,axis=1)
v = labelenc_.inverse_transform(v)

我在 sklearn 中使用 predict() 方法获得大约 0.7 的准确度,但是当我使用我的代码时,准确度在 0.5 左右,具有相同的参数

标签: pythonrandom-forestvotingensemble-learning

解决方案


推荐阅读