Light GBM(也被称为Light Gradient Boosting Machine)和XGBoost(也被称为Extreme Gradient Boost)是两种最受欢迎的免费开源决策树集成算法,它们使用了梯度Boosting框架。在他们各自的网站上,都声称他们的算法快速且准确。
数据拟合梯度提升算法
梯度提升继承了决策树算法,该算法一次添加一棵树并集成决策树以适应和纠正错误预测。拟合和纠正误差预测,也称为“梯度提升”,因为在训练模型时损失梯度被最小化。
此外,梯度提升算法在Kaggle机器学习竞赛中非常受欢迎,用于解决表格或结构化数据集问题。
XGBoost(eXtreme Gradient Boosting)是一种机器学习算法,专注于计算速度和机器学习模型性能。
XGBoost的优点:
Light GBM是一个分布式高性能框架,它使用决策树进行排名、分类和回归任务。
Light GBM的优点:
LightGBM和XGBoost 都只在数据中有需要转换为数值特征的数值特征时才接受数值特征。但是在XGboost中,对于更大的数据集,这是一个问题,因为编码需要更长的时间。
我们使用Scikit-learn库中的Make_Classification来生成一个随机分类样本。我们生成1,000,000个数据,并将其分进行拆分。我们有 900,000 个训练数据集和 100,000 个测试数据集。
from sklearn.datasets import make_classification # import data generator for clasification
from sklearn.model_selection import train_test_split # import feature splitter
X, y = make_classification(
n_samples=1000000, # row number
n_features=20, # feature numbers
n_informative=6, # The number of informative features
n_redundant = 2, # The number of redundant features
n_repeated = 2, # The number of duplicated features
n_classes = 2, # The number of classes
n_clusters_per_class=1,#The number of clusters per class
random_state = 42 # random seed
)
# split data set
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.10, random_state=12)
在使用定义模型时,我们使用基本模型(不含任何超参数的模型),将其与Scikit-learn交叉验证的准确性评分和每个模型的执行时间进行比较,也使用数据测试来查看模型是否过拟合。
XGBoost
我们已经从上面的 xgboost 库中加载了 XGBoostClassifier 类。现在我们可以定义分类器模型。
#library for xgboost
import xgboost as xgb
# Xgboost model definition
xgb_c = xgb.XGBClassifier()
我们将用训练数据拟合模型
# XGBoost training model
%%time # google colab time execution info
xgb_c.fit(X_train,y_train)
然后,我们将通过交叉验证检查训练的准确性
#library for cross validation
from sklearn.model_selection import cross_val_score
# cross validation 5 times
scores = cross_val_score(xgb_c, X_train, y_train, cv=5)
最后,我们将预测测试数据并使用 accuracy_score 检查预测准确度以评估预测结果。
LightGBM
我们已经从上面的 LightGBM 库中加载了LightGBMClassifier类。现在我们可以定义分类器模型。Python代码如下:
# import light gbm library
import lightgbm as lgb
# Light gbm model definition using basemodel
model_lgbm = lgb.LGBMClassifier()
# Cross validation
scores = cross_val_score(model_lgbm, X_train, y_train, cv=5)
我们将用训练数据拟合模型并查看执行持续时间
# Light gbm training
%%time # count execution duration in google colabs
model_lgbm.fit(X_train,y_train)
然后,我们再次通过交叉验证检查训练的准确性,Python代码如下:
# import cross validation from sci-kit learn
from sklearn.model_selection import cross_val_score
# Cross validation Light GBM
scores = cross_val_score(model_lgbm, X_train, y_train, cv=5)
最后,我们将预测测试数据并使用 accuracy_score 检查预测准确度以评估预测LightGBM算法的结果。Python代码如下:
# import accuracy_score metric from sci-kit learn
from sklearn.metrics import accuracy_score
# predict test data with light GBM
lgbm_pred = model_lgbm.predict(X_test)
# print out the accuracy result with test data
print(accuracy_score(y_test,lgbm_pred))
准确度得分
准确度不是精度
基于交叉验证值,XGBoost 算法为 97.5%,LightGBM 为 98.3%。这意味着 LightGBM 比 XGBoost 更好。
训练时间
训练模型的持续时间
对于训练执行时间,LightGBM 的速度要快得多,因为 LGBM 与大型数据集兼容。
这是否意味着XGBoost不是一个好的算法呢?XGBoost仍然是一个很棒的算法,事实上,这个实验只使用了基本模型(没有超参数的模型),这意味着我们没有发挥XGBoost的真正潜力。如果你想尝试使用大数据集并得到快速的结果来评估,我们建议你使用Light GBM。如果您有更多时间,我们鼓励您同时使用这两种算法。
留言与评论(共有 0 条评论) “” |