利用决策树简单构建GBDT模型

利用决策树简单构建 GBDT 模型

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from numpy import ndarray
import numpy as np
from sklearn.tree import DecisionTreeClassifier

class GBDT():
    def __init__(self, max_tree_num = 3):
        """
        In this function you need to initialize the Gradient Boosting Decision Tree
        :param  max_tree_num: Maximum number of boosting tree,default = 3
        """

        self.max_tree_num = max_tree_num
        self.tree_list = []
    
        
    def fit(self, x, y):
        """
        In this function you need to fit the input data and label to the decision Tree
        :param x: Training set X
        :param y: Training set Y
        """
        for i in range(self.max_tree_num):
            model = DecisionTreeClassifier()
            model.fit(x, y)
            self.tree_list.append(model)
            y -=  model.predict(x)

    def predict(self, x):
        """
        In this function you need to use the classifier to predict input test data 
        :param x: Input test data which format is ndarray
        :return: Return the prediction you got from model
        """
        y_pred = np.zeros(x.shape[0])
        for model in self.tree_list:
            y_pred += model.predict(x)
        
        return y_pred
正文完
 
admin
版权声明:本站原创文章,由 admin 2023-11-26发表,共计840字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请联系tensortimes@gmail.com。
评论(没有评论)
验证码