随机森林

随机森林由多棵决策树构成, 可以对样本集合进行多次有放回的采样, 并构建相应的决策树, 对于预测样本, 遍历每一刻决策树, 记录相应结果, 采取相应的总结方法, 总结结果作为随机森林的输出
import numpy as np
from .DT import DecisionTree

#抽样自助
def bootstrap_sample(X, Y):

    N, M = X.shape
    idxs = np.random.choice(N, N, replace=True)
    return X[idxs], Y[idxs]

class RandomForest:

    def __init__(self, n_trees, max_depth, n_feats, classifier=True, criterion="entropy"):
    
        self.trees = [] # 存放决策树
        self.n_trees = n_trees  # 决策树的数目
        self.n_feats = n_feats  # 选取的特征数
        self.max_depth = max_depth  # 树的深度
        self.criterion = criterion   # 划分属性准则
        self.classifier = classifier

    def fit(self, X, Y):
        # 训练多颗决策树
        self.trees = []
        for _ in range(self.n_trees):
            X_samp, Y_samp = bootstrap_sample(X, Y)
            tree = DecisionTree(n_feats=self.n_feats,
                                max_depth=self.max_depth,
                                criterion=self.criterion,
                                classifier=self.classifier)
            tree.fit(X_samp, Y_samp)
            self.trees.append(tree)

    def predict(self, X):
        # 返回一个二维矩阵 每一行为一个决策树对预测样本的输出 维度 n_trees * X.shape[0]
        tree_preds = np.array([[t._traverse(x, t.root) for x in X] for t in self.trees])
        return self._vote(tree_preds)

    def _vote(self, predictions):

        if self.classifier:
            out = [np.bincount(x).argmax() for x in predictions.T]
        else:
            out = [np.mean(x) for x in predictions.T]
        return np.array(out)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值