1. 机器学习系列入门系列[七]:基于英雄联盟数据集的LightGBM的分类预测

本我的项目链接:https://www.heywhale.com/home/column/64141d6b1c8c8b518ba97dcc

1.1 LightGBM原理简介

LightGBM是2017年由微软推出的可扩大机器学习零碎,是微软旗下DMKT的一个开源我的项目,它是一款基于GBDT(梯度晋升决策树)算法的分布式梯度晋升框架,为了满足缩短模型计算工夫的需要,LightGBM的设计思路次要集中在减小数据对内存与计算性能的应用,以及缩小多机器并行计算时的通信代价。

LightGBM能够看作是XGBoost的降级豪华版,在取得与XGBoost近似精度的同时,又提供了更快的训练速度与更少的内存耗费。正如其名字中的Light所蕴含的那样,LightGBM在大规模数据集上跑起来更加优雅轻捷,一经推出便成为各种数据比赛中刷榜夺冠的神兵利器。

LightGBM底层实现了GBDT算法,并且增加了一系列的新个性:

  1. 基于直方图算法进行优化,使数据存储更加不便、运算更快、鲁棒性强、模型更加稳固等。
  2. 提出了带深度限度的 Leaf-wise 算法,摈弃了大多数GBDT工具应用的按层成长 (level-wise) 的决策树成长策略,而应用了带有深度限度的按叶子成长策略,能够升高误差,失去更好的精度。
  3. 提出了单边梯度采样算法,排除大部分小梯度的样本,仅用剩下的样本计算信息增益,它是一种在缩小数据量和保障精度上均衡的算法。
  4. 提出了互斥特色捆绑算法,高维度的数据往往是稠密的,这种稠密性启发咱们设计一种无损的办法来缩小特色的维度。通常被捆绑的特色都是互斥的(即特色不会同时为非零值,像one-hot),这样两个特色捆绑起来就不会失落信息。

LightGBM是基于CART树的集成模型,它的思维是串联多个决策树模型独特进行决策。

那么如何串联呢?LightGBM采纳迭代预测误差的办法串联。举个艰深的例子,咱们当初须要预测一辆车价值3000元。咱们构建决策树1训练后预测为2600元,咱们发现有400元的误差,那么决策树2的训练指标为400元,但决策树2的预测后果为350元,还存在50元的误差就交给第三棵树……以此类推,每一颗树用来预计之前所有树的误差,最初所有树预测后果的求和就是最终预测后果!

LightGBM的基模型是CART回归树,它有两个特点:(1)CART树,是一颗二叉树。(2)回归树,最初拟合后果是间断值。

LightGBM模型能够示意为以下模式,咱们约定$f_t(x)$示意前$t$颗树的和,$h_t(x)$示意第$t$颗决策树,模型定义如下:

$f_{t}(x)=\sum_{t=1}^{T} h_{t}(x)$

因为模型递归生成,第$t$步的模型由第$t-1$步的模型造成,能够写成:

$f_{t}(x)=f_{t-1}(x)+h_{t}(x)$

每次须要加上的树$h_t(x)$是之前树求和的误差:

$r_{t, i}=y_{i}-f_{m-1}\left(x_{i}\right)$

咱们每一步只有拟合一颗输入为$r_{t,i}$的CART树加到$f_{t-1}(x)$就能够了。

1.2 LightGBM的利用

LightGBM在机器学习与数据挖掘畛域有着极为宽泛的利用。据统计LightGBM模型自2016到2019年在Kaggle平台上累积取得数据比赛前三名三十余次,其中包含CIKM2017 AnalytiCup、IEEE Fraud Detection等出名比赛。这些比赛来源于各行各业的实在业务,这些比赛问题表明LightGBM具备很好的可扩展性,在各类不同问题上都能够获得十分好的成果。

同时,LightGBM还被胜利利用在工业界与学术界的各种问题中。例如金融风控、购买行为辨认、交通流量预测、环境声音分类、基因分类、生物成分剖析等诸多畛域。尽管畛域相干的数据分析和个性工程在这些解决方案中也施展了重要作用,但学习者与实践者对LightGBM的统一抉择表明了这一软件包的影响力与重要性。

2.相干流程

  • 理解 LightGBM 的参数与相干常识
  • 把握 LightGBM 的Python调用并将其使用到英雄联盟游戏输赢预测数据集上

