GBDT分类


#now let's get started
n_times = 20
learning_rate=0.1
log_of_odds=np.zeros([n_times+1,n_samples])

residuals = np.zeros([n_times+1,n_samples])
loss = [0] * (n_times+1)
prediction = np.zeros([n_times+1,n_samples])
score = np.zeros([n_times+1])
#calculation
log_of_odds[0] = [log_of_odds0] * n_samples
prediction[0] = [probability0] * n_samples
score[0]=np.sum((prediction[0]>0.5)*1.0==y) / n_samples
residuals[0] = y - prediction[0]
loss[0] = loss0.sum()
trees = []
gamma_value = np.zeros([n_times+1,8])
gamma = np.zeros([n_times+1,n_samples])

for i in range(n_times):
    dt = DecisionTreeRegressor(max_depth=2)
    dt=dt.fit(X, residuals[i])
    tree = dt.tree_
    trees.append(tree)
    leaf_indeces=dt.apply(X)
    unique_leafs=np.unique(leaf_indeces)
    n_leaf=len(unique_leafs)
    #for leaf 1
    for ileaf in range(n_leaf):
        leaf_index=unique_leafs[ileaf]
        n_leaf=len(leaf_indeces[leaf_indeces==leaf_index])
        previous_probability = prediction[i][leaf_indeces==leaf_index]
        denominator = np.sum(previous_probability * (1-previous_probability))
        igamma = tree.value[ileaf+1][0][0] * n_leaf / denominator
        gamma_value[i][ileaf]=igamma

    gamma[i] = [gamma_value[i][np.where(unique_leafs==index)] for index in leaf_indeces]
    # next 
    log_of_odds[i+1] = log_of_odds[i] + learning_rate * gamma[i]

    prediction[i+1] = np.array([np.exp(odds)/(np.exp(odds)+1) for odds in log_of_odds[i+1]])
    score[i+1]=np.sum((prediction[i+1]>0.5)*1.0==y) / n_samples
    residuals[i+1] = y - prediction[i+1]
    loss[i+1]=np.sum(-y * log_of_odds[i+1] + np.log(1+np.exp(log_of_odds[i+1])))



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GBDT(Gradient Boosting Decision Tree)是一种集成学习算法,它通过迭代训练一系列的决策树来完成分类任务。下面是一个使用GBDT进行分类的实例分析: 假设我们有一个二分类问题,需要根据一些特征来预测一个样本是属于类别A还是类别B。我们可以使用GBDT来解决这个问题。 首先,我们需要准备训练数据集,其中包含一些已知样本的特征和对应的类别标签。通常,特征可以是数值型或者类别型的。 然后,我们定义GBDT模型的参数,比如决策树的最大深度、学习率等。这些参数会影响模型的性能和复杂度。 接下来,我们开始训练模型。GBDT模型的训练过程是迭代的,每次迭代都会拟合一个新的决策树来纠正之前模型预测错误的样本。在每次迭代中,新的决策树会以负梯度损失函数为目标进行训练。训练过程中,模型会逐步收敛,不断提升预测性能。 最后,我们可以使用训练好的GBDT模型来进行预测。给定一个新样本的特征,模型会通过组合各个决策树的预测结果来得到最终的分类结果。 需要注意的是,GBDT模型在处理高维稀疏数据时可能会遇到一些挑战,此时可以考虑使用特征工程方法进行数据预处理,或者尝试其他适合稀疏数据的分类算法。 以上是一个简单的GBDT分类实例分析,希望对你有所帮助。如有更多问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值