如何使用Python在随机林中投票?

2024-05-14 03:55:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用Python实现随机林,我知道有库可以学习,但它使用的是概率。我试着用投票来决定班级。准确度是如此的不同。输入类是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,参数相同


Tags: infornptransformtrainsklearnmaxfit