Part1 基于英雄联盟数据集的LightGBM分类实际

  • Step1: 库函数导入
  • Step2: 数据读取/载入
  • Step3: 数据信息简略查看
  • Step4: 可视化形容
  • Step5: 利用 LightGBM 进行训练与预测
  • Step6: 利用 LightGBM 进行特征选择
  • Step7: 通过调整参数取得更好的成果

3.基于英雄联盟数据集的LightGBM分类实战

在实际的最开始,咱们首先须要导入一些根底的函数库包含:numpy (Python进行科学计算的根底软件包),pandas(pandas是一种疾速,弱小,灵便且易于应用的开源数据分析和解决工具),matplotlib和seaborn绘图。

#下载须要用到的数据集!wget https://tianchi-media.oss-cn-beijing.aliyuncs.com/DSW/8LightGBM/high_diamond_ranked_10min.csv
--2023-03-22 19:33:36--  https://tianchi-media.oss-cn-beijing.aliyuncs.com/DSW/8LightGBM/high_diamond_ranked_10min.csv正在解析主机 tianchi-media.oss-cn-beijing.aliyuncs.com (tianchi-media.oss-cn-beijing.aliyuncs.com)... 49.7.22.39正在连接 tianchi-media.oss-cn-beijing.aliyuncs.com (tianchi-media.oss-cn-beijing.aliyuncs.com)|49.7.22.39|:443... 已连贯。已收回 HTTP 申请,正在期待回应... 200 OK长度: 1446502 (1.4M) [text/csv]正在保留至: “high_diamond_ranked_10min.csv”high_diamond_ranked 100%[===================>]   1.38M  --.-KB/s    in 0.04s   2023-03-22 19:33:36 (38.3 MB/s) - 已保留 “high_diamond_ranked_10min.csv” [1446502/1446502])

Step1:函数库导入

##  根底函数库import numpy as np import pandas as pd## 绘图函数库import matplotlib.pyplot as pltimport seaborn as sns

本次咱们抉择英雄联盟数据集进行LightGBM的场景体验。英雄联盟是2009年美国拳头游戏开发的MOBA竞技网游,在每局较量中蓝队与红队在同一个地图进行作战,游戏的指标是毁坏敌方队伍的进攻塔,进而捣毁敌方的水晶枢纽,拿下较量的胜利。

当初共有9881场英雄联盟韩服钻石段位以上的排位较量数据,数据提供了在十分钟时的游戏状态,包含击杀数、死亡数、金币数量、经验值、等级……等信息。列blueWins是数据的标签,代表了本场较量是否为蓝队获胜。

数据的各个特征描述如下:

特色名称特色意义取值范畴
WardsPlaced插眼数量整数
WardsDestroyed拆眼数量整数
FirstBlood是否取得首次击杀整数
Kills击杀英雄数量整数
Deaths死亡数量整数
Assists助攻数量整数
EliteMonsters击杀大型野怪数量整数
Dragons击杀史诗野怪数量整数
Heralds击杀峡谷先锋数量整数
TowersDestroyed推塔数量整数
TotalGold总经济整数
AvgLevel均匀英雄等级浮点数
TotalExperience英雄总教训整数
TotalMinionsKilled英雄补兵数量整数
TotalJungleMinionsKilled英雄击杀野怪数量整数
GoldDiff经济差距整数
ExperienceDiff教训差距整数
CSPerMin分均补刀浮点数
GoldPerMin分均经济浮点数

Step2:数据读取/载入

## 咱们利用Pandas自带的read_csv函数读取并转化为DataFrame格局df = pd.read_csv('./high_diamond_ranked_10min.csv')y = df.blueWins

Step3:数据信息简略查看

