Day 26 : LightGBM 与 GridSearch

LightGBM

GBDT(Gradient Boosting Decision Tree) 是利用弱分类器迭代训练来得到最佳的模型,而 LightGBM(Light Gradient Boosting Machine)是实现 GBDT 的演算法。

由於 XGBoost 在训练的时候空间暂存大,而 LightGBM 优化了 XGBoost 训练的缺陷,在同样的条件下加速 GBDT 演算法的运算。LightGBM 修补了 GBDT 在巨量资料下会遇到记忆体限制、速度上的限制,更适合於实际面的应用。

Leaf-wise的优点是在分裂次数相同的情况下,可以降低更多的误差以达到更好的准确度;但是叶子可能因此一直长下去导致过度拟合,这里可以增加 max_depth 的数量限制防止过拟合。

LightGBM 特色

参考来源

  • 速度更快
    • 采用直方图演算法将遍历样本转变为遍历直方图,极大的降低了时间复杂度。
    • 训练过程中采用单边梯度演算法过滤掉梯度小的样本,减少了大量的计算。
    • 采用了基於 Leaf-wise 演算法的增长策略构建树,减少了很多不必要的计算量。
    • 采用优化後的特徵并行、资料并行方法加速计算,当资料量非常大的时候还可以采用投票并行的策略。
    • 对快取也进行了优化,增加了快取命中率。
  • 记忆体消耗更小
    • 采用直方图演算法将储存特徵值转变为储存 bin 值,降低了记忆体消耗。
    • 训练过程中采用互斥特徵捆绑演算法减少了特徵数量,降低了记忆体消耗。

安装方式

pip install lightgbm

实作程序码

import lightgbm as lgb
from lightgbm import LGBMClassifier

classifier = lgb.LGBMClassifier(objective = 'binary', 
                                learning_rate = 0.05, 
                                n_estimators = 100, 
                                random_state=0)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

print(cm)
>>> [[59  8]
     [ 4 29]]
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))

看图说故事

faeture importance

LGBM_grid_measure = measure_performance(X = X_test, y = y_test, clf = classifier, show_classification_report=True, show_confusion_matrix=True)

# feature importances
print('Feature importances:', list(classifier.feature_importances_))

# visualization
print('Plot feature importances...')
ax = lgb.plot_importance(classifier, max_num_features=len(train))
plt.show()

树状图

ax = lgb.plot_tree(classifier, figsize=(20, 8), show_info=['split_gain'])
plt.show()

比较目前所学的演算法

我们发现基本上有 ensemble 的演算法准确度都相当地高,另外这份资料集刚好对 KNN 也有不错的分类效果。

GridSearch

在机器学习中,本身的资料源和特徵资料处理其实是界定模型的准度上限(upper bound),不同的演算法只是去逼近这个上限。我们测试不同的演算法同时,我们可以进行不同参数的评估与计算准确率来逼近上限。

以下用 LightGBM 来示范 GridSearch(网格搜索法),可以输入自己想要的参数组合,藉此找到最优的参数。

实作程序码

# 建立参数
param_grid = {
    'num_leaves': [30, 40], 
    'feature_fraction': [0.2, 0.3],
    'bagging_fraction': [0.6, 0.7],
    'max_depth':[3, 5, 7],
    'max_bin':[20],
    'lambda_l1':[0.3, 0.6],
    'lambda_l2':[0.08, 0.09],
    'min_split_gain':[0.04, 0.05],
    'min_child_weight':[7]
}

# 建立 LightGBM模型
classifier = lgb.LGBMClassifier(objective = 'binary', 
                                learning_rate = 0.05, 
                                n_estimators = 100, 
                                random_state=0)
# GridSearchCV
from sklearn.model_selection import GridSearchCV
gridsearch = GridSearchCV(classifier, param_grid)

# Final Model
print('Start predicting...')
LGBM = lgb.LGBMClassifier(objective = 'binary',
                         learning_rate = 0.05, 
                         n_estimators = 100, 
                         random_state=0,
                         num_leaves = gridsearch.best_params_['num_leaves'],
                         feature_fraction = gridsearch.best_params_['feature_fraction'], 
                         bagging_fraction = gridsearch.best_params_['bagging_fraction'],
                         max_depth = gridsearch.best_params_['max_depth'],
                         max_bin = gridsearch.best_params_['max_bin'],
                         lambda_l1 = gridsearch.best_params_['lambda_l1'],
                         lambda_l2 = gridsearch.best_params_['lambda_l2'],
                         min_split_gain = gridsearch.best_params_['min_split_gain'],
                         min_child_weight = gridsearch.best_params_['min_child_weight'])
%time LGBM_fit = LGBM.fit(X_train, y_train)
print('Predicting is over')

github 程序码

更详细可以请参考


<<:  [Java Day27] 6.4. 多型

>>:  予焦啦!附录:诡异的时间中断(timer interrupt)搁置位元(pending bit)

Day 12 - Using List<T> to Store JSON Format Path with ASP.NET Web Forms C# 用强类型物件清单储存 JSON 格式的相簿图片路径

=x= 🌵 建立後台相簿管理并使用 JSON 格式储存多个图片的路径。 相簿管理功能介绍 : 📌 这...

线性串列的循序储存 - DAY 4

定义 指的是用一段连续的储存单元一次储存线性串列的资料元素 优缺 优点: 无须为表示串列中元素之间的...

AE-Lightning 雷电云特效4-Day26

终於倒数胜4天了!! 接续昨天的练习 1.在light comp加入一个Solid Composit...

装置在Windows上未就绪错误如何重新连接您的磁碟机

装置未就绪是Windows作业系统一个常见的错误。在Windows OS的所有版本中均会看到此错误。...

初探 YC Startup School

Y Combinator,简称 YC,是国际上十分知名的加速器之一,其中最大的特点在於他们的新创社群...