【机器学习】嘿马机器学习(算法篇)第12篇:集成学习进阶,5.4 otto案例介绍 -- Otto Group Product

🏆🏆🏆教程全知识点简介:1.定位、目标。2. K-近邻算法涵盖距离度量、k值选择、kd树、鸢尾花种类预测数据集介绍、练一练、交叉验证网格搜索、facebook签到位置预测案例。3. 线性回归包括线性回归简介、线性回归损失和优化、梯度下降法介绍、波士顿房价预测案例、欠拟合和过拟合、正则化线性模型、正规方程推导方式、梯度下降法算法比较优化、维灾难。4. 逻辑回归涵盖逻辑回归介绍、癌症分类预测案例(良恶性乳腺癌肿瘤预测、获取数据)、ROC曲线绘制。5. 朴素贝叶斯算法包括朴素贝叶斯算法简介、概率基础复习、产品评论情感分析案例(取出内容列数据分析、判定评判标准好评差评)。6. 支持向量机涵盖SVM算法原理、SVM损失函数、数字识别器案例。7. 决策树算法包括决策树分类原理、cart剪枝、特征工程特征提取、决策树算法api、泰坦尼克号乘客生存预测案例。8. EM算法涵盖初识EM算法、EM算法介绍。9. HMM模型包括马尔科夫链、HMM简介、前向后向算法评估观察序列概率、维特比算法解码隐藏状态序列、HMM模型API介绍。10. 集成学习进阶涵盖Bagging、xgboost算法原理、otto案例(Otto Group Product Classification Challenge xgboost实现)、数据变化可视化、lightGBM、stacking算法基本思想、住房月租金预测。11. 聚类算法包括聚类算法api初步使用、聚类算法实现流程、模型评估、算法优化、特征降维、用户对物品类别喜好细分案例、算法选择指导。12. 数学基础涵盖向量与矩阵范数、朗格朗日乘子法、Huber Loss、极大似然函数取对数原因。