## 利用.info()查看数据的整体信息df.info()
<class 'pandas.core.frame.DataFrame'>RangeIndex: 9879 entries, 0 to 9878Data columns (total 40 columns): #   Column                        Non-Null Count  Dtype  ---  ------                        --------------  -----   0   gameId                        9879 non-null   int64   1   blueWins                      9879 non-null   int64   2   blueWardsPlaced               9879 non-null   int64   3   blueWardsDestroyed            9879 non-null   int64   4   blueFirstBlood                9879 non-null   int64   5   blueKills                     9879 non-null   int64   6   blueDeaths                    9879 non-null   int64   7   blueAssists                   9879 non-null   int64   8   blueEliteMonsters             9879 non-null   int64   9   blueDragons                   9879 non-null   int64   10  blueHeralds                   9879 non-null   int64   11  blueTowersDestroyed           9879 non-null   int64   12  blueTotalGold                 9879 non-null   int64   13  blueAvgLevel                  9879 non-null   float64 14  blueTotalExperience           9879 non-null   int64   15  blueTotalMinionsKilled        9879 non-null   int64   16  blueTotalJungleMinionsKilled  9879 non-null   int64   17  blueGoldDiff                  9879 non-null   int64   18  blueExperienceDiff            9879 non-null   int64   19  blueCSPerMin                  9879 non-null   float64 20  blueGoldPerMin                9879 non-null   float64 21  redWardsPlaced                9879 non-null   int64   22  redWardsDestroyed             9879 non-null   int64   23  redFirstBlood                 9879 non-null   int64   24  redKills                      9879 non-null   int64   25  redDeaths                     9879 non-null   int64   26  redAssists                    9879 non-null   int64   27  redEliteMonsters              9879 non-null   int64   28  redDragons                    9879 non-null   int64   29  redHeralds                    9879 non-null   int64   30  redTowersDestroyed            9879 non-null   int64   31  redTotalGold                  9879 non-null   int64   32  redAvgLevel                   9879 non-null   float64 33  redTotalExperience            9879 non-null   int64   34  redTotalMinionsKilled         9879 non-null   int64   35  redTotalJungleMinionsKilled   9879 non-null   int64   36  redGoldDiff                   9879 non-null   int64   37  redExperienceDiff             9879 non-null   int64   38  redCSPerMin                   9879 non-null   float64 39  redGoldPerMin                 9879 non-null   float64dtypes: float64(6), int64(34)memory usage: 3.0 MB
## 进行简略的数据查看,咱们能够利用 .head() 头部.tail()尾部df.head()

<div>
<style scoped>

.dataframe tbody tr th:only-of-type {    vertical-align: middle;}.dataframe tbody tr th {    vertical-align: top;}.dataframe thead th {    text-align: right;}

</style>

gameIdblueWinsblueWardsPlacedblueWardsDestroyedblueFirstBloodblueKillsblueDeathsblueAssistsblueEliteMonstersblueDragons...redTowersDestroyedredTotalGoldredAvgLevelredTotalExperienceredTotalMinionsKilledredTotalJungleMinionsKilledredGoldDiffredExperienceDiffredCSPerMinredGoldPerMin
0451915782202821961100...0165676.81704719755-643819.71656.7
145233719490121055500...1176206.817438240522908117324.01762.0
2452147453001500711411...0172856.817254203281172103320.31728.5
345243840670431045510...0164787.017961235471321723.51647.8
444360337710754066600...0174047.018313225671004-23022.51740.4

<p>5 rows × 40 columns</p>
</div>

df.tail()

<div>
<style scoped>

.dataframe tbody tr th:only-of-type {    vertical-align: middle;}.dataframe tbody tr th {    vertical-align: top;}.dataframe thead th {    text-align: right;}

</style>

gameIdblueWinsblueWardsPlacedblueWardsDestroyedblueFirstBloodblueKillsblueDeathsblueAssistsblueEliteMonstersblueDragons...redTowersDestroyedredTotalGoldredAvgLevelredTotalExperienceredTotalMinionsKilledredTotalJungleMinionsKilledredGoldDiffredExperienceDiffredCSPerMinredGoldPerMin
987445278732861172174511...0152466.81649822934-2519-246922.91524.6
987545277974661540064811...0154567.01836720656-782-88820.61545.6
987645277137160231067500...0183197.419909261602416187726.11831.9
987745276283130144123311...0152987.21831424740839108524.71529.8
987845237729351180166500...0153396.81737920146-9275820.11533.9

<p>5 rows × 40 columns</p>
</div>

## 标注标签并利用value_counts函数查看训练集标签的数量y = df.blueWinsy.value_counts()
0    49491    4930Name: blueWins, dtype: int64

数据集正负标签数量基本相同,不存在数据不均衡的问题。

## 标注特色列drop_cols = ['gameId','blueWins']x = df.drop(drop_cols, axis=1)
## 对于特色进行一些统计形容x.describe()

<div>
<style scoped>

.dataframe tbody tr th:only-of-type {    vertical-align: middle;}.dataframe tbody tr th {    vertical-align: top;}.dataframe thead th {    text-align: right;}

