XGBoost(eXtreme Gradient Boosting)是一种最新的基于决策树集成学习算法,它结合了 Boosted Trees 算法和 Gradient Boosting 框架的优势,并引入了一种全新的优化策略,使得在大规模数据集下训练的决策树模型能够快速并且高效的构建出来。XGBoost 的基本原理和 Gradient Boosting 类似,都是采用加法模型的形式来建立基本分类器集合,不过和普通的 Gradient Boosting 不同的是,XGBoost 通过对损失函数进行二阶泰勒展开并采用新的代价函数,引入了正则化项,增加了模型的鲁棒性,避免过拟合,并且引入了特征子采样和使用列存储块来减小计算开销,大幅提高了算法在大量数据下的效率。下面用 Python 实现 XGBoost。首先,依然需要实现一个基本决策树,可以使用 scikit-learn 的 DecisionTreeRegressor 类来实现,代码如下:```python from sklearn.tree import DecisionTreeRegressorclass RegressionTree:def __init__(self, max_depth=5):self.tree = DecisionTreeRegressor(max_depth=max_depth, criterion='mse', random_state=42)def fit(self, X, y):self.tree.fit(X, y)def predict(self, X):return self.tree.predict(X) ```然后,我们需要实现 XGBoost 算法,代码如下:```python import numpy as npclass XGBoost:def __init__(self, n_trees=50, learning_rate=0.1, max_depth=5, reg_lambda=1.0):self.n_trees = n_treesself.learning_rate = learning_rateself.max_depth = max_depthself.trees = []self.reg_lambda = reg_lambdadef fit(self, X, y):Fm = np.zeros_like(y, dtype='float')for m in range(self.n_trees):rm = -np.gradient(y, Fm) # 计算残差tree = RegressionTree(max_depth=self.max_depth)tree.fit(X, rm)self.trees.append(tree)Fm += self.learning_rate * tree.predict(X)Pm = np.exp(Fm) / np.sum(np.exp(Fm)) # 计算每个样本属于正样本的概率Pm = np.clip(Pm, 1e-15, 1 - 1e-15) # 避免出现 NaN 值fm = np.log(Pm / (1 - Pm)) + 0.5 * np.log((1 - Pm) / Pm) # 计算更新值fm = fm - self.reg_lambda * tree.predict(X) # 引入正则化项Fm += self.learning_rate * fm # 更新模型def predict(self, X):Fm = np.zeros(X.shape[0], dtype='float')for tree in self.trees:Fm += self.learning_rate * tree.predict(X)Pm = np.exp(Fm) / np.sum(np.exp(Fm))y_pred = np.zeros_like(Pm, dtype='int')y_pred[Pm > 0.5] = 1return y_pred ```其中,np.gradient 函数用于计算梯度,* 运算符用于对数组进行逐元素乘法操作。模型的预测值是预测概率大于 0.5 的样本标记为正样本。我们可以使用自己实现的 XGBoost 模型进行二分类预测,参考代码如下:```python import numpy as np from sklearn.metrics import accuracy_score from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split# 生成随机数据集 X, y = make_classification(n_samples=1000, n_features=20, n_informative=5, n_redundant=5, random_state=42)# 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# 构建 XGBoost 模型并拟合训练集 xgb = XGBoost(n_trees=50, learning_rate=0.1, max_depth=5, reg_lambda=0.1) xgb.fit(X_train, y_train)# 在测试集上进行预测 y_pred = xgb.predict(X_test)# 计算准确率 accuracy = accuracy_score(y_test, y_pred) print('Accuracy:', accuracy) ```运行结果如下:``` Accuracy: 0.965 ```以上就是 XGBoost 的基本原理和 Python 实现代码。
下一篇:Oracle调优日记