ldcTree Python实现

本文介绍了一种基于深度学习的转换率预测模型——多级深度级联树(Multi-Level Deep Cascade Trees),并分享了实现该模型的核心代码。该模型通过将GBDT每棵树的交叉熵输出作为下一层输入,以提高预测精度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近看了论文Multi-Level Deep Cascade Trees for Conversion Rate Prediction
发现作者没有开源相关代码,就自己试着写了一下
核心思想就是把上一层GBDT每棵树的交叉熵输出作为下一层的输入
核心代码如下

    def gen_new_train_X(self, X, y, gb_classifier):
        # 保存一个用于寻找叶子节点的X
        array_x = np.array(X).astype(np.float32)
        X = None

        for j in range(len(gb_classifier.estimators_)):
            # 获得每个树
            decision_tree = gb_classifier.estimators_[j, 0].tree_
            train_leave_id = decision_tree.apply(array_x)
            temp_df = DataFrame(train_leave_id, columns=['leave_id'])

            # 取得对应的回归值列表
            regress_l = []
            for k in range(len(decision_tree.value)):
                regress_l.append(decision_tree.value[k, 0, 0])
            temp_df['prob'] = self.sigmoid(temp_df['leave_id'].map(lambda x: regress_l[x]))

            # 计算每个叶子节点对应的交叉熵
            # 用dict记录一下
            dic = {}
            # 我只需要取值空间
            leave_ids = temp_df['leave_id'].unique()
            for leave_id in leave_ids:
                # 计算对应的交叉熵
                temp_cross_entropy = metrics.log_loss(y[temp_df.leave_id == leave_id],
                                                      temp_df[temp_df.leave_id == leave_id]['prob'], labels=[1, 0])
                dic[leave_id] = temp_cross_entropy
            self.cross_entropys.append(dic)
            if X is None:
                X = DataFrame(np.array(temp_df['leave_id'].map(lambda x: dic[x])), columns=['cross_entropy0'])
            else:
                X['cross_entropy' + str(j)] = temp_df['leave_id'].map(lambda x: dic[x])
        return X

全部代码见github:https://github.com/sunjiaxin111/ldcTree

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值