</style>

blueWardsPlacedblueWardsDestroyedblueFirstBloodblueKillsblueDeathsblueAssistsblueEliteMonstersblueDragonsblueHeraldsblueTowersDestroyed...redTowersDestroyedredTotalGoldredAvgLevelredTotalExperienceredTotalMinionsKilledredTotalJungleMinionsKilledredGoldDiffredExperienceDiffredCSPerMinredGoldPerMin
count9879.0000009879.0000009879.0000009879.0000009879.0000009879.0000009879.0000009879.0000009879.0000009879.000000...9879.0000009879.0000009879.0000009879.0000009879.0000009879.0000009879.0000009879.0000009879.0000009879.000000
mean22.2882882.8248810.5048086.1839256.1376666.6451060.5499540.3619800.1879740.051422...0.04302116489.0414016.92531617961.730438217.34922651.313088-14.41411133.62030621.7349231648.904140
std18.0191772.1749980.5000023.0110282.9338184.0645200.6255270.4805970.3907120.244369...0.2169001490.8884060.3053111198.58391221.91166810.0278852453.3491791920.3704382.191167149.088841
min5.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.000000...0.00000011212.0000004.80000010465.000000107.0000004.000000-11467.000000-8348.00000010.7000001121.200000
25%14.0000001.0000000.0000004.0000004.0000004.0000000.0000000.0000000.0000000.000000...0.00000015427.5000006.80000017209.500000203.00000044.000000-1596.000000-1212.00000020.3000001542.750000
50%16.0000003.0000001.0000006.0000006.0000006.0000000.0000000.0000000.0000000.000000...0.00000016378.0000007.00000017974.000000218.00000051.000000-14.00000028.00000021.8000001637.800000
75%20.0000004.0000001.0000008.0000008.0000009.0000001.0000001.0000000.0000000.000000...0.00000017418.5000007.20000018764.500000233.00000057.0000001585.5000001290.50000023.3000001741.850000
max250.00000027.0000001.00000022.00000022.00000029.0000002.0000001.0000001.0000004.000000...2.00000022732.0000008.20000022269.000000289.00000092.00000010830.0000009333.00000028.9000002273.200000

<p>8 rows × 38 columns</p>
</div>

  • 咱们发现不同对局中插眼数和拆眼数的取值范畴存在显著差距,甚至有前十分钟插了250个眼的异样值。
  • 咱们发现EliteMonsters的取值相当于Deagons + Heralds。
  • 咱们发现TotalGold 等变量在大部分对局中差距不大。
  • 咱们发现两支队伍的经济差和教训差是相反数。
  • 咱们发现红队和蓝队拿到首次击杀的概率大略都是50%
## 依据下面的形容,咱们能够去除一些反复变量,比方只有晓得蓝队是否拿到一血,咱们就晓得红队有没有拿到,能够去除红队的相干冗余数据。drop_cols = ['redFirstBlood','redKills','redDeaths'             ,'redGoldDiff','redExperienceDiff', 'blueCSPerMin',            'blueGoldPerMin','redCSPerMin','redGoldPerMin']x.drop(drop_cols, axis=1, inplace=True)

Step4:可视化形容

data = xdata_std = (data - data.mean()) / data.std()data = pd.concat([y, data_std.iloc[:, 0:9]], axis=1)data = pd.melt(data, id_vars='blueWins', var_name='Features', value_name='Values')fig, ax = plt.subplots(1,2,figsize=(15,5))# 绘制小提琴图sns.violinplot(x='Features', y='Values', hue='blueWins', data=data, split=True,               inner='quart', ax=ax[0], palette='Blues')fig.autofmt_xdate(rotation=45)data = xdata_std = (data - data.mean()) / data.std()data = pd.concat([y, data_std.iloc[:, 9:18]], axis=1)data = pd.melt(data, id_vars='blueWins', var_name='Features', value_name='Values')# 绘制小提琴图sns.violinplot(x='Features', y='Values', hue='blueWins',                data=data, split=True, inner='quart', ax=ax[1], palette='Blues')fig.autofmt_xdate(rotation=45)plt.show()

小提琴图 (Violin Plot)是用来展现多组数据的散布状态以及概率密度。这种图表联合了箱形图和密度图的特色,次要用来显示数据的散布形态。

