#!/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 |
正文完