📚📚仓库code.zip 👉直接-->: https://gitee.com/yinuo112/AI/blob/master/机器学习/嘿马机器学习(算法篇)/note.md 🍅🍅
✨ 本教程项目亮点
🧠 知识体系完整:覆盖从基础原理、核心方法到高阶应用的全流程内容
💻 全技术链覆盖:完整前后端技术栈,涵盖开发必备技能
🚀 从零到实战:适合 0 基础入门到提升,循序渐进掌握核心能力
📚 丰富文档与代码示例:涵盖多种场景,可运行、可复用
🛠 工作与学习双参考:不仅适合系统化学习,更可作为日常开发中的查阅手册
🧩 模块化知识结构:按知识点分章节,便于快速定位和复习
📈 长期可用的技术积累:不止一次学习,而是能伴随工作与项目长期参考
🎯🎯🎯全教程总章节
🚀🚀🚀本篇主要内容
集成学习进阶
学习目标
- 知道xgboost算法原理
- 知道otto案例通过xgboost实现流程
- 知道lightGBM算法原理
- 知道PUBG案例通过lightGBM实现流程
- 知道stacking算法原理
- 知道住房月租金预测通过stacking实现流程
5.4 otto案例介绍 -- Otto Group Product Classification Challenge【xgboost实现】
1 背景介绍
奥托集团是世界上最大的电子商务公司之一,在20多个国家设有子公司。该公司每天都在世界各地销售数百万种产品,所以对其产品根据性能合理的分类非常重要。
不过,在实际工作中,工作人员发现,许多相同的产品得到了不同的分类。本案例要求,你对奥拓集团的产品进行正确的分分类。尽可能的提供分类的准确性。
链接:[
![2nd iteration]
2 思路分析
-
1.数据获取
-
2.数据基本处理
-
2.1 截取部分数据
- 2.2 把标签纸转换为数字
- 2.3 分割数据(使用StratifiedShuffleSplit)
- 2.4 数据标准化
-
2.5 数据pca降维
-
3.模型训练
-
3.1 基本模型训练
-
3.2 模型调优
-
3.2.1 调优参数:
- n_estimator,
- max_depth,
- min_child_weights,
- subsamples,
- consample_bytrees,
- etas
-
3.2.2 确定最后最优参数
-
3 部分代码实现
-
2.数据基本处理
-
2.1 截取部分数据
-
2.2 把标签纸转换为数字
-
2.3 分割数据(使用StratifiedShuffleSplit)
# 使用StratifiedShuffleSplit对数据集进行分割
from sklearn.model_selection import StratifiedShuffleSplit
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=0)
for train_index, test_index in sss.split(X_resampled.values, y_resampled):
print(len(train_index))
print(len(test_index))
x_train = X_resampled.values[train_index]
x_val = X_resampled.values[test_index]
y_train = y_resampled[train_index]
y_val = y_resampled[test_index]
# 分割数据图形可视化
import seaborn as sns
sns.countplot(y_val)
plt.show()
- 2.4 数据标准化
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(x_train)
x_train_scaled = scaler.transform(x_train)
x_val_scaled = scaler.transform(x_val)
- 2.5 数据pca降维
print(x_train_scaled.shape)
# (13888, 93)
from sklearn.decomposition import PCA
pca = PCA(n_components=0.9)
x_train_pca = pca.fit_transform(x_train_scaled)
x_val_pca = pca.transform(x_val_scaled)
print(x_train_pca.shape, x_val_pca.shape)
(13888, 65) (3473, 65)
从上面输出的数据可以看出,只选择65个元素,就可以表达出特征中90%的信息
# 降维数据可视化
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel("元素数量")
plt.ylabel("可表达信息的百分占比")
plt.show()
![image-20200211092529327]
-
3.模型训练
-
3.1 基本模型训练
from xgboost import XGBClassifier
xgb = XGBClassifier()
xgb.fit(x_train_pca, y_train)
# 改变预测值的输出模式,让输出结果为百分占比,降低logloss值
y_pre_proba = xgb.predict_proba(x_val_pca)
# logloss进行模型评估
from sklearn.metrics import log_loss
log_loss(y_val, y_pre_proba, eps=1e-15, normalize=True)
xgb.get_params
-
3.2 模型调优
-
3.2.1 调优参数:
- n_estimator,
scores_ne = []
n_estimators = [100,200,400,450,500,550,600,700]
for nes in n_estimators:
print("n_estimators:", nes)
xgb = XGBClassifier(max_depth=3,
learning_rate=0.1,
n_estimators=nes,
objective="multi:softprob",
n_jobs=-1,
nthread=4,
min_child_weight=1,
subsample=1,
colsample_bytree=1,
seed=42)
xgb.fit(x_train_pca, y_train)
y_pre = xgb.predict_proba(x_val_pca)
score = log_loss(y_val, y_pre)
scores_ne.append(score)
print("测试数据的logloss值为:{}".format(score))
# 数据变化可视化
plt.plot(n_estimators, scores_ne, "o-")
plt.ylabel("log_loss")
plt.xlabel("n_estimators")
print("n_estimators的最优值为:{}".format(n_estimators[np.argmin(scores_ne)]))
![image-20200211092901936]
* **max_depth,**
scores_md = []
max_depths = [1,3,5,6,7]
for md in max_depths: # 修改
xgb = XGBClassifier(max_depth=md, # 修改
learning_rate=0.1,
n_estimators=n_estimators[np.argmin(scores_ne)], # 修改
objective="multi:softprob",
n_jobs=-1,
nthread=4,
min_child_weight=1,
subsample=1,
colsample_bytree=1,
seed=42)
xgb.fit(x_train_pca, y_train)
y_pre = xgb.predict_proba(x_val_pca)
score = log_loss(y_val, y_pre)
scores_md.append(score) # 修改
print("测试数据的logloss值为:{}".format(log_loss(y_val, y_pre)))
# 数据变化可视化
plt.plot(max_depths, scores_md, "o-") # 修改
plt.ylabel("log_loss")
plt.xlabel("max_depths") # 修改
print("max_depths的最优值为:{}".format(max_depths[np.argmin(scores_md)])) # 修改
* **min_child_weights,**
* 依据上面模式进行调整
* **subsamples,**
* **consample_bytrees,**
* **etas**
- 3.2.2 确定最后最优参数
xgb = XGBClassifier(learning_rate =0.1,
n_estimators=550,
max_depth=3,
min_child_weight=3,
subsample=0.7,
colsample_bytree=0.7,
nthread=4,
seed=42,
objective='multi:softprob')
xgb.fit(x_train_scaled, y_train)
y_pre = xgb.predict_proba(x_val_scaled)
print("测试数据的logloss值为 : {}".format(log_loss(y_val, y_pre, eps=1e-15, normalize=True)))
集成学习进阶
学习目标
- 知道xgboost算法原理
- 知道otto案例通过xgboost实现流程
- 知道lightGBM算法原理
- 知道PUBG案例通过lightGBM实现流程
- 知道stacking算法原理
- 知道住房月租金预测通过stacking实现流程
5.5 lightGBM
学习目标
- 了解lightGBM演进过程
- 知道什么是lightGBM
- 知道lightGBM原理
1 写在介绍lightGBM之前
1.1 lightGBM演进过程
![image-20191202130509524]
1.2 AdaBoost算法
AdaBoost是一种提升树的方法,和三个臭皮匠,赛过诸葛亮的道理一样。
AdaBoost两个问题:
-
(1) 如何改变训练数据的权重或概率分布
-
提高前一轮被弱分类器错误分类的样本的权重,降低前一轮被分对的权重
-
(2) 如何将弱分类器组合成一个强分类器,亦即,每个分类器,前面的权重如何设置
-
采取”多数表决”的方法.加大分类错误率小的弱分类器的权重,使其作用较大,而减小分类错误率大的弱分类器的权重,使其在表决中起较小的作用。
1.3 GBDT算法以及优缺点
GBDT和AdaBosst很类似,但是又有所不同。
-
GBDT和其它Boosting算法一样,通过将表现一般的几个模型(通常是深度固定的决策树)组合在一起来集成一个表现较好的模型。
-
AdaBoost是通过提升错分数据点的权重来定位模型的不足, Gradient Boosting通过负梯度来识别问题,通过计算负梯度来改进模型,即通过反复地选择一个指向负梯度方向的函数,该算法可被看做在函数空间里对目标函数进行优化。
因此可以说 。
- GradientBoosting=GradientDescent+BoostingGradientBoosting=GradientDescent+BoostingGradientBoosting=GradientDescent+Boosting
- 点赞
- 收藏
- 关注作者
评论(0)