从图中咱们能够看出:

  • 击杀英雄数量越多更容易赢,死亡数量越多越容易输(bluekills与bluedeaths左右的区别)。
  • 助攻数量与击杀英雄数量造成的图形态相似,阐明他们对游戏后果的影响差不多。
  • 一血的获得状况与获胜有正相干,然而相关性不如击杀英雄数量显著。
  • 经济差与教训差对于游戏输赢的影响较小。
  • 击杀野怪数量对游戏输赢的影响并不大。
plt.figure(figsize=(18,14))sns.heatmap(round(x.corr(),2), cmap='Blues', annot=True)plt.show()

同时咱们画出各个特色之间的相关性热力求,色彩越深代表特色之间相关性越强,咱们剔除那些相关性较强的冗余特色。

# 去除冗余特色drop_cols = ['redAvgLevel','blueAvgLevel']x.drop(drop_cols, axis=1, inplace=True)
sns.set(style='whitegrid', palette='muted')# 结构两个新特色x['wardsPlacedDiff'] = x['blueWardsPlaced'] - x['redWardsPlaced']x['wardsDestroyedDiff'] = x['blueWardsDestroyed'] - x['redWardsDestroyed']data = x[['blueWardsPlaced','blueWardsDestroyed','wardsPlacedDiff','wardsDestroyedDiff']].sample(1000)data_std = (data - data.mean()) / data.std()data = pd.concat([y, data_std], axis=1)data = pd.melt(data, id_vars='blueWins', var_name='Features', value_name='Values')plt.figure(figsize=(10,6))sns.swarmplot(x='Features', y='Values', hue='blueWins', data=data)plt.xticks(rotation=45)plt.show()

咱们画出了插眼数量的散点图,发现不存在插眼数量与游戏输赢间的显著法则。猜想因为钻石分段以上在哪插眼在哪好排眼都是套路,所以数据中前十分钟插眼数拔眼数对游戏的影响不大。所以咱们临时先把这些特色去掉。

## 去除和眼位相干的特色drop_cols = ['blueWardsPlaced','blueWardsDestroyed','wardsPlacedDiff',            'wardsDestroyedDiff','redWardsPlaced','redWardsDestroyed']x.drop(drop_cols, axis=1, inplace=True)
x['killsDiff'] = x['blueKills'] - x['blueDeaths']x['assistsDiff'] = x['blueAssists'] - x['redAssists']x[['blueKills','blueDeaths','blueAssists','killsDiff','assistsDiff','redAssists']].hist(figsize=(12,10), bins=20)plt.show()

咱们发现击杀、死亡与助攻数的数据分布差异不大。然而击杀减去死亡、助攻减去死亡的散布与原散布差异很大,因而咱们新结构这么两个特色。

data = x[['blueKills','blueDeaths','blueAssists','killsDiff','assistsDiff','redAssists']].sample(1000)data_std = (data - data.mean()) / data.std()data = pd.concat([y, data_std], axis=1)data = pd.melt(data, id_vars='blueWins', var_name='Features', value_name='Values')plt.figure(figsize=(10,6))sns.swarmplot(x='Features', y='Values', hue='blueWins', data=data)plt.xticks(rotation=45)plt.show()

从上图咱们能够发现击杀数与死亡数与助攻数,以及咱们结构的特色对数据都有较好的分类能力。

data = pd.concat([y, x], axis=1).sample(500)sns.pairplot(data, vars=['blueKills','blueDeaths','blueAssists','killsDiff','assistsDiff','redAssists'],              hue='blueWins')plt.show()

一些特色两两组合后对于数据的划分能力也有晋升。

x['dragonsDiff'] = x['blueDragons'] - x['redDragons']x['heraldsDiff'] = x['blueHeralds'] - x['redHeralds']x['eliteDiff'] = x['blueEliteMonsters'] - x['redEliteMonsters']data = pd.concat([y, x], axis=1)eliteGroup = data.groupby(['eliteDiff'])['blueWins'].mean()dragonGroup = data.groupby(['dragonsDiff'])['blueWins'].mean()heraldGroup = data.groupby(['heraldsDiff'])['blueWins'].mean()fig, ax = plt.subplots(1,3, figsize=(15,4))eliteGroup.plot(kind='bar', ax=ax[0])dragonGroup.plot(kind='bar', ax=ax[1])heraldGroup.plot(kind='bar', ax=ax[2])print(eliteGroup)print(dragonGroup)print(heraldGroup)plt.show()
eliteDiff-2    0.286301-1    0.368772 0    0.500683 1    0.632093 2    0.735211Name: blueWins, dtype: float64dragonsDiff-1    0.374173 0    0.500000 1    0.640940Name: blueWins, dtype: float64heraldsDiff-1    0.387729 0    0.498680 1    0.595046Name: blueWins, dtype: float64

