otto案例介绍 -- Otto Group Product Classification Challenge【xgboost实现】

本文主要是介绍otto案例介绍 -- Otto Group Product Classification Challenge【xgboost实现】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【机器学习】otto案例介绍

  • 1. 背景介绍
  • 2. 思路分析
  • 3. 代码实现
    • 3.1 数据获取
    • 3.2 数据基本处理
      • 3.2.1 截取部分数据
      • 3.2.2 把标签值转换为数字
      • 3.2.3 分割数据
      • 3.2.4 数据标准化
      • 3.2.5 数据PCA降维
    • 3.3 模型训练
      • 3.3.1 基本模型训练
      • 3.3.2 模型调优
        • 3.3.2.1 确定最优的estimators
        • 3.3.2.2 确定最优的max_depth
        • 3.3.2.3 依据上面模式,运行调试下面参数
      • 3.3.3 最优模型

1. 背景介绍

奥托集团是世界上最⼤的电⼦商务公司之⼀,在20多个国家设有⼦公司。该公司每天都在世界各地销售数百万种产品, 所以对其产品根据性能合理的分类⾮常重要。

不过,在实际⼯作中,⼯作⼈员发现,许多相同的产品得到了不同的分类。本案例要求,你对奥拓集团的产品进⾏正确的分分 类。尽可能的提供分类的准确性。

链接:https://www.kaggle.com/c/otto-group-product-classification-challenge/overview

在这里插入图片描述

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. 代码实现

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

3.1 数据获取

data = pd.read_csv("./data/otto/train.csv")

在这里插入图片描述

data.shape
(61878, 95)
data.describe()

在这里插入图片描述

# 图形可视化,查看数据分布
import seaborn as snssns.countplot(data.target)plt.show()

在这里插入图片描述
由上图可以看出,该数据类别不均衡,所以需要后期处理

3.2 数据基本处理

数据已经经过脱敏,不再需要特殊处理

3.2.1 截取部分数据

new1_data = data[:10000]
new1_data.shape
# 图形可视化,查看数据分布
import seaborn as snssns.countplot(new1_data.target)plt.show()

在这里插入图片描述
使用上面方式获取数据不可行,然后使用随机欠采样获取响应的数据

# 随机欠采样获取数据
# 首先需要确定特征值\标签值y = data["target"]
x = data.drop(["id", "target"], axis=1)

在这里插入图片描述

# 欠采样获取数据
from imblearn.under_sampling import RandomUnderSamplerrus = RandomUnderSampler(random_state=0)X_resampled, y_resampled = rus.fit_resample(x, y)
x.shape, y.shape
X_resampled.shape, y_resampled.shape

在这里插入图片描述

# 图形可视化,查看数据分布
import seaborn as snssns.countplot(y_resampled)plt.show()

在这里插入图片描述

3.2.2 把标签值转换为数字

y_resampled.head()

在这里插入图片描述

from sklearn.preprocessing import LabelEncoderle = LabelEncoder()
y_resampled = le.fit_transform(y_resampled)

在这里插入图片描述

3.2.3 分割数据

from sklearn.model_selection import train_test_splitx_train, x_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2)
x_train.shape, y_train.shape

在这里插入图片描述

# 图形可视化
import seaborn as snssns.countplot(y_test)
plt.show()

在这里插入图片描述

# 通过StratifiedShuffleSplit实现数据分割from sklearn.model_selection import StratifiedShuffleSplitsss = 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]

13888
3473

print(x_train.shape, x_val.shape)
(13888, 93) (3473, 93)
# 图形可视化
import seaborn as snssns.countplot(y_val)
plt.show()

在这里插入图片描述

3.2.4 数据标准化

from sklearn.preprocessing import StandardScalerscaler = StandardScaler()
scaler.fit(x_train)x_train_scaled = scaler.transform(x_train)
x_val_scaled = scaler.transform(x_val)

3.2.5 数据PCA降维

x_train_scaled.shape
(13888, 93)
from sklearn.decomposition import PCApca = 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)
# 可视化数据降维信息变化程度
plt.plot(np.cumsum(pca.explained_variance_ratio_))plt.xlabel("元素数量")
plt.ylabel("表达信息百分占比")plt.show()

在这里插入图片描述

3.3 模型训练

3.3.1 基本模型训练

from xgboost import XGBClassifierxgb = XGBClassifier()
xgb.fit(x_train_pca, y_train)
# 输出预测值,一定输出带有百分占比的预测值
y_pre_proba = xgb.predict_proba(x_val_pca)
y_pre_proba

在这里插入图片描述

# logloss评估
from sklearn.metrics import log_losslog_loss(y_val, y_pre_proba, eps=1e-15, normalize=True)
0.735851001353164
xgb.get_params

在这里插入图片描述

3.3.2 模型调优

3.3.2.1 确定最优的estimators
scores_ne = []
n_estimators = [100, 200, 300, 400, 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))
# 图形化展示相应的logloss值
plt.plot(n_estimators, scores_ne, "o-")plt.xlabel("n_estimators")
plt.ylabel("log_loss")
plt.show()print("最优的n_estimators值是:{}".format(n_estimators[np.argmin(scores_ne)]))
3.3.2.2 确定最优的max_depth
scores_md = []
max_depths = [1,3,5,6,7]
for md in max_depths:print("max_depth:", md)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(score))
# 图形化展示相应的logloss值
plt.plot(max_depths, scores_md, "o-")plt.xlabel("max_depths")
plt.ylabel("log_loss")
plt.show()print("最优的max_depths值是:{}".format(max_depths[np.argmin(scores_md)]))
3.3.2.3 依据上面模式,运行调试下面参数
min_child_weights,subsamples,consample_bytrees,etas

3.3.3 最优模型

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("测试数据的log_loss值为 : {}".format(log_loss(y_val, y_pre, eps=1e-15, normalize=True)))

加油!

感谢!

努力!

这篇关于otto案例介绍 -- Otto Group Product Classification Challenge【xgboost实现】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/404845

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import