咱们结构了两队之间是否拿到龙、是否拿到峡谷先锋、击杀大型野怪的数量差值,发现在游戏的后期拿到龙比拿到峡谷先锋更容易获得胜利。拿到大型野怪的数量和胜率也存在着强相干。

x['towerDiff'] = x['blueTowersDestroyed'] - x['redTowersDestroyed']data = pd.concat([y, x], axis=1)towerGroup = data.groupby(['towerDiff'])['blueWins']print(towerGroup.count())print(towerGroup.mean())fig, ax = plt.subplots(1,2,figsize=(15,5))towerGroup.mean().plot(kind='line', ax=ax[0])ax[0].set_title('Proportion of Blue Wins')ax[0].set_ylabel('Proportion')towerGroup.count().plot(kind='line', ax=ax[1])ax[1].set_title('Count of Towers Destroyed')ax[1].set_ylabel('Count')
towerDiff-2      27-1     347 0    9064 1     406 2      28 3       6 4       1Name: blueWins, dtype: int64towerDiff-2    0.185185-1    0.216138 0    0.498124 1    0.741379 2    0.964286 3    1.000000 4    1.000000Name: blueWins, dtype: float64Text(0,0.5,'Count')

推塔是英雄联盟这个游戏的外围,因而推塔数量可能与游戏的输赢有很大关系。咱们绘图发现,只管前十分钟推掉第一座进攻塔的概率很低,然而一旦某只队伍推掉第一座进攻塔,取得游戏的胜率将大大增加。

Step5:利用 LightGBM 进行训练与预测

## 为了正确评估模型性能,将数据划分为训练集和测试集,并在训练集上训练模型,在测试集上验证模型性能。from sklearn.model_selection import train_test_split## 抉择其类别为0和1的样本 (不包含类别为2的样本)data_target_part = ydata_features_part = x## 测试集大小为20%, 80%/20%分x_train, x_test, y_train, y_test = train_test_split(data_features_part, data_target_part, test_size = 0.2, random_state = 2020)
## 导入LightGBM模型from lightgbm.sklearn import LGBMClassifier## 定义 LightGBM 模型 clf = LGBMClassifier()# 在训练集上训练LightGBM模型clf.fit(x_train, y_train)
LGBMClassifier()
## 在训练集和测试集上散布利用训练好的模型进行预测train_predict = clf.predict(x_train)test_predict = clf.predict(x_test)from sklearn import metrics## 利用accuracy(准确度)【预测正确的样本数目占总预测样本数目标比例】评估模型成果print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_train,train_predict))print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test,test_predict))## 查看混同矩阵 (预测值和实在值的各类状况统计矩阵)confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)print('The confusion matrix result:\n',confusion_matrix_result)# 利用热力求对于后果进行可视化plt.figure(figsize=(8, 6))sns.heatmap(confusion_matrix_result, annot=True, cmap='Blues')plt.xlabel('Predicted labels')plt.ylabel('True labels')plt.show()
The accuracy of the Logistic Regression is: 0.8447425028470201The accuracy of the Logistic Regression is: 0.722165991902834The confusion matrix result: [[714 300] [249 713]]

咱们能够发现共有718 + 707个样本预测正确,306 + 245个样本预测谬误。

Step7: 利用 LightGBM 进行特征选择

LightGBM的特征选择属于特征选择中的嵌入式办法,在LightGBM中能够用属性feature_importances_去查看特色的重要度。

sns.barplot(y=data_features_part.columns, x=clf.feature_importances_)
<matplotlib.axes._subplots.AxesSubplot at 0x7fcb1048e350>

总经济差距等特色,助攻数量、击杀死亡数量等特色都具备很大的作用。插眼数、推塔数对模型的影响并不大。

首次之外,咱们还能够应用LightGBM中的下列重要属性来评估特色的重要性。

  • gain:当利用特色做划分的时候的评估基尼指数
  • split:是以特色用到的次数来评估
from sklearn.metrics import accuracy_scorefrom lightgbm import plot_importancedef estimate(model,data):    #sns.barplot(data.columns,model.feature_importances_)    ax1=plot_importance(model,importance_type="gain")    ax1.set_title('gain')    ax2=plot_importance(model, importance_type="split")    ax2.set_title('split')    plt.show()def classes(data,label,test):    model=LGBMClassifier()    model.fit(data,label)    ans=model.predict(test)    estimate(model, data)    return ans ans=classes(x_train,y_train,x_test)pre=accuracy_score(y_test, ans)print('acc=',accuracy_score(y_test,ans))

acc= 0.722165991902834

这些图同样能够帮忙咱们更好的理解其余重要特色。

Step8: 通过调整参数取得更好的成果

LightGBM中包含但不限于下列对模型影响较大的参数:

  1. learning_rate: 有时也叫作eta,零碎默认值为0.3。每一步迭代的步长,很重要。太大了运行准确率不高,太小了运行速度慢。
  2. num_leaves:零碎默认为32。这个参数管制每棵树中最大叶子节点数量。
  3. feature_fraction:零碎默认值为1。咱们个别设置成0.8左右。用来管制每棵随机采样的列数的占比(每一列是一个特色)。
  4. max_depth: 零碎默认值为6,咱们罕用3-10之间的数字。这个值为树的最大深度。这个值是用来管制过拟合的。max_depth越大,模型学习的更加具体。

调节模型参数的办法有贪婪算法、网格调参、贝叶斯调参等。这里咱们采纳网格调参,它的根本思维是穷举搜寻:在所有候选的参数抉择中,通过循环遍历,尝试每一种可能性,体现最好的参数就是最终的后果

## 从sklearn库中导入网格调参函数from sklearn.model_selection import GridSearchCV## 定义参数取值范畴learning_rate = [0.1, 0.3, 0.6]feature_fraction = [0.5, 0.8, 1]num_leaves = [16, 32, 64]max_depth = [-1,3,5,8]parameters = { 'learning_rate': learning_rate,              'feature_fraction':feature_fraction,              'num_leaves': num_leaves,              'max_depth': max_depth}model = LGBMClassifier(n_estimators = 50)## 进行网格搜寻clf = GridSearchCV(model, parameters, cv=3, scoring='accuracy',verbose=3, n_jobs=-1)clf = clf.fit(x_train, y_train)
[CV 1/3] END feature_fraction=1, learning_rate=0.6, max_depth=8, num_leaves=64;, score=0.672 total time=   0.1s[CV 3/3] END feature_fraction=1, learning_rate=0.6, max_depth=8, num_leaves=64;, score=0.685 total time=   0.1s[LightGBM] [Warning] feature_fraction is set=1, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=1
## 网格搜寻后的最好参数为clf.best_params_
{'feature_fraction': 1, 'learning_rate': 0.1, 'max_depth': 3, 'num_leaves': 16}
## 在训练集和测试集上散布利用最好的模型参数进行预测## 定义带参数的 LightGBM模型 clf = LGBMClassifier(feature_fraction = 0.8,                    learning_rate = 0.1,                    max_depth= 3,                    num_leaves = 16)# 在训练集上训练LightGBM模型clf.fit(x_train, y_train)train_predict = clf.predict(x_train)test_predict = clf.predict(x_test)## 利用accuracy(准确度)【预测正确的样本数目占总预测样本数目标比例】评估模型成果print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_train,train_predict))print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test,test_predict))## 查看混同矩阵 (预测值和实在值的各类状况统计矩阵)confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)print('The confusion matrix result:\n',confusion_matrix_result)# 利用热力求对于后果进行可视化plt.figure(figsize=(8, 6))sns.heatmap(confusion_matrix_result, annot=True, cmap='Blues')plt.xlabel('Predicted labels')plt.ylabel('True labels')plt.show()
The accuracy of the Logistic Regression is: 0.7440212577502214The accuracy of the Logistic Regression is: 0.7317813765182186The confusion matrix result: [[722 289] [241 724]]

本来有306 + 245个谬误,当初有 287 + 230个谬误,带来了显著的正确率晋升。

3.1基本参数调整

  1. num_leaves参数 这是管制树模型复杂度的主要参数,个别的咱们会使num_leaves小于(2的max_depth次方),以避免过拟合。因为LightGBM是leaf-wise建树与XGBoost的depth-wise建树办法不同,num_leaves比depth有更大的作用。、
  2. min_data_in_leaf 这是解决过拟合问题中一个十分重要的参数. 它的值取决于训练数据的样本个树和 num_leaves参数. 将其设置的较大能够防止生成一个过深的树, 但有可能导致欠拟合. 理论利用中, 对于大数据集, 设置其为几百或几千就足够了.
  3. max_depth 树的深度,depth 的概念在 leaf-wise 树中并没有多大作用, 因为并不存在一个从 leaves 到 depth 的正当映射。

3.2针对训练速度的参数调整

  1. 通过设置 bagging_fraction 和 bagging_freq 参数来应用 bagging 办法。
  2. 通过设置 feature_fraction 参数来应用特色的子抽样。
  3. 抉择较小的 max_bin 参数。
  4. 应用 save_binary 在将来的学习过程对数据加载进行减速。

3.3针对准确率的参数调整

  1. 应用较大的 max_bin (学习速度可能变慢)
  2. 应用较小的 learning_rate 和较大的 num_iterations
  3. 应用较大的 num_leaves (可能导致过拟合)
  4. 应用更大的训练数据
  5. 尝试 dart 模式

3.4针对过拟合的参数调整

  1. 应用较小的 max_bin
  2. 应用较小的 num_leaves
  3. 应用 min_data_in_leaf 和 min_sum_hessian_in_leaf
  4. 通过设置 bagging_fraction 和 bagging_freq 来应用 bagging
  5. 通过设置 feature_fraction 来应用特色子抽样
  6. 应用更大的训练数据
  7. 应用 lambda_l1, lambda_l2 和 min_gain_to_split 来应用正则
  8. 尝试 max_depth 来防止生成过深的树

4.总结

LightGBM的次要长处:

  1. 简略易用。提供了支流的Python\C++\R语言接口,用户能够轻松应用LightGBM建模并取得相当不错的成果。
  2. 高效可扩大。在解决大规模数据集时高效迅速、高准确度,对内存等硬件资源要求不高。
  3. 鲁棒性强。相较于深度学习模型不须要精密调参便能获得近似的成果。
  4. LightGBM间接反对缺失值与类别特色,无需对数据额定进行非凡解决

LightGBM的次要毛病:

  1. 绝对于深度学习模型无奈对时空地位建模,不能很好地捕捉图像、语音、文本等高维数据。
  2. 在领有海量训练数据,并能找到适合的深度学习模型时,深度学习的精度能够遥遥领先LightGBM。

本我的项目链接:https://www.heywhale.com/home/column/64141d6b1c8c8b518ba97dcc

参考链接:https://tianchi.aliyun.com/course/278/3424


自己最近打算整合ML、DRL、NLP等相干畛域的体系化我的项目课程,不便入门同学疾速把握相干常识。申明:局部我的项目为网络经典我的项目不便大家疾速学习,后续会一直削减实战环节(较量、论文、事实利用等)。

  • 对于机器学习这块布局为:根底入门机器学习算法--->简略我的项目实战--->数据建模较量----->相干事实中利用场景问题解决。一条路线帮忙大家学习,疾速实战。
  • 对于深度强化学习这块布局为:根底单智能算法教学(gym环境为主)---->支流多智能算法教学(gym环境为主)---->单智能多智能题实战(论文复现偏业务如:无人机优化调度、电力资源调度等我的项目利用)
  • 自然语言解决相干布局:除了单点算法技术外,次要围绕常识图谱构建进行:信息抽取相干技术(含智能标注)--->常识交融---->常识推理---->图谱利用

上述对于你把握后的期许:

  1. 对于ML,心愿你后续能够乱杀数学建模相干较量(加入就获奖保底,top还是难的须要钻研)
  2. 能够理论解决事实中一些优化调度问题,而非停留在gym环境下的一些游戏demo玩玩。(更深层次可能须要本人钻研了,难度还是很大的)
  3. 把握可常识图谱全流程构建其中各个重要环节算法,蕴含图数据库相干常识。

这三块畛域耦合状况比拟大,后续会通过比方:搜寻举荐零碎整个我的项目进行耦合,各项算法都会耦合在其中。举例:常识图谱就会用到(图算法、NLP、ML相干算法),搜寻举荐零碎(除了该畛域召回粗排精排重排混排等算法外,还有强化学习、常识图谱等耦合在其中)。饼画的有点大,前面缓缓实现。